1)使用標籤庫所需要的配置:
添加國際化的配置:首先複製 <message-resources parameter="MessageResources" />至struts-config.xml。接着複製MessageResources.properties這個文件至src目錄下。
2)部署好項目
3)在jsp頁面使用struts中bean標籤。
首先使用struts標籤需要引入struts標籤庫。引入時如下; <%@ taglib prefix="" uri=""%>,其中prefix和uri可以在struts.jar-->META-INF-->tlds-->struts-bean.tld中找到。如下;<shortname>bean</shortname>對應prefix
<uri>http://struts.apache.org/tags-bean</uri>對應uri,引入之後如下: <%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%>
引入Bean標籤庫之後就可以使用Bean標籤了。在Bean標籤庫中主要是bean:write標籤。bean:write標籤常用屬性如下:
name屬性:name=“變量的名字”,根據scop中相應變量的名字取出變量的值。例如:<bean:write name="Hello"/>,標籤是在服務器端執行的。
filter屬性:filter="true"默認爲true。表示遇到html內容,原樣輸出,即不解析。設爲false,則會解析html標籤,只輸出內容。
format屬性:格式化日期,不使用這個屬性,則會原樣輸出。例如:format="yyyy-MM-dd HH:mm:ss".format也用於格式化數字,用法如下:format="###,###.###"表示每隔三位一個逗號。小數點後保留三位,#也可以用0代替
property屬性:用於取出一個對象裏的某一個屬性,property的值爲要取的對象的屬性的名字。支持導航。
4)在jsp頁面使用struts中logic標籤。
同樣,要使用logic標籤,需要引入logic標籤庫。方法通上。引入之後就可以使用了。
logic:empty和logic:notEmpty標籤的主要屬性有:
name屬性:name的值等於要判斷的值或者對象的名字,會在scop範圍內尋找。結果爲空或者不空,不存在也安爲空處理。相當於if
logic:present和logic:notPresent標籤的主要屬性有:
name屬性:name的值等於要判斷的值或者對象的名字,會在scop範圍內尋找。結果爲存在或者不存在。
logic:iterate標籤的主要屬性:(iterate標籤和上面的empry在一起使用)
name屬性:指的就是scop中的變量名字
id屬性:id用於設定臨時變量,id=”臨時變量名“,迭代時先把值放在這個變量裏。
property屬性:用於取出一個對象裏的某一個屬性,property的值爲要取的對象的屬性的名字。支持導航。
在iterate標籤裏輸出使用Beanwrite標籤
用法舉例:
<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>
轉自:http://www.cnblogs.com/liuyang-1037/archive/2009/03/09/1407158.html