在Spring MVC中重定向

在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";
    }

 }

我得到:


javax.servlet.ServletException:
无法解析名为
的视图'redirect:index.html'在servlet中,带
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)

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?

试试这个,如果你已经正确配置你的视图解析器它应该可行

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

 return "redirect:/index.html";