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

发布于 2024-09-04 22:06:15 字数 1080 浏览 2 评论 0原文

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

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";
    }
}

我在这里期望的是看到模型属性“one”的值为“valueone” " 应该出现在方法调用 Two()、Three() 和 dest() 中,但它的缺失非常明显。我将如何使这项工作按预期进行?

I'm sure there is some way to accomplish what I'd like here, but I haven't been able to find it in the documentation

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";
    }
}

What I would expect here is to see that the model attribute "one" with value of "valueone" should be present in the method calls two(), three() and dest(), however it is quite conspicuous by it's absence. How would I make this work as expected?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奈何桥上唱咆哮 2024-09-11 22:06:15

您需要在控制器上使用 @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

You need to use the @SessionAttributes annotation on the controller, then use a SessionStatus to tell the framework when you are done with the attribute.

@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";
    }
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文