walnutmon
walnutmon

Reputation: 5953

Spring 3 Controllers - Maintaining Model Through Flow

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?

Upvotes: 0

Views: 849

Answers (1)

ptomli
ptomli

Reputation: 11818

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

Upvotes: 2

Related Questions