专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »Java » struts:Struts中Beanutils的用法范例 »正文

struts:Struts中Beanutils的用法范例

来源: 发布时间:星期三, 2008年9月10日 浏览:36次 评论:0

Apache Commons BeanUtils,其官方网站上介绍为“ Easy-to-use wrappers around the Java reflection and introspection APIs.”(易于使用的Java反射和自省API包装器),也就是一个可以为你访问Java Bean的属性带来方便的工具了。Struts中的JSP自定义标签便是配合使用BeanUtils来访问Java Bean的属性的,,www. 。

参考:
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</td>
<td>bean</td><td></td><td><bean:write name=\"bean\"/></td></tr>
<tr><td rowspan=\"5\">property</td>
<td>bean</td><td>id</td><td><bean:write name=\"bean\" property=\"id\"/></td></tr>
<tr><td>bean</td><td>array</td><td><bean:write name=\"bean\" property=\"array\"/></td></tr>
<tr><td>bean</td><td>list</td><td><bean:write name=\"bean\" property=\"list\"/></td></tr>
<tr><td>bean</td><td>map</td><td><bean:write name=\"bean\" property=\"map\"/></td></tr>
<tr><td>bean</td><td>nested</td><td><bean:write name=\"bean\" property=\"nested\"/></td></tr>
<tr><td rowspan=\"5\">nested(simple) property</td>
<td>bean</td><td>nested.id</td><td><bean:write name=\"bean\" property=\"nested.id\"/></td></tr>
<tr><td>bean</td><td>nested.array</td><td><bean:write name=\"bean\" property=\"nested.array\"/></td></tr>
<tr><td>bean</td><td>nested.list</td><td><bean:write name=\"bean\" property=\"nested.list\"/></td></tr>
<tr><td>bean</td><td>nested.map</td><td><bean:write name=\"bean\" property=\"nested.map\"/></td></tr>
<tr><td>bean</td><td>nested.nested</td><td><bean:write name=\"bean\" property=\"nested.nested\"/></td></tr>

相关文章

读者评论

  • 共0条 分0页

发表评论

  • 昵称:
  • 内容: