<set>中的属性无效:&#34; null&#34;在JSP的新ArrayList中

时间:2017-07-12 09:34:02

标签: java jsp arraylist

我有一组对象。我需要迭代它并添加我在JSP页面创建的新ArrayList。但我有一个错误

  

javax.servlet.jsp.JspTagException:&lt; set&gt;中的无效属性:&#34; null&#34;

这是我的代码

<jsp:useBean id="idList" class="java.util.ArrayList"/>
                        <c:forEach items="${car.tape.idSet}" var="item">
                            <c:set target="${idList}" value="${item.id}"/>
                        </c:forEach>
                        <aui:input class="form-check" checked="${idList.contains(1)?'true':''}" inlineLabel="right"
                                   name="name1"
                                   type="checkbox" value="1"
                                   label="Name1"/>
                        <aui:input class="form-check" checked="${idList.contains(2)?'true':''}" inlineLabel="right"
                                   name="name2" type="checkbox" value="2"
                                   label="name2"/>

1 个答案:

答案 0 :(得分:1)

您收到错误

&#34; avax.servlet.jsp.JspTagException:无效的属性:&#34; null&#34; &#34;

因为你没有在

中指定任何属性属性
<c:set /> something like    
<c:set target="${myclass}" property="attrib" value="somevalue"/>

但是在你的情况下,ArrayList是bean,你想在其中设置一些值。请尝试使用下面的示例代码。     包com.example

public class  ArrayListBean {

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

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

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

之后只需修改您的代码,如:

<jsp:useBean id="idList" class="com.example.ArrayListBean"/>
  <c:forEach items="${car.tape.idSet}" var="item">
     <c:set target="${idList}" property="list" value="${item.id}" />
  </c:forEach>
<aui:input class="form-check" checked="${idList.contains(1)?'true':''}" inlineLabel="right" name="name1" type="checkbox" value="1 label="Name1"/>
<aui:input class="form-check" checked="${idList.contains(2)?'true':''}" inlineLabel="right" name="name2" type="checkbox" value="2" label="name2"/>

<jsp:useBean id="idList" class="com.example.ArrayListBean"/>
  <c:forEach items="${car.tape.idSet}" var="item">
     <jsp:setProperty name="idList" property="list" value="${item.id}" />
  </c:forEach>
<aui:input class="form-check" checked="${idList.contains(1)?'true':''}" inlineLabel="right" name="name1" type="checkbox" value="1 label="Name1"/>
<aui:input class="form-check" checked="${idList.contains(2)?'true':''}" inlineLabel="right" name="name2" type="checkbox" value="2" label="name2"/>

<jsp:setProperty>, This works basically the same way, only the name must refer the bean name, not the bean itself.

<jsp:useBean id="bean" class="com.example.Bean" />
<jsp:setProperty name="bean" property="someProperty" value="newvalue" />

这对我来说很好。