51338

Using Redirect in Spring Boot

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.

Recommend

  • Deployed Simple HelloWorld with Spring MVC app (without web.xml) gives 404 error
  • Spring boot: apply @Configuration to certain package only
  • How can I access the repository from the entity in Spring Boot?
  • springSecurityFilterChain bean not found using MockMVC and @ComponentScan
  • SpringBoot CRUD Repository fail to Autowire
  • Difference between Component-Scan and @Component ?
  • How to avoid the use of Spring annotations in my java beans and use them only in the configuration c
  • Dangers of @ComponentScan'ing all packages with filters
  • how to exclude java sources from a third party jar?
  • Hudson dependencies
  • Spring @Transactional - javax.persistence.TransactionRequiredException
  • How to subscribe to an event without preventing garbage collection?
  • How do we set maximum_bad_records when loading a Bigquery table from dataflow?
  • How to get or calculate size of Azure File/Share or Service
  • How to set up precision attribute used by @Column annotation?
  • TypeScript DefinitelyTyped Ember.js d.ts doesn't compile
  • maven-dependency-plugin ignores outputDirectory configuration
  • How to create MSBuild targets file to deploy files?
  • Maven-Release-Plugin: Force to use specific version of scm provider
  • Hibernate - Cannot connect to DB
  • Maven use Encrypted passwords in POM
  • How to send params in url query_string in Symfony?
  • How to package a jar and all dependencies within a new jar with maven
  • How do I configure Maven Cargo to use an embedded Tomcat server?
  • Symfony2 redirect to https route fails (uses wrong port)
  • How to force Composer to download a local package?
  • How to configure Cygnus in relation to Orion and Cosmos
  • MAVEN : Run Multiple Maven Project using Maven Test
  • Loading fixtures in sails tests
  • Unzip archive in .Net CORE 1.0
  • Add reference to ASP.NET 5 Class Library from Framework 4.5 Class Library Project
  • VSCode change debug shell to bash on windows
  • C#: Import/Export Settings into/from a File
  • How to retrieve information from antrun back to maven?
  • How can I replace the server in Web Component Tester
  • Is there some graphical way to create my own configuration file on SonarLint?
  • Spring Cloud Microservice Architecture Confusion
  • How can I send an e-mail from a vbs script
  • Is there a mandatory requirement to switch app.yaml?
  • UserPrincipal.Current returns apppool on IIS