Results 1 to 3 of 3

Thread: Access the model from a JSP view

  1. #1
    Join Date
    Nov 2008
    Posts
    19

    Default 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. #2

    Smile 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.

  3. #3
    Join Date
    Jul 2008
    Posts
    239

    Default

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •