分類(lèi):我的著(zhù)作
在第三章示例的showAttackSolution.jsp中出現了這樣的使用:
<logic:iterate name="allAttackSolution"
id="attackSolution"
type="struts.sample.cap1.sample3.entity.AttackSolution">
<tr>
<td style="word-break: break-all;">
<bean:write property="attack_event_code" name="attackSolution" />
</td>
<td style="word-break: break-all;">
<bean:write property="attack_mean" name="attackSolution" />
</td>
<td style="word-break: break-all;">
<bean:write property="attack_action" name="attackSolution" />
</td>
<td style="word-break: break-all;">
<input type="button"
onclick="del(‘<%=attackSolution.getAttack_event_code()%>‘);"
value="<bean:message key="message.delete"/>">
</td>
</tr>
</logic:iterate>
由于在Action中將顯示的內容作為ArrayList類(lèi)型的實(shí)例保存在request中,因此這段JSP頁(yè)面標簽的工作是:
(1)利用<logic:iterate>標簽對保存在ArrayList實(shí)例中的所有對象進(jìn)行循環(huán)取得。
(2)ArrayList類(lèi)型
根據之前討論的“<logic:iterate>標簽被<c:forEach>標簽和EL表達式替換”,可以利用<c:forEach>標簽和EL表達式來(lái)修改該段JSP代碼。修改的方式有兩種:
q 完全使用<c:forEach>標簽和EL表達式來(lái)替換全部。
q 僅使用EL表達式來(lái)替換<bean:write>標簽。
1. <c:forEach>標簽和EL表達式
<c:forEach>標簽和EL表達式:
<c:forEach items="${requestScope.allAttackSolution}"
var="attackSolution">
<tr>
<td style="word-break: break-all;" >
${attackSolution.attack_event_code}
</td>
<td style="word-break: break-all;" >
${attackSolution.attack_mean}
</td>
<td style="word-break: break-all;" >
${attackSolution.attack_action}
</td>
<td style="word-break: break-all;" >
<input type="button"
onclick="del(‘${attackSolution.attack_event_code}‘);"
value="<bean:message key="message.delete"/>">
</td>
</tr>
</c:forEach>
這種修改方式將屏棄Struts框架的<logic:iterate>標簽,而以<c:forEach>標簽來(lái)作為循環(huán)迭代的工作。它的最大優(yōu)點(diǎn)是無(wú)需關(guān)注集合中的對象類(lèi)型,只要保證該對象是一個(gè)標準的JavaBean就可以了。
2. 使用EL表達式來(lái)替換<bean:write>標簽
<logic:iterate name="allAttackSolution"
id="attackSolution"
type="struts.sample.cap1.sample3.entity.AttackSolution">
<tr>
<td style="word-break: break-all;" >
${attackSolution.attack_event_code}
</td>
<td style="word-break: break-all;" >
${attackSolution.attack_mean}
</td>
<td style="word-break: break-all;" >
${attackSolution.attack_action}
</td>
<td style="word-break: break-all;" >
<input type="button"
onclick="del(‘${attackSolution.attack_event_code}‘);"
value="<bean:message key="message.delete"/>">
</td>
</tr>
</logic:iterate>
這種方式對原來(lái)的代碼沒(méi)有做多大的改動(dòng),依然會(huì )使用<logic:iterate>標簽來(lái)作為循環(huán)標簽。不過(guò)對于原來(lái)使用<bean:write>標簽做顯示功能的地方,摒棄了<bean:write>標簽而直接使用EL表達式。靈活的EL表達式對頁(yè)面顯示邏輯有很大幫助,這種方式比較適合熟悉<logic:iterate>標簽的程序設計者。
下面看一個(gè)完整的修改后JSP頁(yè)面的代碼,注釋掉的是被替換之前的代碼,讀者可以比較一下兩種實(shí)現方法。請見(jiàn)例9.7。
例9.7:修改后showAttackSolution.jsp。
<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<html>
<head>
<!-- 略過(guò)JavaScript部分 -->
...
</head>
<body>
<em><bean:message key="message.attacksolutionDB"/></em><p>
<table>
<html:errors/>
</table>
<bean:message key="message.attackcode"/>:
<input name="attack_event_codeC" value="" type="text">
<bean:message key="message.attackdesc"/>:
<TEXTAREA style="height:100" name=attack_meanC></TEXTAREA>
<bean:message key="message.attacksolution"/>:
<TEXTAREA style="height:100" name=attack_actionC></TEXTAREA>
<p/>
<html:form action="AddAttackSolutionAction.do">
<html:hidden property="attack_event_code"/>
<html:hidden property="attack_mean"/>
<html:hidden property="attack_action"/>
<input type="button" onclick="add();" value="<bean:message key="message.add"/>">
<input type="button"
onclick="search();"
value="<bean:message key="message.search"/>">
</html:form>
<table border=1 cellspacing=1 cellpadding=2>
<tr>
<td style="background-color: #808080;font-size: 12pt;color: #ffffff;font-weight:
bold;line-height: 15pt;border: 1px solid #808080;">
<bean:message key="message.attackcode"/>
</td>
<td style="background-color: #808080;font-size: 12pt;color: #ffffff;font-weight:
bold;line-height: 15pt;border: 1px solid #808080;">
<bean:message key="message.attackdesc"/>
</td>
<td style="background-color: #808080;font-size: 12pt;color: #ffffff;font-weight:
bold;line-height: 15pt;border: 1px solid #808080;">
<bean:message key="message.attacksolution"/>
</td>
<td style="background-color: #808080;font-size: 12pt;color: #ffffff;font-weight:
bold;line-height: 15pt;border: 1px solid #808080;">
<bean:message key="message.delete"/>
</td>
</tr>
<!-- 沒(méi)有替換前的代碼 -->
<!--
<logic:notEmpty name="allAttackSolution">
<logic:iterate name="allAttackSolution"
id="attackSolution"
type="struts.sample.cap1.sample3.entity.AttackSolution">
<tr>
<td style="word-break: break-all;" >
<bean:write property="attack_event_code"
name="attackSolution"/>
</td>
<td style="word-break: break-all;" >
<bean:write property="attack_mean" name="attackSolution"/>
</td>
<td style="word-break: break-all;" >
<bean:write property="attack_action" name="attackSolution"/>
</td>
<td style="word-break: break-all;" >
<input type="button"
onclick="del(‘
<bean:write
property="attack_event_code"
name="attackSolution"/>‘);"
value="<bean:message key="message.delete"/>">
</td>
</tr>
</logic:iterate>
</logic:notEmpty>
-->
<!-- 僅替換<bean:write>標簽的代碼 -->
<!--
<logic:notEmpty name="allAttackSolution">
<logic:iterate name="allAttackSolution"
id="attackSolution"
type="struts.sample.cap1.sample3.entity.AttackSolution">
<tr>
<td style="word-break: break-all;" >
${attackSolution.attack_event_code}
</td>
<td style="word-break: break-all;" >
${attackSolution.attack_mean}
</td>
<td style="word-break: break-all;" >
${attackSolution.attack_action}
</td>
<td style="word-break: break-all;" >
<input type="button"
onclick="del(‘${attackSolution.attack_event_code}‘);"
value="<bean:message key="message.delete"/>">
</td>
</tr>
</logic:iterate>
</logic:notEmpty>
-->
<!-- 替換后的實(shí)現代碼 -->
<c:if test="${(requestScope.allAttackSolution != null)
&& fn:length(requestScope.allAttackSolution) != 0}">
<c:forEach items="${requestScope.allAttackSolution}" var="attackSolution">
<tr>
<td style="word-break: break-all;" >
${attackSolution.attack_event_code}
</td>
<td style="word-break: break-all;" >
${attackSolution.attack_mean}
</td>
<td style="word-break: break-all;" >
${attackSolution.attack_action}
</td>
<td style="word-break: break-all;" >
<input type="button"
onclick="del(‘${attackSolution.attack_event_code}‘);"
value="<bean:message key="message.delete"/>">
</td>
</tr>
</c:forEach>
</c:if>
</table>
</body>
</html>
可以看到,在這個(gè)被修改的JSP頁(yè)面代碼中,利用了Struts框架提供的標簽來(lái)實(shí)現提交部分的工作以及國際化資源配置文件讀取顯示的工作,也利用JSTL的標簽庫和EL表達式來(lái)實(shí)現頁(yè)面邏輯部分的工作。
在JSP頁(yè)面使用JSTL是一種規范,也是一件令人興奮的事情,因為它使JSP部分的程序設計變得更加有效合理。
在本章的介紹中,筆者花了很大一段來(lái)介紹JSTL的EL
不得不說(shuō)的是,JSTL實(shí)在是很棒的技術(shù),它為JSP的程序設計者帶來(lái)了福音。
當在JSP
聯(lián)系客服