JSTL与Struts的结合(十二)

 JSP部分:<logic:iterate><c:forEach>标签

在第三章示例的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"

                    οnclick="del(‘<%=attackSolution.getAttack_event_code()%>‘);"

value="<bean:message key="message.delete"/>">

                   </td>

         </tr>

</logic:iterate>

由于在Action中将显示的内容作为ArrayList类型的实例保存在request中,因此这段JSP页面标签的工作是:

1)利用<logic:iterate>标签对保存在ArrayList实例中的所有对象进行循环取得。

2ArrayList类型 实例 中的对象为struts.sample.cap1.sample3.entity.AttackSolution类型,AttackSolution Java类中的变量属性都有getset方法,因此可以被认为是一个标准的JavaBean。利用<bean:write>标签将AttackSolution实例的变量属性读取出来,并显示。

根据之前讨论的“<logic:iterate>标签被<c:forEach>标签和EL表达式替换”,可以利用<c:forEach>标签和EL表达式来修改该段JSP代码。修改的方式有两种:

q         完全使用<c:forEach>标签和EL表达式来替换全部。

q         仅使用EL表达式来替换<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"

οnclick="del(‘${attackSolution.attack_event_code}‘);"

value="<bean:message key="message.delete"/>">

</td>

         </tr>

</c:forEach>

这种修改方式将屏弃Struts框架的<logic:iterate>标签,而以<c:forEach>标签来作为循环迭代的工作。它的最大优点是无需关注集合中的对象类型,只要保证该对象是一个标准的JavaBean就可以了。

2. 使用EL表达式来替换<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"

οnclick="del(‘${attackSolution.attack_event_code}‘);"

value="<bean:message key="message.delete"/>">

</td>

         </tr>

</logic:iterate>

这种方式对原来的代码没有做多大的改动,依然会使用<logic:iterate>标签来作为循环标签。不过对于原来使用<bean:write>标签做显示功能的地方,摒弃了<bean:write>标签而直接使用EL表达式。灵活的EL表达式对页面显示逻辑有很大帮助,这种方式比较适合熟悉<logic:iterate>标签的程序设计者。

9.9.5  完整的JSP

下面看一个完整的修改后JSP页面的代码,注释掉的是被替换之前的代码,读者可以比较一下两种实现方法。请见例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>

    <!-- 略过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">&nbsp;

<bean:message key="message.attackdesc"/>:

<TEXTAREA style="height:100" name=attack_meanC></TEXTAREA>&nbsp;

<bean:message key="message.attacksolution"/>:

<TEXTAREA style="height:100" name=attack_actionC></TEXTAREA>&nbsp;

    <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" οnclick="add();" value="<bean:message key="message.add"/>">

        <input type="button" 

οnclick="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>

        <!-- 没有替换前的代码 -->

        <!--

            <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"

οnclick="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"

οnclick="del(‘${attackSolution.attack_event_code}‘);"

value="<bean:message key="message.delete"/>">

                        </td>

                    </tr>

                </logic:iterate>

            </logic:notEmpty>

        -->

       

        <!-- 替换后的实现代码 -->      

        <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"

                              οnclick="del(‘${attackSolution.attack_event_code}‘);"

value="<bean:message key="message.delete"/>">

                    </td>

                </tr>

            </c:forEach>

        </c:if>

    </table>

  </body>

</html>

可以看到,在这个被修改的JSP页面代码中,利用了Struts框架提供的标签来实现提交部分的工作以及国际化资源配置文件读取显示的工作,也利用JSTL的标签库和EL表达式来实现页面逻辑部分的工作。

JSP页面使用JSTL是一种规范,也是一件令人兴奋的事情,因为它使JSP部分的程序设计变得更加有效合理。

9.10  本章回顾

在本章的介绍中,笔者花了很大一段来介绍JSTLEL 表达式 和标签库的工作,接着也对Struts标签库和JSTL标签库中相类似的标签作了比较,最后还修改了第三章的示例来真正的将Struts框架和JSTL结合在了一起。

不得不说的是,JSTL实在是很棒的技术,它为JSP的程序设计者带来了福音。

当在JSP 页面 使用Java语言片段已经成为了历史,在JSP页面自定义 标签 库已经不成为时尚的今天,何不试试JSTL呢?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不讲理的胖子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值