Struts2.x快速上手4--对视图的改进(使用struts标签库)
目的:尽可能的消灭jsp中的Java代码
1)加入标签库:
<%@ taglib prefix="s" uri="/struts-tags"%>
?
2)logon.jsp---->logonPage.jsp
<table border=1>
?<s:form action="Logon" method="post">
??<tr>
???<s:textfield name="username" label="username"></s:textfield>
??</tr>
??<tr>
???<s:password name="password" label="password"></s:password>
??</tr>
??<tr>
???<s:submit value="login"></s:submit>
??</tr>
?</s:form>
</table>
注解:
<s:form/>标签
<s:textfield/>
<s:password/>
<s:submit/>
?
3)viewCompanies.jsp----->listCompanies.jsp
<h1>It公司信息列表</h1>
<table border=1>
?<tr>
??<th>公司名</th>
??<th>城市</th>
??<th>地址</th>
??<th>邮箱</th>
?</tr>
?<!-- 迭代输出ValueStack对象中的comps, 其中status为迭代索引 -->
?<s:iterator value="comps" status="index">
??<!-- 判断索引是否为奇数 -->
??<s:if test="#index.odd == true">
???<tr style="background-color:yellow">
??</s:if>
??<s:else>
???<tr style="background-color:red">
??</s:else>
???<td><s:property value="compName"/></td>
???<td><s:property value="city"/></td>
???<td><s:property value="address"/></td>
???<td><s:property value="email"/></td>
??</tr>
?</s:iterator>
</table>
注解:
<s:if test/>:判断标签
<s:else/>
<s:iterator/>迭代输出,等价于jstl中的<c:forEach/>