Java – Redirect in Spring MVC

javaspringspring-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?

Best Answer

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

 return "redirect:/index.html";
Related Topic