14 Ответы Последний ответ: 12.06.2008 21:16, автор: 843840

    How do you create an array using JSTL / EL?

    843838
      Is it possible to create an array using EL in JSTL tags? For example, I would like to create something like this, only using JSTL and EL:
      String[] myArray = {"one", "two", "three"};
      I have read through the specification for both 1.0 and 1.1, and I could not find anything that mentioned it. Right now, I have to be able to do something using 1.0, but if this is only possible using 1.1, then I am still interested in how you do it. We should be upgrading our container to one that can use 1.1 sometime later this year.

      I have tried the following, without success:
      <c:set var="myArray" value="'one', 'two', 'three'" />
      <c:set var="myArray" value="['one', 'two', 'three']" />
      <c:set var="myArray" value="{'one', 'two', 'three'}" />
      If anyone knows if this can be done and how to do it, I would greatly appreciate your ideas.

      Thanks in advance.
        • 1. Re: How do you create an array using JSTL / EL?
          794117
          Nope, can't be done.
          EL is not a programming language in its own right. It is not there to replace java code 100% - just to remove scriptlets from JSP pages.

          Often if you can't do something in JSP, you have to ask yourself - should I be doing this in JSP, or does it belong in a bean/servlet?

          In this case a simple bean to declare the options as an attribute will do it easily. EL can then access that bean.
          • 2. Re: How do you create an array using JSTL / EL?
            843838
            Thanks for the info. I didn't think it could be done, but if there was some trick out there, I wanted to know about it.
            • 3. Re: How do you create an array using JSTL / EL?
              843838
              Is the following right? There is an asymmetry with EL/JSTL between reading and writing arrays: you can read arrays, but cannot edit them. However, the symmetry does exist with simple variables as you can set them with the tag <c:set ...>

              Can anyone give an overview on how to manage arrays with EL/JSTL and without scriptlet?

              I saw the two following ways to do it by using scriptlets:
              <jsp:useBean id="colors" class="java.util.ArrayList"  scope="session"> 
                <% 
                   colors.add("red");
                   colors.add("green");
                %>
              </jsp:useBean>
              and:
              <% pageContext.setAttribute("colors", new String[] {"red","green"} , pageContext.SESSION_SCOPE);   %>
              In both cases, you access the array-values with the EL-expression:
              ${colors[0]}
              • 4. Re: How do you create an array using JSTL / EL?
                843838
                I haven't officially read this anywhere, but I have been taught that JSP's main purpose is front end and that it is best practice to use a JSP to only display data , and not modify data or add new data.

                One would reserve adding new data, modifying and deleting data to the middle layer via JavaBeans , Java Classes and Data Access objects.

                I think the JSTL and EL are limited in their abilities to allow one to perform CRUD operations (except for R - read) inside a JSP page on purpose.

                By having this limitation, one is forced to implement the core logic in the middle layer, and only reserve the display part to the JSPs.

                So, where ever it's possible to write the main logic inside the middle layer it should be avoided in the JSPs.
                • 5. Re: How do you create an array using JSTL / EL?
                  843838
                  <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
                  <c:set var="myArray" value="${fn:split('one,two,three',',')}" />
                  • 6. Re: How do you create an array using JSTL / EL?
                    843840
                    You can work around this issue by using
                    <c:forTokens>
                    if your array has a limited length, short enough to declare it in
                    <c:set var="names">Joe,Cindy,Andy,Candy,Mudy</c:set>
                    • 7. Re: How do you create an array using JSTL / EL?
                      843840
                      MicroNova YUZU JSP Tag Library (https://micronova-yuzu.dev.java.net) supports lists/arrays (create/insert/remove/replace) in JSTL-style:
                      <m:set var="list" className="java.util.ArrayList">
                        <%-- append "a", "b", "c", "d" --%>
                        <m:set property="*" value="a"/>
                        <m:set property="*" value="b"/>
                        <m:set property="*" value="c"/>
                        <m:set property="*" value="d"/>
                        <%-- remove element at 1 ("b") --%>
                        <m:set property="*1"/>
                        <%-- insert "e" at position 1 --%>
                        <m:set property="*1" value="e"/>
                        <%-- insert "f" before the last two elements --%>
                        <m:set property="*-2" value="f"/>
                        <%-- replace element at index 1 with "E" --%>
                        <m:set property="1" value="E"/>
                        <%-- remove the last element --%>
                        <m:set property="*"/>
                      </m:set>
                      
                      <%-- this outputs "[a, E, f, c]" --%>
                      
                      <m:out value="${list}"/>
                      Hope this helps.
                      • 8. Re: How do you create an array using JSTL / EL?
                        843840
                        This seemed to work in my case

                        Say for example you would like to loop through a list of items and each time you loop you want to display a value from your array.

                        this will require the help of c:set var and c:forTokens:
                        <c:set var="items" value="1,2,3" />
                        <c:set var="names" value="Bob,Henry,Joe,Jane,Jimmy" />
                        <c:forTokens items="${items}" delims="," varStatus="status">
                             <c:forTokens items="${names}" delims="," var="name" begin="${status.index}" end="${status.index}">
                                   <p>${name}</p>
                                </c:forTokens>
                        </c:forTokens> 
                        • 9. Re: How do you create an array using JSTL / EL?
                          843840
                          Business logic should not be placed in a JSP. JSP should be used for display only.
                          • 10. Re: How do you create an array using JSTL / EL?
                            843840
                            not going to disagree with you there, other than that sometimes you only have access to the JSP. In which case work-around solutions need to be considered.

                            Edited by: dsdesign on May 5, 2008 7:29 AM
                            • 11. Re: How do you create an array using JSTL / EL?
                              794117
                              In this sort of case IF I was only limited to JSP I would resort to scriptlet code to define the arrays properly, and then just set them as attributes in scope.

                              I prefer scriptlet code declaration of arrays to that parsing comma separated data strings.
                              • 12. Re: How do you create an array using JSTL / EL?
                                843840
                                As I am not a native java programmer I was actually unaware that scriptlets were a possibility. For that I thank you :) If you find the time I would appreciate and example. thanks.
                                • 13. Re: How do you create an array using JSTL / EL?
                                  794117
                                  The first post had most of it actually.
                                  Just missing the <% scriptlet tags %>
                                  <%
                                    String[] names = {"Thomas", "Richard", "Harold"};
                                    pageContext.setAttribute("names", names);
                                  %>
                                  
                                  The second name in the list is: ${names[1]}
                                  
                                  <table>
                                    <c:forEach var="name" items="${names}">
                                      <tr><td><c:out value="${name}"/></td></tr>
                                    </c:forEach>
                                  </table>
                                  Cheers,
                                  evnafets
                                  • 14. Re: How do you create an array using JSTL / EL?
                                    843840
                                    cheers evnafets, good to know :)