title: Spring MVC controller redirect to some URL after login
body: <p>I have a Spring MVC Controller with handlers like so:</p>
  
  <pre><code>@RequestMapping(value = "/account/login", method = RequestMethod.GET)
  public String login() {
      return "login";
  }
  
  
  @RequestMapping(value = "/account/login", method = RequestMethod.POST, params = "login")
  public String login(@RequestParam(value = "username") String username, 
          @RequestParam(value = "password") String password) {
  
      // do authentication
      return "home";
  }
  </code></pre>
  
  <p>The form in the login.html page POSTs to account/login (the same url). I'd like that after the authentication, I redirect the user to the home page of my application, so that he sees <code>www.mywebappexample.com</code> in address bar instead of <code>www.mywebappexample.com/account/login</code>. When I return the string from the login method, it renders the proper html but I don't have the URL I want to show. How can I redirect?</p>
  
  <p><strong>Edit:</strong> I had to prefix my controller return String with <code>redirect:</code>. This works if you have a view resolver that subclasses UrlBasedViewResolver <a href="http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/web/servlet/view/UrlBasedViewResolver.html" rel="nofollow noreferrer">UrlBasedViewResolver</a>. Thymeleaf's view resolver doesn't do that but it does have the behavior -> <a href="http://www.thymeleaf.org/apidocs/thymeleaf-spring3/2.0.8/org/thymeleaf/spring3/view/ThymeleafViewResolver.html" rel="nofollow noreferrer">ThymeleafViewResolver</a>. Here's my servlet-context.xml (I'm using thymeleaf):</p>
  
  <pre><code>&lt;bean id="templateResolver"
        class="org.thymeleaf.templateresolver.ServletContextTemplateResolver"&gt;
      &lt;property name="prefix" value="/WEB-INF/"/&gt;
      &lt;property name="suffix" value=".html"/&gt;
      &lt;property name="templateMode" value="HTML5"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine"&gt;
      &lt;property name="templateResolver" ref="templateResolver"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="viewResolver" class="org.thymeleaf.spring3.view.ThymeleafViewResolver"&gt;
      &lt;property name="templateEngine" ref="templateEngine"/&gt;
      &lt;property name="order" value="1"/&gt;
  &lt;/bean&gt;
  </code></pre>