Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect from one controller method to another controller method

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!

like image 585
Carl Avatar asked Jun 20 '13 16:06

Carl


People also ask

How do I redirect one controller to another?

In this blog you will learn how to Redirect from One Controller Action to Another. Step1: Create an ASP.net MVC project. Choose ASP.Net MVC project from template and Press Next, then name the empty project as RoutingExample and click ok. Step 2: Add two controllers.

Can we call method from one controller to another controller?

Yes, you can call a method of another controller. The controller is also a simple class.

Which return type is used to redirect to an action method of another controller?

An ActionResult is a return type of a controller method in MVC. Action methods help us to return models to views, file streams, and also redirect to another controller's Action method.

How do you redirect an action method?

Redirects to the specified action using the action name. Redirects to the specified action using the action name and route values. Redirects to the specified action using the action name and controller name. Redirects to the specified action using the action name and route dictionary.


1 Answers

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"); } 
like image 166
Stephen Dillon Avatar answered Sep 17 '22 13:09

Stephen Dillon