How to add values to an ArrayList referenced by jsp:useBean?

17,015

That isn't directly possible. There are the <c:set> and <jsp:setProperty> tags which allows you to set properties in a fullworthy javabean through a setter method. However, the List interface doesn't have a setter, just an add() method.

A workaround would be to wrap the list in a real javabean like so:

public class ListBean {

    private List<Object> list = new ArrayList<Object>();

    public void setChild(Object object) {
        list.add(object);
    }

    public List<Object> getList() {
        return list;
    }
}

and set it by

<jsp:useBean id="listBean" class="com.example.ListBean" scope="request" />
<jsp:setProperty name="listBean" property="child" value="foo" />
<jsp:setProperty name="listBean" property="child" value="bar" />
<jsp:setProperty name="listBean" property="child" value="waa" />

But that makes little sense. How to solve it rightly depends on the sole functional requirement. If you want to preserve some List upon a GET request, then you should be using a preprocessing servlet. Create a servlet which does the following in doGet() method:

List<String> list = Arrays.asList("foo", "bar", "waa");
request.setAttribute("list", list);
request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);

When you invoke the servlet by its URL, then the list is in the forwarded JSP available by

${list}

without the need for old fashioned <jsp:useBean> tags. In a servlet you've all freedom to write Java code the usual way. This way you can use JSP for pure presentation only without the need to gobble/hack some preprocessing logic by <jsp:useBean> tags.

See also:

Share:
17,015
Jonathan Hult
Author by

Jonathan Hult

Jonathan is an experienced IT consultant with 9+ years of experience. Jonathan has a deep understanding of the Oracle Fusion Middleware stack, hands-on experience and years of experience in design, development, support of large-scale projects. Jonathan started his IT journey in his teens by fixing computers for family and friends. During high school, he served as Technical Director for a financial advisor's office. Jonathan continued his journey by attending Iowa State University where he graduated in three years with a B.S. in Management Information Systems. After college, Jonathan started working with Oracle WebCenter Content. Jonathan is globally recognized within the WebCenter Content community. He has worked with clients around the world to help them solve their business problems. Jonathan has worked with clients in a wide variety of industries (finance, tech, non-profit, transportation, education, retail), worked on large and small projects (from small businesses to Fortune 100 companies and U.S. Federal Government agencies) and in varying capacities (from developer to project lead). After coming to Mythics, Jonathan progressed from a technical consultant to a Technical Director. Today, Jonathan’s primary goal is to manage technical innovations which internally benefit Mythics as well as our clients. Jonathan maintains a personal and technical blog at JonathanHult.com. He also shares his Oracle expertise on the Mythics blog. He is very active on the OTN forums where he enjoys mentoring others. Jonathan regularly presents at COLLABORATE, tech days and other events. Outside of his daily work, Jonathan enjoys disc golf, bowling, snow skiing, board and video games, watching movies/TV and rock music/concerts. He can play multiple instruments including piano, trumpet, and guitar. He loves Jesus and worships Him at The Austin Stone Community Church.

Updated on June 04, 2022

Comments

  • Jonathan Hult
    Jonathan Hult almost 2 years

    In JSP/JSTL, how can I set values for a usebean of class="java.util.ArrayList".

    If I try using c:set property or value, I get the following error: javax.servlet.jsp.JspTagException: Invalid property in : "null"