English 中文(简体)
Add values to arraylist use JSTL
原标题:
  • 时间:2009-12-02 18:59:42
  •  标签:
  • java
  • jstl

is it possible to add values to an ArrayList instead of using a HashMap

something like:

<jsp:useBean id="animalList" class="java.util.ArrayList" />

<c:set target="${animalList}" value="Sylvester"/>

<c:set target="${animalList}" value="Goofy"/>

<c:set target="${animalList}" value="Mickey"/>

<c:forEach items="${animalList}" var="animal">

${animal}<br>

</c:forEach>    

now getting the error:

javax.servlet.jsp.JspTagException: Invalid property in &lt;set&gt;:  "null"

thx

最佳回答

JSTL is not designed to do this kind of stuff. This really belongs in the business logic which is (in)directly to be controlled by a servlet class.

Create a servlet which does like:

protected void doGet(HttpServletRequest request, HttpServletResponse response) {
    List<String> animals = new ArrayList<String>();
    animals.add("Sylvester");
    animals.add("Goofy");
    animals.add("Mickey");
    request.setAttribute("animals", animals);
    request.getRequestDispatcher("/WEB-INF/animals.jsp").forward(request, response);
}

Map this on an url-pattern of /animals.

Now create a JSP file in /WEB-INF/animals.jsp (place it in WEB-INF to prevent direct access):

<c:forEach items="${animals}" var="animal">
    ${animal}<br>
</c:forEach>

No need for jsp:useBean as servlet has already set it.

Now call the servlet+JSP by http://example.com/context/animals.

问题回答

To do add() to a List or others methods from Map, Set, etc... You have to use a unusable variable.

<jsp:useBean id="list" class="java.util.ArrayList"/>
<c:set var="noUse" value="${list.add( YourThing )}"/>
<c:out value="${list}"/>

The above code is not working.

Following are the lines of code that has to be placed in file animals.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<c:forEach var="animal" items="${animals}">
   <c:set var="animalName" value="${animal}"/>
   <c:out value="${animalName}"/>
</c:forEach>




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签