gpt4 book ai didi

java - Spring MVC 3.1 RedirectAttributes 不工作

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:00:18 26 4
gpt4 key购买 nike

我正在尝试在 Spring MVC 3.1-Release 中实现 RedirectAttributes 特性

我正在向 Post URL 发送简单的表单,并希望看到我在重定向中发送的值:

我的 Controller 看起来像这样:

@Controller
public class DefaultController {

@RequestMapping(value="/index.html", method=RequestMethod.GET)
public ModelAndView indexView(){
ModelAndView mv = new ModelAndView("index");
return mv;
}

@RequestMapping(value="/greetings.action", method=RequestMethod.POST)
public ModelAndView startTask(@RequestParam("firstName") String firstName,RedirectAttributes redirectAttributes){
redirectAttributes.addFlashAttribute("redirectAttributes.firstName", firstName);
ModelAndView mv = new ModelAndView(new RedirectView("success.html"));
return mv;
}

@RequestMapping(value="/success.html", method=RequestMethod.GET)
public ModelAndView successView(){
ModelAndView mv = new ModelAndView("success");
return mv;
}
}

现在我的 Servlet XML 看起来像这样:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.vanilla.flashscope.controllers" />

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>

我的问题是在 success.html View 中 redirectAttributes 是空的。

<body>
<h5>${redirectAttributes.firstName} </h5>
</body>

什么都不打印。

最佳答案

它不起作用,因为您正在使用 ModelAndView 作为返回值。

参见 JIRA 问题:https://jira.springsource.org/browse/SPR-8968

关于java - Spring MVC 3.1 RedirectAttributes 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8637043/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com