0

In a Spring Controller method, I want to enrich the Model elements from what is posted from the jsp file.

Here is the start of the code function. the model is void everytime the function is entered. Any Hint about that?

@RequestMapping(value = Uris.IMPORTADDRBOOK)
public String mainImportController(HttpServletRequest request, HttpServletResponse response, Model model,
        @RequestParam(value = "chosenSP", required = false) String bookToImportName,
        @RequestParam(value = "catMode", required = false) String catMode,
        @RequestParam(value = "transition", required = false) ImportControllerTransitions transition) {

    logger.debug(String.format("controller import. Etat entrée : %s, transition demandée : %s, chosen SP : %s, catmode : %s",
            model.asMap().get("importState"), transition, bookToImportName, catMode));


    logger.debug(String.format("Modele Entree  Import Controller:"));
    for (String attribute : model.asMap().keySet())
        logger.debug(String.format("%s : %s", attribute, model.asMap().get(attribute)));


    if (!model.containsAttribute("importState"))
        model.addAttribute("importState", ImportControllerState.INITIAL);

    switch ((ImportControllerState) model.asMap().get("importState")) {
    case INITIAL:
        if (transition == ImportControllerTransitions.CONNECTORCHOICE) {
            model.addAttribute("importState", ImportControllerState.CONNECTOR);
            if (bookToImportName != null)
                model.addAttribute("chosenSP", DynAddrBookTypes.valueOf(bookToImportName));
        }
        break;

    default:
        break;

    }

    logger.debug(String.format("Modele sortie Import Controller:"));
    for (String attribute : model.asMap().keySet())
        logger.debug(String.format("%s : %s", attribute, model.asMap().get(attribute)));
    return Uris.IMPORTADDRBOOK;
}

The JSP can read correctly the model attribute but when I come back into the controller, model is empty again... Thanks in advance,

Yves Nicolas
  • 6,901
  • 7
  • 25
  • 40
  • What do you mean by come back into the controller? – Bart Apr 02 '14 at 19:57
  • in the JSP, there are several forms all doing a POST to the same URL which is "@requestmapped" to the controller function, generating successive calls to the same controller function. – Yves Nicolas Apr 02 '14 at 20:26
  • Do you mean you need the behavior of [session attributes](http://docs.spring.io/spring/docs/3.1.x/javadoc-api/org/springframework/web/bind/annotation/SessionAttributes.html)? – Bart Apr 02 '14 at 20:29
  • Following Bart's comment and looking at http://stackoverflow.com/questions/5834558/spring-3-mvc-move-share-model-from-controler-to-another-controler?rq=1 saying that MVC Spring is stateless, I guess my stuff can not work if I do not manage to keep what I need stored in the session as a session attribute... – Yves Nicolas Apr 02 '14 at 20:30
  • Bart, I definitely need the session Attributes. Thanks for your help. – Yves Nicolas Apr 02 '14 at 20:32

1 Answers1

0

Following up on Bart's comments above and answer from Donal Boyle in this related SO question, @SessionAttributes is the right way to keep the values of the model objects despite the fact the Spring MVC server side is stateless.

The code in my question works just fine provided the class declaration include the @SessionAttributes annotation as below :

@Controller
@SessionAttributes({ "importState", "chosenSP", "catMode" })
public class ImportContactController {
Community
  • 1
  • 1
Yves Nicolas
  • 6,901
  • 7
  • 25
  • 40
  • One thing I like to clarify a bit. Spring MVC is not stateless perse. **HTTP is stateless.**. – Bart Apr 04 '14 at 09:38