Announcement Announcement Module
Collapse
No announcement yet.
Access the model from a JSP view Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Access the model from a JSP view

    Hello,

    I believe the traditional way to access the model from a JSP view is to use EL. For example, to display the user name, I would use in my JSP:

    Code:
    ${user.name}
    Actually I'd like to use constants from the controller as keys for the model map, for increasing security and consistency while coding. The problem is that if I modify a map key in my controller, Eclipse won't tell me my JSP is wrong, and accessing the JSP with my browser will result in a blank field instead of the expected model object.

    So now I'm using :

    Code:
    <%=((User) request.getAttribute(UserController.MODEL_ATTR_USER)).getName()%>
    which is far more verbose. Is there something better ? And is it worth it ?

  • #2
    Not as far as I know and probably not

    You're far better off with EL.
    Eclipse cannot tell what is expected in the model (it isn't that clever) so cannot show an error.

    In any case the fact that something the jsp expects isn't in the model is not an error as such. You just have to be careful to use the correct model keys.

    You can always test for the objects existence in the jsp with:
    Code:
    <c:choose>
         <c:when test="${not empty user.name}">
               The name is ${user.name}
         </c:when>
         <c:otherwise>
               Do something else
         </c:otherwise>
    </c:choose>
    Using the scriptlet approach is indeed verbose.
    Believe me you'll regret using them later as it can be a PITA to maintain.

    Comment


    • #3
      The problem, the OP said, is not whether to use JSTL, but that the field is EMPTY in the HTML output of the JSP - i have the same problem, which I cannot resolve.

      Comment

      Working...
      X