Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect in Spring MVC

Why can't I get this to work in my Controller

@RequestMapping(method = RequestMethod.POST) public String onSubmit(     Model model,     @ModelAttribute("form") Form form,     BindingResult result, HttpServletRequest request)     throws IOException, WriteException, BiffException {      if (result.hasErrors()) {         return "redirect:index.html";     }   } 

I get:

javax.servlet.ServletException: Could not resolve view with name 'redirect:index.html' in servlet with name 'dispatcher'
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1042)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:798)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)

I've got this to work before. Why not now?

like image 532
mjf Avatar asked Jan 03 '11 12:01

mjf


2 Answers

Try this, it should work if you have configured your view resolver properly

 return "redirect:/index.html"; 
like image 102
jmj Avatar answered Sep 19 '22 02:09

jmj


Also note that redirect: and forward: prefixes are handled by UrlBasedViewResolver, so you need to have at least one subclass of UrlBasedViewResolver among your view resolvers, such as InternalResourceViewResolver.

like image 45
axtavt Avatar answered Sep 22 '22 02:09

axtavt