Zany
Zany

Reputation: 328

JSTL - Cast Error String to Long

I'm trying to compare to value using JSTL but I'm bumping this error.

An error occurred while evaluating custom action attribute "value" with value "${item.ruleValues.size}": The "." operator was supplied with an index value of type "java.lang.String" to be applied to a List or array, but that value cannot be converted to an integer. (null)

This are the specific code line -

<c:set var="nElCol" value="0" scope="page"/>
<c:forEach var="elem" items="${item.ruleValues}" varStatus="status">
    <c:set var="size" value="${item.ruleValues.size}" scope="page" />
    <c:set var="nElCol" value="${nElCol + 1}" scope="page"/>

    <c:if test="${size == (nElCol-1)}">
        <TD align="center" width="110">
            <input id='<c:out value="${count}" />' type="text" name="fname" value='<c:out value="${elem}"/>'> 
        </TD>   
        <TD align="center" width="110">
            <img src="/XA-IME-PF/public/img/Plus.jpg" alt="add" width="10" height="10"/>
        </TD>
    </c:if> 
</c:forEach>

The item object is this one:

public class BoElementToPrint implements Serializable{

    private List ruleValues; 

    /**
     * @return
     */
    public List getRuleValues() {
        return ruleValues;
    }

    /**
     * @param list
     */
    public void setRuleValues(List list) {
        ruleValues = list;
    }
}

ruleValues is a List of String.

Upvotes: 0

Views: 2223

Answers (2)

Alan Hay
Alan Hay

Reputation: 23246

In more recent versions of EL I believe you can simply do the following:

${item.ruleValues.size()} 

viz. append () otherwise the EL parser will be looking for a method getSize();

See here:

https://stackoverflow.com/tags/el/info

Invoking non-getter methods

Since EL 2.2, which is maintained as part of Servlet 3.0 / JSP 2.2 (Tomcat 7, Glassfish 3, JBoss AS 6, etc), it's possible to invoke non-getter methods, if necessary with arguments.

e.g.

${bean.find(param.id)} with

public Something find(String id) { return someService.find(id); } will invoke the method with request.getParameter("id") as argument.

Upvotes: 0

Konstantin Yovkov
Konstantin Yovkov

Reputation: 62864

You have to use the JSTL function taglib, in order to evaluate the size of the List. The . operator is only used for referring bean properties or hash map keys.

So, you have to first import the taglib:

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

and then change the size definition:

<c:set var="size" value="${fn:length(item.ruleValues)}" scope="page" />

Upvotes: 2

Related Questions