简体   繁体   中英

How to put an object into JSP variable?

I've got collection of objects I want use in my custom tag.But I can get the element of collection only once,because it iterates to next after getting. So I decided to define a variable. But it doesn't work.

<jsp:useBean id="rw" scope="request" class=
"by.epam.web.libruary.transferobject.AvailableBookSet">
    <c:set var="element"
        value="${rw.element}"
        scope="page"/>
    <mytag:bodyattr num="${rw.size}">
        <form name="orderBook" method="POST" action="${controllerpage}">
            <input type="submit" name="submit" 
              value="${rw.element.shortinfo}">
        </form>
    </mytag:bodyattr>
</jsp:useBean>

Here is the code of collection itself:

public class AvailableBookSet extends java.util.HashSet {
    private java.util.Iterator it;

    @SuppressWarnings("unchecked")
    public AvailableBookSet(){

            try {
                    //Retrieving info from DataBase (100% working code)

            } catch (CannotTakeConnectionException e) {
            }
    }

    public String getSize(){
            it = this.iterator();
            return Integer.toString(this.size());
    }

    public Object getElement(){
            return it.next();
    }

}

And here is my exception:

org.apache.jasper.JasperException: An exception occurred processing JSP page /jsp/available_books.jsp at line 51

48:     
49:     <jsp:useBean id="rw" scope="request" class=
50:     "by.epam.web.libruary.transferobject.AvailableBookSet">
51:         <c:set var="element"
52:             value="${rw.element}"
53:             scope="page"/>
54:         <mytag:bodyattr num="${rw.size}">

Will be very grateful for your help.

Unless you've omitted some key code in your post, it is null when you are invoking the .getElement() method. The call to .next() will then throw a NullPointerException .

Try reversing the order of these two lines:

<c:set var="element" value="${rw.element}" scope="page"/>
<mytag:bodyattr num="${rw.size}">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM