Announcement Announcement Module
Collapse
No announcement yet.
2.5 MVC annotations and model propagation problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 2.5 MVC annotations and model propagation problem

    I have an application with a "home" controller that puts a Client object in the model and the "home" view displays the opening application interfaces. On that interface, selecting an option sends the user to another controller, but I can't seem to get the client object out of the model; the model in the second controller does not contain anything. I'm clearly not using the annotations correctly, but I can't seem to find out where. Here is the code:

    HomeController:
    @Controller
    public class HomeController {


    @RequestMapping(value="/home.htm", method=RequestMethod.GET)
    public String home(ModelMap model, WebRequest req) {
    // Client c;
    // ... load client code deleted
    model.addAttribute("client", c);

    return "home";
    }
    }

    The second controller:
    @Controller
    public class IndexesController {

    @RequestMapping(value="/allindexes.htm", method=RequestMethod.GET)
    public String showAllIndexes(final ModelMap m, WebRequest req){
    Client c = (Client)m.get("client"); // NULL!!!

    }

  • #2
    In your example, you are storing the "client" in the request. If you want some state to be persisted accross requests, you have to store it in the session. Have a look at http://static.springframework.org/sp...-sessionattrib for more details.

    Comment


    • #3
      If you're passing along the Client as a simple id, and using a GET request (no form submit) then you could embed the client id in the link's url; e.g.,
      Code:
      <a href="allindexes.htm?clientId=${clientId}">all indexes</a><
      then in the allindexes controller something like
      Code:
          @RequestMapping(value="/allindexes.htm", method = RequestMethod.GET)
          public String setupForm(@RequestParam(value = "clientId", required = false)
          final String clientId, final ModelMap model) {
              log.debug("clientId: {}", clientId);
      And the allindexes controller would use the client id to fetch all of that client's stuff from the database.

      But in my test/example I'm putting the @RequestMapping on the class:
      Code:
      @Controller
      @RequestMapping("/get1.zug")
      public final class Get1Controller {
          private final transient Logger log = LoggerFactory.getLogger(getClass());
      
          @RequestMapping(method = RequestMethod.GET)
          public String setupForm(@RequestParam(value = "optParam", required = false)
          final String optParam, final ModelMap model) {
              log.debug("optParam: {}", optParam);
      I can type in either get1.zug?optParam=xyz or get1.zug in my browser's address box. In the first case the log file shows optParam having xyz as its value, in the second it's null.

      Also, if you go this route it's probably best to use the jsp/jstl <c:url> and <c : param> tags rather than manually building the url yourself.

      Comment

      Working...
      X