Apache Commons BeanUtils,其官方网站上介绍为“ Easy-to-use wrappers around the Java reflection and introspection APIs.”(易于使用的Java反射和自省API包装器),也就是一个可以为你访问Java Bean的属性带来方便的工具了。Struts中的JSP自定义标签便是配合使用BeanUtils来访问Java Bean的属性的,中国自学编程网,www.zxbc.cn 。
参考:
Apache Commons BeanUtils http://commons.apache.org/beanutils/
在JSP中使用Struts“bean”标签来输出bean属性的范例代码:
<%@ page language=\"java\" %>
<jsp:directive.page import=\"java.util.*\"/>
<%@ taglib uri=\"http://struts.apache.org/tags-bean\" prefix=\"bean\" %>
<%!
public static class Bean {
private String id;
private int array;
private List list = new ArrayList();
private Map map = new HashMap();
private Bean nested;
public int getArray() {
return array;
}
public void setArray(int array) {
this.array = array;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public Bean getNested() {
return nested;
}
public void setNested(Bean nested) {
this.nested = nested;
}
public String toString() {
return \"this is the Bean with id: \"+ this.id;
}
}
%>
<%
Bean bean = new Bean();
bean.setId(\"b001\");
bean.setArray(new int{0, 1, 2});
bean.getList().add(\"listItem0\");
bean.getList().add(\"listItem1\");
bean.getList().add(\"listItem2\"); [Page]
bean.getMap().put(\"key1\", \"value1\");
bean.getMap().put(\"key2\", \"value2\");
bean.getMap().put(\"key3\", \"value3\");
bean.setNested(new Bean());
bean.getNested().setId(\"b002\");
pageContext.setAttribute(\"bean\", bean);
%>
<html>
<body>
<table border=\"1\">
<tr><th>type</th><th>name</th><th>property</th><th>value</th></tr>
<tr><td rowspan=\"1\">bean</t