Announcement Announcement Module
Collapse
No announcement yet.
How to persist model data in session Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to persist model data in session

    I have some data (a search result list) which I put in the model to help render the next view. However, the same data is needed by the controller the next view submit to. How do I persist data across controllers? Should I put the data in session? If so, doesn't that go against the spirit of Spring? (data needed for rendering should be put in modelAndView)

    thanks!

  • #2
    It would be better to *not* put it in the session, and do database caching, but if you absolutely must do this, I would provide an interface:
    Code:
      public interface ResultsLoader {
        int execute(final HttpServletRequest request, final Query query);
        List getResults(final HttpServletRequest request, final int start, final int end);
      }
    and use this interface in your controller. You could then have one (stateless) implementation which is backed by the session, another (stateless) backed by the DAO etc.

    The only nasty thing is that you need to pass in the request .

    Comment


    • #3
      Is persisting information in session discouraged in Spring? I can see that something like serach results can be better cached in someway, but what about other information you need to persist across controllers?

      My real issue is with when that information also takes part in the rendering of the next view. Now in the controller you have to put the data in ModelAndView and persist it in Session at the same time. Seems redundant.

      Comment


      • #4
        Spring does not dictate some best practise. Even if its 99% the best way, you still have that 1% why you should use some other method. So if you've fully considered and feels that there is a need to do so and that its a nicest thing you can come up with, then go ahead. There is no one size fit all solution and thats one of the things spring recognise(I'm not saying every solution is ok as long as it works). If Spring is really so against persisting data in your session, they wouldn't give us WebUtils.setSessionAttribute().

        In your case you have to consider what you feel is the better way. Normally, I would recommend doing things the simpler way, usually just reloading the database(since database caching make it quite efficient anyway).

        If you are just thinking about performance, forget it as I reckon reloading from DB is faster. And even if not, its not worth sacrificing a nicer solution for a little improvement that is not even proven.

        But if your database is expected to change really fast and you need to keep your records as it is loaded, then its perfectly alright to store it in session, perfectly ligitimate.

        Comment


        • #5
          ModelAndView is only used for rendering.

          If I were you I would pass in the start and end index to the controller, and pass these straight through to the DAO. Obviously you will need to check the sanity of those values, but.... If you want a bit cleaner then pass in "nextPage" and "previousPage" and store the start and end end in the session.

          Keeping things in the session, particularly huge lists is the quickest way to bring your app server down

          Comment


          • #6
            I agree with what you guys are saying. However, I am really just using the search result scenerio as an example. What I was pondering about is if you need to perserve some information X in the session and the same information is used to render the next view, do you:

            Save X in the session and pass it to ModelAndView at the same time?

            It just seems a bit redundant to me and I thought there might be a better way to do it.

            Thanks!

            Comment


            • #7
              Persisting the Model (command object)

              Try this after a post:

              protected ModelAndView onSubmit(HttpServletRequest request,
              HttpServletResponse response,
              Object command, BindException errors) throws Exception {
              ....
              return(showForm(request, errors, "nextViewName"));
              }

              or, try this after a get:

              protected Object formBackingObject(HttpServletRequest request) throws Exception {
              if ((result = request.getSession().getAttribute(getFormSessionAt tributeName())) == null)
              result = super.formBackingObject(request);
              return(result);
              }

              Hope that helps :wink:

              Comment


              • #8
                You can always persist your form on the session. AbstractFormController has a property "sessionForm" which determines whether the form will be "persisted" in the session of the request. By default it is false (i.e. request).

                Comment


                • #9
                  Originally posted by thenakedsingularity
                  I agree with what you guys are saying. However, I am really just using the search result scenerio as an example. What I was pondering about is if you need to perserve some information X in the session and the same information is used to render the next view, do you:

                  Save X in the session and pass it to ModelAndView at the same time?

                  It just seems a bit redundant to me and I thought there might be a better way to do it.
                  If X comes from DB, I'd say just leave it to the second level cache. Keeping it in the session bloats the session, which leads all kinds of pain. Besides, you'd be taking up memory just for a _possible_ submission in the future.

                  If X is some session specific converstaional state, then obviously you have to keep it in the session.

                  Comment

                  Working...
                  X