我有一组对象。我需要迭代它并添加我在 JSP 页面创建的新 ArrayList。但我有一个错误
javax.servlet.jsp.JspTagException: Invalid property in <set>: "null"
这是我的代码
<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"/>
最佳答案
您收到错误
“avax.servlet.jsp.JspTagException:无效属性:“null””
因为您没有指定任何property属性
<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" />
它对我来说工作得很好。
关于java - <set> 中的属性无效 : "null" in new ArrayList at JSP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45053884/