本人共测试了<bean : write>,以及其中的property,format等,<logic : empty> ,<logic : notEmpty>,<logic : present>,<logic : notPresent>,<logic : iterate>
在这里我就只贴出几个关键的代码,如有人需要全部代码,可联系本人。
1.struts-config.xml:
<?
xml version="1.0" encoding="ISO-8859-1"
?>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<!
DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
struts-config
>
<
action-mappings
>
<
action
path
="/beanwrite"
type
="com.codedestiny.struts.BeanWriteTestAction"
scope
="request"
>
<
forward
name
="success"
path
="/beanwrite.jsp"
></
forward
>
</
action
>
<
action
path
="/emptypresent"
type
="com.codedestiny.struts.EmptyPresentTestAction"
scope
="request"
>
<
forward
name
="success"
path
="/emptypresent.jsp"
></
forward
>
</
action
>
<
action
path
="/iterate"
type
="com.codedestiny.struts.IterateTestAction"
scope
="request"
>
<
forward
name
="success"
path
="/iterate.jsp"
></
forward
>
</
action
>
</
action-mappings
>
<
message-resources
parameter
="MessageResources"
/>
</
struts-config
>
2.BeanWriteTestAction.java
package
com.codedestiny.struts;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
java.util.Date;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.apache.struts.action.Action;
import
org.apache.struts.action.ActionForm;
import
org.apache.struts.action.ActionForward;
import
org.apache.struts.action.ActionMapping;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
BeanWriteTestAction
extends
Action
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception ...{
request.setAttribute("hello", "Hello world");
request.setAttribute("cd", "<font color='red' size='10'>成都欢迎您</font>");
request.setAttribute("today", new Date());
request.setAttribute("number", 1234456789.987654321);
Group relatedGroup = new Group();
relatedGroup.setName("西华大学");
User u = new User();
u.setUsername("CodeDestiny");
u.setAge(21);
u.setRelatedGroup(relatedGroup);
request.setAttribute("user", u);
return mapping.findForward("success");
}
}
相应的beanwrite.jsp:
<%
...
@ page language="java" import="java.util.*" pageEncoding="GB18030"
%>
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
<%
...
@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"
%>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
>
<
html
>
<
head
>
<
title
>
BeanWrite
</
title
>
</
head
>
<
body
>
<
h1
>
Bean Write 测试
</
h1
>
<
li
>
普通字符串
</
li
><
br
>
hello(jsp):
<%
=
request.getAttribute(
"
hello
"
)
%>
<
br
>
hello(tag):
<
bean:write
name
="hello"
/><
br
>
<
hr
>
<
li
>
html文本
</
li
><
br
>
cd(defualt):
<
bean:write
name
="cd"
/><
br
>
cd(filter=true):
<
bean:write
name
="cd"
filter
="true"
/><
br
>
cd(filter=false):
<
bean:write
name
="cd"
filter
="false"
/><
br
>
<
hr
>
<
li
>
日期格式化
</
li
><
br
>
today(default):
<
bean:write
name
="today"
/><
br
>
today(format):
<
bean:write
name
="today"
format
="yyyy-MM-dd HH:mm:ss"
/>
<
hr
>
<
li
>
数字格式化
</
li
><
br
>
number(default):
<
bean:write
name
="number"
/><
br
>
number(format:###,####.00):
<
bean:write
name
="number"
format
="###,###.00"
/><
br
>
<
hr
>
<
li
>
结构
</
li
><
br
>
name:
<
bean:write
name
="user"
property
="username"
/><
br
>
age:
<
bean:write
name
="user"
property
="age"
/><
br
>
relatedGroup:
<
bean:write
name
="user"
property
="relatedGroup.name"
/><
br
>
</
body
>
</
html
>
3.EmptyPresentTestAction.java:
package
com.codedestiny.struts;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
java.util.ArrayList;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
org.apache.struts.action.Action;
import
org.apache.struts.action.ActionForm;
import
org.apache.struts.action.ActionForward;
import
org.apache.struts.action.ActionMapping;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
EmptyPresentTestAction
extends
Action
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception ...{
request.setAttribute("attr2", "");
request.setAttribute("attr3", new ArrayList());
return mapping.findForward("success");
}
}
相应的emptypresent.jsp:
<%
...
@ page language="java" import="java.util.*" pageEncoding="GB18030"
%>
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
<%
...
@ taglib prefix="logic" uri="http://struts.apache.org/tags-logic"
%>
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
>
<
html
>
<
head
>
</
head
>
<
body
>
<
h1
>
测试 logic:empty,notEmpty 以及 logic:present,notPresent
</
h1
>
<
hr
>
<
logic:empty
name
="attr1"
>
attr1为空
</
logic:empty
>
<
logic:notEmpty
name
="attr1"
>
attr1不为空
</
logic:notEmpty
><
br
>
<
logic:present
name
="attr1"
>
attr1存在
</
logic:present
>
<
logic:notPresent
name
="attr1"
>
attr1不存在
</
logic:notPresent
><
br
>
<
hr
>
<
logic:empty
name
="attr2"
>
attr2为空
</
logic:empty
>
<
logic:notEmpty
name
="attr2"
>
attr2不为空
</
logic:notEmpty
><
br
>
<
logic:present
name
="attr2"
>
attr2存在
</
logic:present
>
<
logic:notPresent
name
="attr2"
>
attr2不存在
</
logic:notPresent
><
br
>
<
hr
>
<
logic:empty
name
="attr3"
>
attr3为空
</
logic:empty
>
<
logic:notEmpty
name
="attr3"
>
attr3不为空
</
logic:notEmpty
><
br
>
<
logic:present
name
="attr3"
>
attr3存在
</
logic:present
>
<
logic:notPresent
name
="attr3"
>
attr3不存在
</
logic:notPresent
><
br
>
</
body
>
</
html
>
4.IterateTestAction.java:
package
com.codedestiny.struts;
import
java.util.ArrayList;
import
java.util.List;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
org.apache.struts.action.Action;
import
org.apache.struts.action.ActionForm;
import
org.apache.struts.action.ActionForward;
import
org.apache.struts.action.ActionMapping;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
IterateTestAction
extends
Action
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception ...{
List<User> users = new ArrayList<User>();
Group relatedGroup = new Group();
relatedGroup.setName("Xihua Univercity");
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for(int i=1; i<10; i++) ...{
User u = new User();
u.setUsername("user" + i);
u.setAge(20 + i);
u.setRelatedGroup(relatedGroup);
users.add(u);
}
request.setAttribute("users", users);
return mapping.findForward("success");
}
}
相应的iterate.jsp:
<%
...
@ page language="java" import="java.util.*" pageEncoding="GB18030"
%>
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
<%
...
@ page import="com.codedestiny.struts.*"
%>
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
<%
...
@ page import="java.util.*"
%>
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
<%
...
@ taglib prefix="logic" uri="http://struts.apache.org/tags-logic"
%>
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
<%
...
@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"
%>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
>
<
html
>
<
head
>
</
head
>
<
body
>
<
h1
>
测试logic:iterate
</
h1
>
<
hr
>
常规:
<
br
>
<
table
border
="1"
>
<
tr
><
td
>
姓名
</
td
><
td
>
年龄
</
td
><
td
>
所属组
</
td
></
tr
>
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
<%
...
List<User> users = (ArrayList<User>)request.getAttribute("users");
if(users == null || users.size() == 0) {
out.println("<tr><td colspan=3>没有任何相关数据</td></tr>");
} else {
for(Iterator<User> it=users.iterator(); it.hasNext(); ) {
User u = it.next();
%>
<
tr
>
<
td
>
<%
=
u.getUsername()
%>
</
td
>
<
td
>
<%
=
u.getAge()
%>
</
td
>
<
td
>
<%
=
u.getRelatedGroup().getName()
%>
</
td
>
</
tr
>
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
<%
...
}
}
%>
</
table
>
<
hr
>
使用logic:iterate,empty,notEmpty 并结合 bean:write,property
<
br
>
<
table
border
="1"
>
<
tr
><
td
>
姓名
</
td
><
td
>
年龄
</
td
><
td
>
所属组
</
td
></
tr
>
<
logic:empty
name
="users"
>
<
tr
><
td
colspan
=3
>
没有任何相关数据
</
td
></
tr
>
</
logic:empty
>
<
logic:notEmpty
name
="users"
>
<
logic:iterate
id
="user"
name
="users"
>
<
tr
>
<
td
><
bean:write
name
="user"
property
="username"
/></
td
>
<
td
><
bean:write
name
="user"
property
="age"
/></
td
>
<
td
><
bean:write
name
="user"
property
="relatedGroup.name"
/></
td
>
</
tr
>
</
logic:iterate
>
</
logic:notEmpty
>
</
table
>
</
body
>
</
html
>