2011-12-26 61 views
3

我想實現RedirectAttributes在Spring MVC配備3.1-RELEASESpring MVC的3.1 RedirectAttributes不工作

我發送簡單的形式發佈網址,並希望看到我送的價值重定向:

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

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

打印什麼。

+0

我也一樣,如果你得到解決方案,請告訴我。 – YNChumak 2012-01-09 20:28:45

+0

是的,我做到了。我在我的社區網站上寫了一些想法,希望它能幫助你。 http://goo.gl/Qbym2 – 2012-01-09 21:42:46

+0

泰克丹尼。我正在循環着同樣的問題。關鍵是返回一個字符串,而不是一個ModelAndView,似乎很奇怪,但工程。 – 2012-02-09 21:02:19

回答

2

看起來你需要刪除「redirectAttributes」。鍵名」

相反的:

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

這樣做:

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

嗨,它在密鑰名稱中有或沒有這個前綴完美的作品。 – 2012-01-24 09:18:50