Spring 3控制器 - 通过流程维护模型

时间:2010-06-10 13:07:21

标签: java model spring-mvc controller

我确信有一些方法可以在这里完成我想要的,但我无法在文档中找到它

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping(value = "/test")
public class TestController {

    @RequestMapping(value = "/one")
    public String one(Model m) {
        System.out.println("one: m = " + m);
        m.addAttribute("one", "valueone");
        return "redirect:two";
    }

    @RequestMapping(value = "/two")
    public String two(Model m) {
        System.out.println("two: m = " + m);
        return "redirect:three";
    }

    @RequestMapping(value = "/three")
    public String three(Model m) {
        System.out.println("three: m = " + m);
        return "redirect:one/two/three";
    }

    @RequestMapping(value = "/one/two/three")
    public String dest(Model m) {
        System.out.println("one/two/three: m = " + m);
        return "test";
    }
}

我在这里期望的是看到值为“valueone”的模型属性“one”应该存在于方法调用two(),three()和dest()中,但它是非常引人注目的缺席。我如何按预期完成这项工作?

1 个答案:

答案 0 :(得分:2)

您需要在控制器上使用@SessionAttributes注释,然后使用SessionStatus在完成属性时告诉框架。

@Controller
@RequestMapping(value = "/test")
@SessionAttributes("one")
public class TestController {
    // ...

    @RequestMapping(value = "/one/two/three")
    public String dest(Model m, SessionStatus status) {
        System.out.println("one/two/three: m = " + m);
        status.setComplete();
        return "test";
    }
}

请参阅http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/bind/annotation/SessionAttributes.html