bean标签:
Action:
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// 普通属性
request.setAttribute("hello", "Hello World");
// html文本
request.setAttribute("hz", "<font color='red'>杭州欢迎您</font>");
// 日期
request.setAttribute("today", new Date());
// 数字
request.setAttribute("n", 123456.987);
// 结构
Group group = new Group();
group.setName("和盈");
User user = new User();
user.setUsername("张三");
user.setAge(18);
user.setGroup(group);
request.setAttribute("user", user);
return mapping.findForward("success");
}
页面显示:
<h1>测试BeanWrite</h1>
<hr>
<li>普通字符串</li><br>
hello(jsp脚本):<%=request.getAttribute("hello") %><br>
hello(标签):<bean:write name="hello"/><br>
<p>
<li>html文本</li><br>
hz(default):<bean:write name="hz"/><br>
hz(filter="true"):<bean:write name="hz" filter="true"/><br>
hz(filter="false"):<bean:write name="hz" filter="false"/><br>
<p>
<li>格式化日期</li><br>
today(default):<bean:write name="today"/><br>
today(format="yyyy-MM-dd HH:mm:ss"):<bean:write name="today" format="yyyy-MM-dd HH:mm:ss"/>
<p>
<li>格式化数字</li><br>
n(default):<bean:write name="n"/><br>
n(format="###,###.####"):<bean:write name="n" format="###,###.####"/><br>
n(format="###,###.####"):<bean:write name="n" format="###,###.0000"/><br>
<p>
<li>结构</li><br>
姓名:<input type="text" value="<bean:write name="user" property="username"/>"><br>
年龄:<input type="text" value="<bean:write name="user" property="age"/>"><br>
所属组:<input type="text" value="<bean:write name="user" property="group.name"/>"><br>
</body>
配置文件:
<action-mappings>
<action path="/beanwrite" type="com.aowin.struts.BeanWriteTestAction">
<forward name="success" path="/beanwrite.jsp" />
</action>
<action path="/emptypresent" type="com.aowin.struts.EmptyPresentTestAction">
<forward name="success" path="/emptypresent.jsp" />
</action>
<action path="/iterate" type="com.aowin.struts.IterateTestAction">
<forward name="success" path="/iterate.jsp" />
</action>
</action-mappings>
<message-resources parameter="MessageResources" />
使用struts1 bean:write标签必须提供一份国际化文件: MessageResources
否则会报:
Cannot find message resources under key org.apache.struts.action.MESSAGE
在src下面提供一份文件即可 内容为空也可以
使用bean标签需要在页面中加入:
<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%>
logic标签:
Action:
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
request.setAttribute("attr1", null);
request.setAttribute("attr2", "");
request.setAttribute("attr3", new ArrayList());
return mapping.findForward("success");
}
页面使用标签:
<h1>测试empty,notEmpty,present,notPresent</h1>
<hr>
<logic:empty name="attr1">
attr1为空<br>
</logic:empty>
<logic:notEmpty name="attr1">
attr1不为空<br>
</logic:notEmpty>
<logic:present name="attr1">
attr1存在<br>
</logic:present>
<logic:notPresent name="attr1">
attr1不存在<br>
</logic:notPresent>
<p>
<logic:empty name="attr2">
attr2为空<br>
</logic:empty>
<logic:notEmpty name="attr2">
attr2不为空<br>
</logic:notEmpty>
<logic:present name="attr2">
attr2存在<br>
</logic:present>
<logic:notPresent name="attr2">
attr2不存在<br>
</logic:notPresent>
<p>
<logic:empty name="attr3">
attr3为空<br>
</logic:empty>
<logic:notEmpty name="attr3">
attr3不为空<br>
</logic:notEmpty>
<logic:present name="attr3">
attr3存在<br>
</logic:present>
<logic:notPresent name="attr3">
attr3不存在<br>
</logic:notPresent>
Iterate标签:
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Group group = new Group();
group.setName("和盈");
List userList = new ArrayList();
for (int i=0; i<10; i++) {
User user = new User();
user.setUsername("user_" + i);
user.setAge(18+i);
user.setGroup(group);
userList.add(user);
}
request.setAttribute("userlist", userList);
return mapping.findForward("success");
}
页面显示:
<h1>测试Iterate</h1>
<hr>
<li>jsp脚本</li><br>
<table border="1">
<tr>
<td>姓名</td>
<td>年龄</td>
<td>所属组</td>
</tr>
<%
List userList = (List)request.getAttribute("userlist");
if (userList == null || userList.size() == 0) {
%>
<tr>
<td colspan="3">没有符合条件的数据!</td>
</tr>
<%
}else {
for (Iterator iter=userList.iterator(); iter.hasNext(); ) {
User user = (User)iter.next();
%>
<tr>
<td><%=user.getUsername() %></td>
<td><%=user.getAge() %></td>
<td><%=user.getGroup().getName() %></td>
</tr>
<%
}
}
%>
</table>
<p>
<li>标签</li><br>
<table border="1">
<tr>
<td>姓名</td>
<td>年龄</td>
<td>所属组</td>
</tr>
<logic:empty name="userlist">
<tr>
<td colspan="3">没有符合条件的数据!</td>
</tr>
</logic:empty>
<logic:notEmpty name="userlist">
<logic:iterate id="u" name="userlist">
<tr>
<td>
<bean:write name="u" property="username"/>
</td>
<td>
<bean:write name="u" property="age"/>
</td>
<td>
<bean:write name="u" property="group.name"/>
</td>
</tr>
</logic:iterate>
</logic:notEmpty>
</table>
使用Iterate标签加上:
<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%>
<%@ taglib prefix="logic" uri="http://struts.apache.org/tags-logic"%>