Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

spring - 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...