spring 从一种控制器方法重定向到另一种控制器方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17218856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:02:10  来源:igfitidea点击:

Redirect from one controller method to another controller method

springjspspring-mvctiles

提问by Carl

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.

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

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.

我试图在 pageviews.properties 文件中创建一个新条目。这样我就可以从 Controller1 返回这个名称,它会从 xml 文件中查找我的瓷砖定义名称。

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;

但是,当我尝试返回如下所示时,我的 URL 包含 createRejectionEmail 作为 URL 的一部分 - 而不是使用它在瓷砖 defs 中进行查找。mav.setViewName("redirectRejectionEmail"); 返回 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!

任何帮助和指导将不胜感激!

回答by Stephen Dillon

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.

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

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

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

或者,您可以在控制器方法中接收 HttpServletResponse 并返回重定向。

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

回答by Yasir Shabbir Choudhary

@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.

当此方法处理程序调用时,它会重定向到 /app/timeout 控制器。