28

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?

Aritz
  • 30,971
  • 16
  • 136
  • 217
mjf
  • 301
  • 1
  • 3
  • 4
  • Does the exception occure, before or after the redirect is send to the browser? – Ralph Jan 03 '11 at 12:45
  • It's also worth pointing out that BindingResult will not be persisted after the redirect. In other words if you redirect to your input form (in this example index.html) the errors will not show up as they are lost due to the redirect. I had to solve this just before using a HandlerInterceptor. – garyj Mar 13 '11 at 05:09
  • @garyj Do you have an example of your HandlerInterceptor? – blong824 Apr 19 '11 at 19:09

8 Answers8

59

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

 return "redirect:/index.html";
jmj
  • 237,923
  • 42
  • 401
  • 438
25

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.

axtavt
  • 239,438
  • 41
  • 511
  • 482
8

For completing the answers, Spring MVC uses viewResolver(for example, as axtavt metionned, InternalResourceViewResolver) to get the specific view. Therefore the first step is making sure that a viewResolver is configured.

Secondly, you should pay attention to the url of redirection(redirect or forward). A url starting with "/" means that it's a url absolute in the application. As Jigar says,

return "redirect:/index.html";  

should work. If your view locates in the root of the application, Spring can find it. If a url without a "/", such as that in your question, it means a url relative. It explains why it worked before and don't work now. If your page calling "redirect" locates in the root by chance, it works. If not, Spring can't find the view and it doesn't work.

Here is the source code of the method of RedirectView of Spring

protected void renderMergedOutputModel(  
Map<String, Object> model, HttpServletRequest request, HttpServletResponse response)  
throws IOException {  
  // Prepare target URL.  
  StringBuilder targetUrl = new StringBuilder();  
  if (this.contextRelative && getUrl().startsWith("/")) {  
    // Do not apply context path to relative URLs.  
    targetUrl.append(request.getContextPath());  
  }  
  targetUrl.append(getUrl());  

  // ...  

  sendRedirect(request, response, targetUrl.toString(), this.http10Compatible);  
}  
Qianyue
  • 1,767
  • 19
  • 24
4

try to change this in your dispatcher-servlet.xml

<!-- Your View Resolver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
    <property name="basenames" value="views" />
    <property name="order" value="1" />
</bean>   
<!-- UrlBasedViewResolver to Handle Redirects & Forward -->
<bean id="urlViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" />
    <property name="order" value="2" />
</bean>        

What happens is clearly explained here http://projects.nigelsim.org/wiki/RedirectWithSpringWebMvc

Fareed Alnamrouti
  • 30,771
  • 4
  • 85
  • 76
4

Try this

HttpServletResponse response;       
response.sendRedirect(".../webpage.xhtml");
Shifat
  • 732
  • 1
  • 6
  • 20
  • 8
    @Bonatti - The fact that OP is not waiting on a reply has nothing to do with the long-term usefulness of this answer... and there are still people out there who want to know how to redirect in spring. – Ben Barden Oct 03 '17 at 20:37
  • This solution works for me. Full method is following: @GetMapping(value = "/oldpage") void oldPortalPage(HttpServletRequest request, HttpServletResponse response) throws Exception { response.sendRedirect(request.getContextPath() + "/newpage"); } .... – Ruslan Nov 23 '18 at 18:45
2

Axtavt answer is correct.

This is how your resolver should look like (annotations based):

    @Bean
UrlBasedViewResolver resolver(){
    UrlBasedViewResolver resolver = new UrlBasedViewResolver();

    resolver.setPrefix("/views/");
    resolver.setSuffix(".jsp");
    resolver.setViewClass(JstlView.class);

    return resolver;
}

Obviously the name of your views directory should change based on your project.

Gerardo Martínez
  • 793
  • 2
  • 12
  • 26
-2

It is possible to define a urlBasedViewResolver in your properties file:

excel.(class)=fi.utu.seurantaraporttisuodatin.service.Raportti  
index.(class)=org.springframework.web.servlet.view.urlBasedView  
index.viewClass =org.springframework.web.servlet.view.JstlView  
index.prefix = /WEB-INF/jsp/  
index.suffix =.jsp
Robert Columbia
  • 6,313
  • 15
  • 32
  • 40
mjgirl
  • 1,214
  • 7
  • 24
  • 42
-2

i know this is late , but you should try redirecting to a path and not to a file ha ha