36

I am using Spring 3 and Tiles 2 in my application and have a bit of trouble with redirecting. Preferably, I would like to be able to just call or redirect from a Controller1 method to Controller2 method, but so far have been unsuccessful.

I have tried to create a new entry in the pageviews.properties file. That way I could just return this name from Controller1 and it would look up my tiles def name from the xml files.

createRejectionEmail.(parent)=tilesView
createRejectionEmail.url=createRejectionEmail.page

redirectRejectionEmail.(class)=org.springframework.web.servlet.view.RedirectView
rediectRejectionEmail.contextRelative=true
redirectRejectionEmail.url=createRejectionEmail.page

But, when I try returning like shown below my the URL contains createRejectionEmail as part of the URL - instead of using that to do the look up in the tiles defs. mav.setViewName("redirectRejectionEmail"); return mav;

<definition name="createRejectionEmail.page" extends="brandedLayout">
  <put-attribute name="targetFunction" value="status" />
  <put-attribute name="content" value="/WEB  INF/jsp/pages/status/createRejectionEmail.jsp" />
</definition>

My current config is below.

<bean id="resourceViewResolver"
class="org.springframework.web.servlet.view.ResourceBundleViewResolver"
p:order="0" p:basename="config.spring.viewresolution.pageviews"/>



<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
   <list>
  <value>/WEB-INF/jsp/**/views.xml</value>
    </list>
</property>
</bean>

Any help and guidance would be greatly appreciated!

Carl
  • 635
  • 2
  • 8
  • 15

2 Answers2

65

From your controller you can change the return type to be a ModelAndView and return code below. This will re-direct the request and call the controller for the new URL.

return new ModelAndView("redirect:/myURL");

Alternatively you could take in the HttpServletResponse in your controller method and return a redirect.

public void myController(HttpServletResponse response){
response.sendRedirect("/myURL");
}
Stephen Dillon
  • 775
  • 7
  • 12
  • Hi thanks for your response. I tried this and I get the following.Could not resolve view with name 'redirect:/myapppath/status/displayEmailRejectionForm.html' in servlet with name 'Spring-MVC-for-OA'. SHoudl I be using a different view resolver? Currently, we are using the ResourceBundleViewResolver with tiles2 – Carl Jun 21 '13 at 18:06
  • OK Whew! It's working! Thank you so much for your help. I also had to add the UrlBasedViewResolver to my application context. I really appreciate you taking the time to respond! – Carl Jun 21 '13 at 18:23
  • This seems to be working fine even with String return type for the @RequestMapping annotated methods. Like in Carl's case the key is to add the UrlBasedViewResolver class configuration. – Marin Mar 07 '16 at 22:10
  • 1
    This works for me without any modifications. using spring 4.1+ and hibernate 5.0+. – Akah Apr 22 '16 at 15:35
  • this is vary userfull – Ravikumar Rathod Sep 14 '17 at 06:51
9
@RequestMapping(value = "/timeout", method = RequestMethod.GET)
    public ModelAndView loginForm(HttpServletRequest request,HttpServletResponse response) {


                return new ModelAndView("redirect:/app/timeout");

    }

When this method handler call then it redirect to the /app/timeout controller.

Yasir Shabbir Choudhary
  • 2,458
  • 2
  • 27
  • 31
  • Works like a charm, and the best is that if your application have context then the this method solve it without need to pass explicitly. – deFreitas Nov 29 '16 at 14:25