Spring MVC 3.1 RedirectAttributes无效 [英] Spring MVC 3.1 RedirectAttributes is not working

查看:137
本文介绍了Spring MVC 3.1 RedirectAttributes无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Spring MVC 3.1中实现RedirectAttributes功能 - 发布

I'm trying to implement RedirectAttributes feature in Spring MVC 3.1-Release

我正在将简单表单​​发送到Post URL并希望看到值I我发送重定向:

I'm sending simple form to Post URL and would like to see the the value I'm sending in redirect:

我的控制器看起来像这样:

my Controller looks like this:

@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看起来像这样:

Now my Servlet XML looks like this:

<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是空的。

My problem is that in success.html view redirectAttributes is empty.

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

什么都不打印。

推荐答案

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

It does not work because You are using ModelAndView as a return value.

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

See JIRA Issue: https://jira.springsource.org/browse/SPR-8968

这篇关于Spring MVC 3.1 RedirectAttributes无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆