views:

106

answers:

1

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?

+2  A: 

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

ptomli
great, that's very cool, I have read the SessionAttributes documentation, but I didn't understand it until now. The only thing that worries me is it seems to put at least string values into the url via a Query string, but when I tested it with objects it works great, and doesn't modify the query string at all, do you think that's a bug?
walnutmon
I've never used it to store String attributes so I've not noticed that behaviour. I guess it might be a bug to expose these attributes, though it's possible there's a knob to twiddle somewhere to set this. I can't see immediately where this might be, being unsure what class actually handles the SessionAttributes attribute storage. You have a workaround in any case.
ptomli