簡體   English   中英

Spring MVC 3.1 RedirectAttributes無效

[英]Spring MVC 3.1 RedirectAttributes is not working

我正在嘗試在Spring MVC 3.1-Release中實現RedirectAttributes功能

我正在發送簡單的表單到Post URL,並希望看到我在重定向中發送的值:

我的控制器看起來像這樣:

@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視圖中,redirectAttributes為空。

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

什么都不打印。

它不起作用,因為您使用ModelAndView作為返回值。

請參閱JIRA問題: https//jira.springsource.org/browse/SPR-8968

看起來您需要刪除“redirectAttributes”。 從關鍵名稱“

代替:

     redirectAttributes.addFlashAttribute("redirectAttributes.firstName", firstName);

做這個:

    redirectAttributes.addFlashAttribute("firstName", firstName);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM