从一种控制器方法重定向到另一种控制器方法 [英] Redirect from one controller method to another controller method

查看:186
本文介绍了从一种控制器方法重定向到另一种控制器方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中使用Spring 3和Tiles 2,并且在重定向方面遇到了一些麻烦. 最好是,我希望能够仅从Controller1方法调用或重定向到Controller2方法,但到目前为止还没有成功.

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.

我试图在pageviews.properties文件中创建一个新条目.这样,我就可以从Controller1返回此名称,它将从xml文件中查找我的tile def名称.

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

但是,当我尝试返回如下所示的URL时,URL包含createRejectionEmail作为URL的一部分-而不是使用它在tile def中进行查找. mav.setViewName("redirectRejectionEmail"); 返回mav;

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>

我当前的配置在下面.

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

推荐答案

在您的控制器中,您可以将返回类型更改为ModelAndView并在下面返回代码.这将重定向请求并为新的URL调用控制器.

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

或者,您可以在控制器方法中加入HttpServletResponse并返回重定向.

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

public void myController(HttpServletResponse response){
response.sendRedirect("/myURL");
}

这篇关于从一种控制器方法重定向到另一种控制器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆