
Question:
Im having trouble using redirect functionality within my spring boot application. As shown in my code below im returning "redirect:/aucConfirm/" but when i initiate this i get a "This application has no explicit mapping" error.
@Controller
@RequestMapping("/")
public class WelcomeController {
@Autowired
AuctionItemRepository aucRepository;
// inject via application.properties
@Value("${welcome.message:test}")
private String message = "Hello World";
@RequestMapping(value = "/")
public String welcome(Map<String, Object> model) {
model.put("message", this.message);
return "welcome";
}
@RequestMapping(value = "/sell", method = RequestMethod.GET)
public String addAuction(Model model, HttpServletRequest request) {
model.addAttribute("newAuction", new AuctionItem());
return "NewAuction";
}
@RequestMapping(value = "/sell", method = RequestMethod.POST)
public String saveAuction(@ModelAttribute AuctionItem newAuction, RedirectAttributes attributes){
return "redirect:/aucConfirm";
}
@RequestMapping(value = "/aucConfirm", method = RequestMethod.GET)
public String confirmNewAuction(Model model, HttpServletRequest request) {
return "aucConfirm";
}
}
This is my current configuration class -
@SpringBootApplication
@ComponentScan
@EnableJpaRepositories("All")
@EntityScan("All")
public class AuctionWebApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(AuctionWebApplication.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(AuctionWebApplication.class, args);
}
@Bean
DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/auctiondb?zeroDateTimeBehavior=convertToNull");
dataSource.setUsername("root");
dataSource.setPassword("toor");
return dataSource;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
entityManager.setDataSource(dataSource);
entityManager.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
entityManager.setPackagesToScan("All");
Properties jpaProperties = new Properties();
jpaProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
jpaProperties.setProperty("hibernate.hbm2ddl.auto", "update");
jpaProperties.setProperty("hibernate.id.new_generator_mappings", "false");
entityManager.setJpaProperties(jpaProperties);
return entityManager;
}
}
<em>Update</em>
Could it possibly have something to do with my jsp page that im trying to establish the recommit? I have a mvc form here -
<mvc:form class="form-inline" action="${action}" method="post" modelAttribute="newAuction" id="addNewAuc">
and when the user hits the next button it should hit the "redirect:/aucConfirm" -
<button type="submit" name="submit" class="btn btn-success" form="addNewAuc">Next</button>
Is there anything wrong with this code? Im running out of ideas at this point. Do i need to add anything to my spring boot configuration class to get it working? - Please help!
<em>Update 2</em>
<a href="http://localhost:8080/AuctionWebsite/sell" rel="nofollow">http://localhost:8080/AuctionWebsite/sell</a> <a href="https://i.stack.imgur.com/fDhqQ.png" rel="nofollow"><img alt="" class="b-lazy" data-src="https://i.stack.imgur.com/fDhqQ.png" data-original="https://i.stack.imgur.com/fDhqQ.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>
When i enter details int this form and select next i want it to redirect to- <a href="http://localhost:8080/AuctionWebsite/aucConfirm" rel="nofollow">http://localhost:8080/AuctionWebsite/aucConfirm</a>
However this is what appears-
<a href="https://i.stack.imgur.com/hpC3S.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/hpC3S.png" data-original="https://i.stack.imgur.com/hpC3S.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>
not redirecting at all and remaining with link <a href="http://localhost:8080/AuctionWebsite/sell" rel="nofollow">http://localhost:8080/AuctionWebsite/sell</a>
<a href="http://localhost:8080/AuctionWebsite/aucConfirm" rel="nofollow">http://localhost:8080/AuctionWebsite/aucConfirm</a> works when entered manually
Im getting a little bit desperate at this point, would really appreciate any help.
Answer1:Use this code
redirect:aucConfirm
Instead of
redirect:/aucConfirm
As per your updated question. it seems that
<blockquote>"This application has no explicit mapping" error
</blockquote>Is for /error url;
The actual error is
<blockquote>Validation failed for object 'auctionItem'
</blockquote>So problem is not with redirection , it is something else related to validation. You may find specific error on console log.