问题描述
在JSP / JSTL,我怎么能对类的useBean的设定值=java.util.ArrayList中。
如果我尝试使用C:设置属性或值,我得到以下错误:
javax.servlet.jsp.JspTagException:空
这是不能直接成为可能。还有的 c为C:集>
和< JSP:的setProperty>
标签,它允许你在设置属性通过一个setter方法fullworthy的javabean。但是,列表
接口没有一个二传手,只是一个添加()
方法。
A 解决方法将包装清单在一个真正的javabean像这样:
公共类ListBean { 私人列表<对象>名单=新的ArrayList<对象>(); 公共无效setChild(Object对象){
list.add(对象);
} 公开名单<对象>的GetList(){
返回列表;
}
}
和
设置 < JSP:useBean的ID =listBean级=com.example.ListBean范围=请求/>
< JSP:的setProperty NAME =listBean属性=孩子VALUE =富/>
< JSP:的setProperty NAME =listBean属性=孩子VALUE =酒吧/>
< JSP:的setProperty NAME =listBean属性=孩子VALUE =WAA/>
但是,这没有什么意义。如何解决它正确地依赖于单一功能需求。如果您想preserve一些列表
在一个GET请求,那么你就应该使用preprocessing的servlet。创建一个servlet,它确实在的doGet)以下(
方法:
列表<串GT;清单= Arrays.asList(富,酒吧,WAA);
了request.setAttribute(清单,清单);
的request.getRequestDispatcher(/ WEB-INF / page.jsp)向前(请求,响应)。
当您通过URL调用Servlet,则列表在转发的JSP可以通过
$ {}列表
无需要老式 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" That isn't directly possible. There are the A workaround would be to wrap the list in a real javabean like so: and set it by But that makes little sense. How to solve it rightly depends on the sole functional requirement. If you want to preserve some When you invoke the servlet by its URL, then the list is in the forwarded JSP available by without the need for old fashioned 这篇关于如何添加值由JSP引用一个ArrayList:useBean的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!< JSP:useBean的>
标记。在一个servlet你都自由编写Java code中的常用方法。 useBean的> 标记:JSP;通过&LT这样,您就可以使用JSP纯presentation只,无需狼吞虎咽/破解一些preprocessing逻辑p>
参见:
<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.public class ListBean {
private List<Object> list = new ArrayList<Object>();
public void setChild(Object object) {
list.add(object);
}
public List<Object> getList() {
return list;
}
}
<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" />
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);
${list}
<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: