Announcement Announcement Module
Collapse
No announcement yet.
Accessing model in a JSP using JSp Expressions Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Accessing model in a JSP using JSp Expressions

    I can access my model as returned by a Spring Controller in a jsp

    <c:forEach items="${model.dataitems}" var="dataitem">
    <c:out value="${dataitem.name}"/>
    </c:forEach>

    but not having much luck accessing it using Jsp expression tag instead
    i.e
    <%=model.dataitems.size()%>

    Please, what do i have to do to get it working.

  • #2
    The model items are added to the request as attributes in the case of a jsp view. So you'd need to do request.getAttribute("nameOfModelItem")

    HTH

    Comment


    • #3
      Ok, thanks got it.

      This leads to a follow up question. Should you put everything you want the jsp to access within your Model ( which is automatically added to the HttpServletRequest), or is there some information that you may want to return to the view which you would add directly to the HttpRequest in your controller rather than as part of the model.

      Also, am I right in thinking that whatever you put into the HtppRequest will no longer be there when the user does something on the page and contacts the server in the next iteration. Or does setting the <%@ page session="true"%> on the jsp affect this is some way ?

      Comment


      • #4
        Should you put everything you want the jsp to access within your Model ( which is automatically added to the HttpServletRequest), or is there some information that you may want to return to the view which you would add directly to the HttpRequest in your controller rather than as part of the model.
        The 'Spring good practice' way is to put everything in the model.
        The reason is that then your code is independent of your view technology.
        If you add directly to the request, then you would have to change your code if you ever wanted to change your view technology to something that couldn't access the request.
        Also, am I right in thinking that whatever you put into the HtppRequest will no longer be there when the user does something on the page and contacts the server in the next iteration.
        Yes. You need to persist it in some way yourself (e.g. store it as a session attribute).
        does setting the <%@ page session="true"%> on the jsp affect this is some way ?
        If I understand it correctly, this is essentially just a flag which says whether or not the jsp page is allowed to access the session object. I think it's considered good practice not to, and setting this to false enforces that.

        Comment


        • #5
          If I understand it correctly, this is essentially just a flag which says whether or not the jsp page is allowed to access the session object. I think it's considered good practice not to, and setting this to false enforces that.
          Thanks but why is this good practise ?
          Is it to ensure the JSP only gains access to items it has been specifically given (through the Httprequest).

          I am currently implementing a sort of caching on the servlet engine so that when rows are retrieved from the database simple cvalue obkject representations are stored in the httpsession (per user) so that if additional requests are made such as sorting the data the controller just has to sort it and return the sorted result rather than going back to the database.

          The trouble with this approach is deciding when to remove this data as obviously it uses memory. Ideally we want o remove it when user moves onto another part of the system or maybe just when they move to a different page. But I dont want this new page to have to worry about where the user came from. I thought page=session scope might help me get round this problem in some way.

          Comment


          • #6
            Thanks but why is this good practise ?
            I think it's just to keep your view decoupled from your controller . . . certainly what I said above about putting everything in the model applies equally here. The view shouldn't know or care where data comes from, it's just displaying stuff . . .

            If you want the view to have access to data in the session, just have your controller add it to the model.

            The trouble with this approach is deciding when to remove this data as obviously it uses memory. Ideally we want o remove it when user moves onto another part of the system or maybe just when they move to a different page. But I dont want this new page to have to worry about where the user came from.
            I don't know any easy answers to that one.
            One or two thoughts:
            - try and keep what you store in the session to a minimum
            - It'll be freed up for gc anyway when the session times out
            - If that's not good enough, you could write a 'sessonMaintenanceInterceptor' which looks at all requests before the specific controller gets them, and gets rid of stuff in the session it thinks is no longer necessary. But of course that only works if the client sends another request . . .
            - in some cases it may be better/more efficient to cache stuff in the business tier, using something like ehCache, and you also have some degree of control over how much memory it will use.

            I thought page=session scope might help me get round this problem in some way.
            I don't think so.

            Comment

            Working...
            X