在JSP頁面中,使用標記庫代替傳統的Java代碼來實現頁面的顯示邏輯是大勢所趨,然而,由於自定義標記很容易造成重復定義和非標准的實現,於是出現了基於Java Web的JSTL和基於struts的taglib等標簽庫來解決這些問題。
這些標簽庫在實現原理上相差無幾,學會一種,另一種也就很容易上手,那咱們就從Struts的標簽庫走起。
常用的strust標簽庫有<bean:write>、<logic:empty>和<logic:notEmpty>、<logic:present>和<logic:notPresent>、<logic:iterator>4種。
使用方法:
1.在strus-config.xml中配置message-resources,注意,這個標簽要放在action-mappings標簽後面。配置信息如下:
<message-resources parameter="MessageResources" />
2.在src目錄下引入提供國際化資源文件MessageResources.properties
3.在JSP頁面中引入taglib
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
4.下面是分別是<bean:wriete>(獲取字段值、JavaBean屬性值)、<logic:empty>和<logic:notEmpty>(判斷是否為空)、<logic:present>和<logic:notPresent>(判斷是否存在)、<logic:iterator>(迭代取出集合中的數據)分別在Action和JSP頁面的配置
(1)<bean:wriete>-->action
public class BeanWriteAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// 普通字符串
request.setAttribute("hello", "quwenzhe");
request.setAttribute("today", new Date());
Group group = new Group();
group.setName("提高班");
User user = new User();
user.setUsername("quwenzhe");
user.setAge("24");
user.setGroup(group);
request.setAttribute("user", user);
return mapping.findForward("success");
}
}
<bean:wriete>-->JSP
姓名:<input type="text" name="username" value="<bean:write name="user" property="username"/>"><br/>
年齡:<input type="text" name="age" value=" <bean:write name="user" property="age"/>"><br>
所屬組:<input type="text" name="group" value=" <bean:write name="user" property="group.name"/>"><br/>
(2)<logic:empty>和<logic:notEmpty>、<logic:present>和<logic:notPresent>-->action
public class EmptyPresentAction extends 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");
}
}
<logic:empty>和<logic:notEmpty>、<logic:present>和<logic:notPresent>-->JSP
<p>
<logic:empty name="attr1">
attr1為空
</logic:empty>
<logic:notEmpty name="attr1">
attr1為空
</logic:notEmpty>
<logic:present name="attr1">
attr1存在
</logic:present>
<logic:notPresent name="attr1">
attr1不存在
</logic:notPresent>
</p>
<p>
<logic:empty name="attr2">
attr2為空
</logic:empty>
<logic:notEmpty name="attr2">
attr2為空
</logic:notEmpty>
<logic:present name="attr2">
attr2存在
</logic:present>
<logic:notPresent name="attr2">
attr2不存在
</logic:notPresent>
</p>
<p>
<logic:empty name="attr3">
attr3為空
</logic:empty>
<logic:notEmpty name="attr3">
attr3為空
</logic:notEmpty>
<logic:present name="attr3">
attr3存在
</logic:present>
<logic:notPresent name="attr3">
attr3不存在
</logic:notPresent>
</p>
(3)<logic:iterator>-->action
public class IteratorAction extends Action {
@Override
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("quwenzhe-->" + i);
user.setAge("24-->" + i);
user.setGroup(group);
userList.add(user);
}
request.setAttribute("userList", userList);
return mapping.findForward("success");
}
}
<logic:iterator>-->JSP
<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="user" name="userList">
<tr>
<td>
<bean:write name="user" property="username"/>
</td>
<td>
<bean:write name="user" property="age"/>
</td>
<td>
<bean:write name="user" property="group.name"/>
</td>
</tr>
</logic:iterate>
</logic:notEmpty>
</table>
這樣我們在後台Action中通過request設置的值,在JSP頁面中通過標簽就能很方便的獲取到,大大提高工作效率。
struts2文件上傳(保存為BLOB格式) http://www.linuxidc.com/Linux/2014-06/102905.htm
Struts2的入門實例 http://www.linuxidc.com/Linux/2013-05/84618.htm
Struts2實現ModelDriven接口 http://www.linuxidc.com/Linux/2014-04/99466.htm
遇到的Struts2文件下載亂碼問題 http://www.linuxidc.com/Linux/2014-03/98990.htm
Struts2整合Spring方法及原理 http://www.linuxidc.com/Linux/2013-12/93692.htm
Struts2 注解模式的幾個知識點 http://www.linuxidc.com/Linux/2013-06/85830.htm
Struts 的詳細介紹:請點這裡
Struts 的下載地址:請點這裡