Announcement Announcement Module
Collapse
No announcement yet.
Selecting from an array. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Selecting from an array.

    Given an integer value I want to be able to map it to a string, in Java I'd do something like:

    String[] foo= {"first","second","third"};

    System.out.println(foo[index]);

    In JSP the best way I've found is to use "choose" but that's a really clunky solution: is there a better way either using JSP/JSTL or using a Spring taglib?

    Edward

  • #2
    you could use spring message tag and message ressource, or pass your foo object with your model in order to access it.

    Comment


    • #3
      Originally posted by olivier
      you could use spring message tag and message ressource, or pass your foo object with your model in order to access it.
      Yes that#s the easy bit: the bit I can't work out is how do I do the equivalent of: myStringArray[object.index] ?

      Comment


      • #4
        This is pretty clunky, and I'd like to think there's a better way; but you could do something like
        Code:
        <c&#58;foreach var="arrayItem" items="$&#123;myArrayObject&#125;" varStatus="status">
          <c&#58;if test=$&#123;myIndex == status.index&#125;">$&#123;arrayItem&#125;</c&#58;if>
        </c&#58;foreach>

        Comment


        • #5
          with spring message, you can use in your jsp :

          <spring:message code="myStringArray.${index}"/>

          where index is your number, and with messages like :

          myStringArray.1 = first
          myStringArray.2 = second
          myStringArray.3 = third

          With the model, if you pass a model to the JSP with a List called mapping,

          like List myStringArray = new ArrayList()
          myStringArray.add("zero");
          myStringArray.add("first");
          myStringArray.add("second");

          you can retrieve it in the jsp with
          <c:out value="${mapping[index]}"/>

          where ${index} is your index.

          Comment


          • #6
            That's...

            perfect, thankyou.

            Comment

            Working...
            X