laies 发表于 2013-2-6 10:00:51

常用的Struts 2.0的标志(Tag)介绍

在介绍常用标志前,我想先从总体上,对Struts 1.x与Struts 2.0的标志库(Tag Library)作比较。
                                     Struts 1.x            Struts 2.0                        分类            将标志库按功能分成HTML、Tiles、Logic和Bean等几部分            严格上来说,没有分类,所有标志都在URI为“/struts-tags”命名空间下,不过,我们可以从功能上将其分为两大类:非UI标志和UI标志                        表达式语言(expression languages)            不支持嵌入语言(EL)            OGNL、JSTL、Groovy和Velcity          以上表格,纯属个人总结,如有所不足或错误,请不吝指正
好了,我要开始介绍“常用”(这里所谓的“常用”,是指在已往工作中使用Struts里经常用到的)的标志了。
 
                        http://cwiki.apache.org/confluence/images/icons/emoticons/lightbulb_on.gif             要在JSP中使用Struts 2.0标志,先要指明标志的引入。通过在JSP的代码的顶部加入以下代码可以做到这点。
            <%@taglib prefix="s" uri="/struts-tags" %>          <ol>    <li style="font-weight: bold; font-size: 14px;">非UI标志    <ul>      <li style="font-size: 12px;">if、elseif和else      <div style="font-weight: normal;">      描述:
      执行基本的条件流转。
      参数:
                                                    名称                  必需                  默认                  类型                  描述                  备注                                                  test                  是                                     Boolean                  决定标志里内容是否显示的表达式                  else标志没有这个参数                                                  id                  否                                     Object/String                  用来标识元素的id。在UI和表单中为HTML的id属性                                                            例子:
      <div style="border-right: #cccccc 1px solid; padding-right: 5px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; padding-top: 4px; border-bottom: #cccccc 1px solid; background-color: #eeeeee;"><%@ page contentType="text/html; charset=UTF-8" %>
      <%@ taglib prefix="s" uri="/struts-tags" %>
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      <html>
          <head>
              <title>Condition Flow</title>
          </head>
          <body>
              <h3>Condition Flow</h3>            
              <!--
                  这里有点小技巧:
                  本来可以用#parameters.name来获得,请求中name的值。但是,在我实现include例子时,
                  无论我用param标志给name赋任何值,#parameters里面不会含有任何值,所以#parameters.name也为空值。
                  
                  其原因为:
                  当使用include标志时,被包含的页面(included)里#parameters拿到的是包含页面里的请求参数。
                  
                  因此,这里必须手工调用request.getParameter("name")。
              -->
              <s:set name="name" value="<%= "'" + request.getParameter("name") + "'" %>" />
              <s:if test="#name == 'Max'">
                  Max's file here
              </s:if>
              <s:elseif test="#name == 'Scott'">
                  Scott's file here
              </s:elseif>
              <s:else>
                  Other's file here
              </s:else>        
          </body>
      </html>
页: [1]
查看完整版本: 常用的Struts 2.0的标志(Tag)介绍