Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By gimbal2

Thread: Accessing HashMap with JSTL

  1. #1
    anjibman is offline Member
    Join Date
    Dec 2010
    Posts
    48
    Rep Power
    0

    Default Accessing HashMap with JSTL

    Hi All,

    I have a HashMap returned from the server. There are two conditions
    1. HashMap returned with only one set of key value pair or
    2. HashMap with multiple set of data key value pairs.

    Now in UI I have to display either text box or drop down box depending upon the size of map for that I am using length method

    Java Code:
    <c:choose>
    <c:when test="${fn:length(myDto.mayMapInDto) eq 1}">
    display text box
    </c:when>
    <c:otherwise>
    display drop box
    </c:otherwise>
    </c:choose>
    I can display drop box by looping but not sure how I can get only one element for text box. Tricky is I can't use key value to access since UI don't know what key will be returned.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Accessing HashMap with JSTL

    I can only advise an alternative: add methods to 'myDto' which return the relevant information to render the view with.

    Java Code:
    public boolean isSingleValueAvailable(){
      return myMap.size() == 1; // so also returns true when there are NO values
    }
    
    public String getSingleValue(){
       if(myMap.size() != 1){
         throw new IllegalStateException("getSingleValue() called when there is not a singular result!"); // a sanity check like this helps to catch programming mistakes
       }
    
       return myMap.values().iterator.next();
    }
    My rule of thumb is: cleverness goes into the bean, not into the view. The view needs to be nice and dumb.
    Last edited by gimbal2; 08-18-2014 at 06:46 PM.
    Tolls likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. JSTL accessing properties on an object that extends ArrayList
    By chester_perry in forum Advanced Java
    Replies: 6
    Last Post: 07-29-2013, 06:54 PM
  2. final HashMap hm=new HashMap();
    By sangramkeshari.jena in forum New To Java
    Replies: 4
    Last Post: 07-21-2011, 10:44 PM
  3. regarding jstl and jsp
    By bhanusri in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 06-09-2011, 10:26 AM
  4. JSTL and JSP
    By mohinicharankar in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-23-2011, 08:26 AM
  5. Replies: 7
    Last Post: 12-08-2009, 08:17 PM

Posting Permissions

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