2011-12-26 127 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