176142998

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            116 Posts :: 0 Stories :: 45 Comments :: 0 Trackbacks
          Struts2提供了大量豐富的標(biāo)簽供使用,它不再像Struts1中一樣,將種類標(biāo)簽進(jìn)行分門列別,但可以根據(jù)其使用的用途用以區(qū)別。本文通過對Struts2中數(shù)據(jù)標(biāo)簽的學(xué)習(xí),來對Struts2中標(biāo)簽的學(xué)習(xí)起到一個(gè)拋磚引玉的作用。文中將介紹Action標(biāo)簽、Bean標(biāo)簽、Data標(biāo)簽、Include標(biāo)簽、Param標(biāo)簽、Set標(biāo)簽、Text標(biāo)簽、Property標(biāo)簽等標(biāo)簽。
          一、<s:action>標(biāo)簽

          Action標(biāo)簽,顧名思義,是用來調(diào)用Action的標(biāo)簽,在JSP中頁面中,可以指向具體指定某一命名空間中的某一Action。而標(biāo)簽的主體用于顯示及渲染Actionr的處理結(jié)果。

          1WebRoot\pages\dataTagssuccess.jsp處理頁面

           

          <%@ page contentType="text/html; charset=GBK" %>

          <%@ taglib prefix="s" uri="/struts-tags" %>

          <html>

                 
          <head>

                        
          <title>Action Tag 示例</title>

                 
          </head>

                 
          <body>

                        
          <h2>Action Tag 示例</h2>

                               
          <s:action name="success">

                                      
          <b><i>s:action標(biāo)簽用于在頁面顯示結(jié)果.</i></b></div>

                               
          </s:action>

                 
          </body>

          </html>

           

          這里使用<s:action>標(biāo)簽進(jìn)行頁面跳轉(zhuǎn),并用于顯示處理的結(jié)果。

          2.先來看struts.xml中的配置:

           

                 <action name="actionTag" class="com.sterning.actionTag">

                     
          <result name="success">/pages/dataTags/success.jsp</result>

                 
          </action>

           

          3.接著創(chuàng)建actionTag類:代碼如下:

           

          package com.sterning;

          import com.opensymphony.xwork2.ActionSupport;

          publicclass actionTag 
          extends ActionSupport {

              
          public String execute() throws Exception{

                 returnSUCCESS;

              }


          }

           

          其實(shí)該類中沒有做任何處理,只是進(jìn)行頁面跳轉(zhuǎn)而已。

          4.運(yùn)行效果。


          1.<s:action>標(biāo)簽

          二、<s:bean>標(biāo)簽

          Bean標(biāo)簽,當(dāng)然需要一個(gè)JavaBean。其的屬性值的操作是經(jīng)由Bean標(biāo)簽中的參數(shù)屬性來進(jìn)行賦值。當(dāng)然,它還有一個(gè)id屬性可以進(jìn)行賦值,由于就可以在上下文中使用這個(gè)Bean。請看如下的頁面:

          1WebRoot\pages\dataTags\beanTag.jsp,代碼如下:

           

          <%@ page contentType="text/html; charset=GBK" %>

          <%@ taglib prefix="s" uri="/struts-tags" %>

          <html>

                 
          <head>

                        
          <title>Bean Tag 示例</title>

                 
          </head>

                 
          <body>

                        
          <h2>Bean Tag 示例</h2>

                               
          <s:bean name="com.sterning.companyName" id="uid">

                                      
          <s:param name="name">sterning</s:param> 

                                             
          <s:property value="%{name}" /><br>

                               
          </s:bean>

                 
          </body>

          </html>

           

          可參其關(guān)聯(lián)的JavaBeancom.sterning.companyName,同時(shí)參數(shù)name賦值為sterning

          2.首先創(chuàng)建Action進(jìn)行跳轉(zhuǎn), src\com\sterning\beanTag.java,代碼如下:

           

          package com.sterning;

          import com.opensymphony.xwork2.ActionSupport;

          public class beanTag extends ActionSupport {

                 
          public String execute() throws Exception{

                        
          return SUCCESS;

                 }


          }

           

          然后創(chuàng)建JavaBeansrc\com\sterning\companyName.java,代碼如下:

           

          package com.sterning;

          public class companyName {

                 
          private String name;

                 
          public void setName(String name){

                        
          this.name =name ;

                 }


                 
          public String getName(){

                        
          return name;

                 }


          }

           

          3Struts.xml的配置

          這里配置很簡單,與前面的例子差不多。

           

                        <action name="beanTag" class="com.sterning.beanTag">

                               
          <result name="success">/pages/dataTags/beanTag.jsp</result>

                        
          </action>

           

          4.運(yùn)行效果


          2.<s:bean>標(biāo)簽

          三、<s:date>標(biāo)簽

          Data標(biāo)簽方便在頁面進(jìn)行格式化的日期輸出。格式有多種可供選擇。同時(shí),還可以通過在properties屬性文件中定義好”struts.date.format”參數(shù)的值,從而自定義格式輸出。

          Date標(biāo)簽包含三個(gè)屬性,可以從下面的代碼中感受一下,分別是:

          l         Name:

          l         Nice

          l         Format

          1WebRoot\pages\dataTags\dateTag.jsp

           

          <%@ page contentType="text/html; charset=GBK" %>

          <%@ taglib prefix="s" uri="/struts-tags" %>

          <html>

                 
          <head>

                        
          <title>Date Tag 示例</title>

                 
          </head>

                 
          <body>

                        
          <h2>顯示當(dāng)前的時(shí)間</h2>

                               
          <table border="1" width="35%">

                                      
          <tr>

                                             
          <td><b>日期格式</b></td>

                                             
          <td><b>日期</b></td>

                                      
          </tr>

                                      
          <tr>

                                             
          <td>Day/Month/Year</td>

                                             
          <td><s:date name="currentDate" format="dd/MM/yyyy" /></td>

                                      
          </tr>

                                      
          <tr>

                                             
          <td>Month/Day/Year</td>

                                             
          <td><s:date name="currentDate" format="MM/dd/yyyy" /></td>

                                      
          </tr>

                                      
          <tr>

                                             
          <td>Month/Day/Year</td>

                                             
          <td><s:date name="currentDate" format="MM/dd/yy" /></td>

                                      
          </tr>

                                      
          <tr>

                                             
          <td>Month/Day/Year Hour<B>:</B>Minute</td>

                                             
          <td><s:date name="currentDate" format="MM/dd/yy hh:mm" /></td>

                                      
          </tr>

                                      
          <tr>

                                             
          <td>Month/Day/Year Hour<B>:</B>Minute<B>:</B>Second</td>

                                             
          <td><s:date name="currentDate" format="MM/dd/yy hh:mm:ss" /></td>

                                      
          </tr>

                                      
          <tr>

                                             
          <td>Nice Date (Current Date & Time)</td>

                                             
          <td><s:date name="currentDate" nice="false" /></td>

                                      
          </tr>

                                      
          <tr>

                                             
          <td>Nice Date</td>

                                             
          <td><s:date name="currentDate" nice="true" /></td>

                                      
          </tr>

                               
          </table>

                 
          </body>

          </html>

           

          2src\com\sterning\beanTag.java

          該項(xiàng)類更加簡單,頁面跳轉(zhuǎn)

           

          package com.sterning;

          import com.opensymphony.xwork2.ActionSupport;

          public class beanTag extends ActionSupport {

                 
          public String execute() throws Exception{

                        
          return SUCCESS;

                 }


          }

           

          3Struts.xml配置

           

                        <action name="dateTag" class="com.sterning.dateTag">

                               
          <result>/pages/dataTags/dateTag.jsp</result>

                        
          </action>

           

          4.運(yùn)行效果


          3.<s:date>標(biāo)簽

          四、<s:include>標(biāo)簽

          <s:include>標(biāo)簽用于在當(dāng)前頁面中包含來自其它servletJSP頁面的處理結(jié)果。由于是頁面與頁面(或servlet)之間的頁面包含,因此不需要action來進(jìn)行頁面的跳轉(zhuǎn)。

          1WebRoot\pages\dataTags\includeTag.jsp

           

          <%@ page contentType="text/html; charset=GBK" %>

          <%@ taglib prefix="s" uri="/struts-tags" %>

          <html>

                 
          <head>

                        
          <title>Include Tag 示例</title>

                 
          </head>

                 
          <body>

                        
          <h2>Include Tag 示例</h2>

                               
          <s:include value="myBirthday.jsp" />

                 
          </body>

          </html>

           

          這里包含了另外一個(gè)頁面myBirthday.jsp,其實(shí)相當(dāng)于在JSP頁面里包含其它的頁面。原理一樣的。

          2WebRoot\pages\dataTags\myBirthday.jsp

          這個(gè)頁面利用了上面所講的<s:date>標(biāo)簽進(jìn)行日期的格式化輸出

           

          <%@ page contentType="text/html; charset=GBK" %>

          <%@ taglib prefix="s" uri="/struts-tags" %>

          <html>

                 
          <head>

                        
          <title>Include Tag 示例</title>

                 
          </head>

                 
          <body>

                               
          <table border="1" width="35%">

                                      
          <tr>

                                             
          <td><b>Date Format</b></td>

                                             
          <td><b>Date</b></td>

                                      
          </tr>

                                      
          <tr>

                                             
          <td>Day/Month/Year</td>

                                             
          <td><s:date name="myBirthday" format="dd/MM/yyyy" /></td>

                                      
          </tr>

                                      
          <tr>

                                             
          <td>Month/Day/Year</td>

                                             
          <td><s:date name="myBirthday" format="MM/dd/yyyy" /></td>

                                      
          </tr>

                                      
          <tr>

                                             
          <td>Month/Day/Year</td>

                                             
          <td><s:date name="myBirthday" format="MM/dd/yy" /></td>

                                      
          </tr>

                                      
          <tr>

                                             
          <td>Month/Day/Year Hour<B>:</B>Minute</td>

                                             
          <td><s:date name="myBirthday" format="MM/dd/yy hh:mm" /></td>

                                      
          </tr>

                                      
          <tr>

                                             
          <td>Month/Day/Year Hour<B>:</B>Minute<B>:</B>Second</td>

                                             
          <td><s:date name="myBirthday" format="MM/dd/yy hh:mm:ss" /></td>

                                      
          </tr>

                                      
          <tr>

                                             
          <td>Nice Date (Current Date & Time)</td>

                                             
          <td><s:date name="myBirthday" nice="false" /></td>

                                      
          </tr>

                               
          </table>

                 
          </body>

          </html>

           

          3Struts.xml配置

           

                        <action name="includeTag" class="com.sterning.includeTag">

                               
          <result>/pages/dataTags/includeTag.jsp</result>

                        
          </action>

           

          4.運(yùn)行效果


          4.<s:include>標(biāo)簽

          五、<s:param>標(biāo)簽

          Param標(biāo)簽用于傳遞參數(shù),如給<s:bean>標(biāo)簽傳遞參數(shù)。它有如下兩個(gè)屬性:

          l         Name(String):參數(shù)名;

          l         Value(Object):參數(shù)值。

          1WebRoot\pages\dataTags\paramTag.jsp

           

          <%@ page contentType="text/html; charset=GBK" %>

          <%@ taglib prefix="s" uri="/struts-tags" %>

          <html>

                 
          <head>

                        
          <title>Param Tag 示例</title>

                 
          </head>

                 
          <body>

                        
          <h2>Param Tag 示例</h2>

                               
          <ui:component>

                                      
          <ui:param name="empname">Emp1</ui:param><br>

                                      
          <ui:param name="empname">Emp2</ui:param><br>

                                      
          <ui:param name="empname">Emp3</ui:param> 

                               
          </ui:component>

                 
          </body>

          </html>

           

          2Struts.xml配置

           

                        <action name="paramTag">

                               
          <result>/pages/dataTags/paramTag.jsp</result>

                        
          </action>

           

          3.運(yùn)行效果


          5.<s:param>標(biāo)簽

          六、<s:set>標(biāo)簽

          Set標(biāo)簽比較簡單。Set標(biāo)簽用戶將某一值賦給某一變量,因此,任何對該項(xiàng)值的引用都可以通過該變量來得到該值。該變量的活動(dòng)范圍可自定義。如下例中,定義一健/值對,對值的引用,直接引用值就可以。。請看示例

          1WebRoot\pages\dataTags\ setTag.jsp

           

          <%@ page contentType="text/html; charset=GBK" %>

          <%@ taglib prefix="s" uri="/struts-tags" %>

          <html>

                 
          <head>

                        
          <title>Set Tag 示例</title>

                 
          </head>

                 
          <body>

                        
          <h2>Set Tag 示例</h2>

                               
          <s:set name="technologyName" value="%{'Java'}"/>

                                      Technology Name: 
          <s:property value="#technologyName"/>

                 
          </body>

          </html>

           

          2Struts.xml配置

           

                        <action name="setTag">

                               
          <result>/pages/dataTags/setTag.jsp</result>

                        
          </action>

           

          3.運(yùn)行效果


          6.<s:set>標(biāo)簽

          七、<s:property>標(biāo)簽

          Property顧名思義,可以與<s:bean>標(biāo)簽結(jié)合使用,一個(gè)是給bean賦值,一個(gè)是從bean中讀取值。直接來看示例:

          1WebRoot\pages\dataTags\propertyTag.jsp

           

          <%@ page contentType="text/html; charset=GBK" %>

          <%@ taglib prefix="s" uri="/struts-tags" %>

          <html>

                 
          <head>

                        
          <title>Property Tag 示例</title>

                 
          </head>

                 
          <body>

                        
          <h2>Property Tag 示例</h2>

                               
          <!-- Example to pick the value through bean class -->

                               
          <s:bean name="com.sterning.companyName" id="uid">

                                      
          <s:param name="name">sterning</s:param> 

                                             
          <s:property value="%{name}" /><br>

                                      
          </s:bean>

                               
          <!-- Default value -->

                                             
          <s:property value="name" default="Default Value" />

                 
          </body>

          </html>

           

          2Bean

          當(dāng)然這里用到了Bean. com\sterning\ companyName.java,代碼如下:

           

          package com.sterning;

          public class companyName {

                 
          private String name;

                 
          public void setName(String name){

                        
          this.name =name ;

                 }


                 
          public String getName(){

                        
          return name;

                 }


          }

           

          3src"com"sterning" propertyTag.java

          進(jìn)行頁面跳轉(zhuǎn)

           

          package com.sterning;

          import com.opensymphony.xwork2.ActionSupport;

          public class propertyTag extends ActionSupport {

            
          public String execute() throws Exception{

              
          return SUCCESS;

           }


          }

           

          4Struts.xml配置

           

                        <action name="propertyTag" class="com.sterning.propertyTag">

                               
          <result>/pages/dataTags/propertyTag.jsp</result>

                        
          </action>

           

          5.運(yùn)行效果


          7.<s:property>標(biāo)簽

          posted on 2008-08-04 10:58 飛飛 閱讀(375) 評論(1)  編輯  收藏

          Feedback

          # re: Struts2中數(shù)據(jù)標(biāo)簽使用示例 2008-12-12 13:26 tpf
          好,正需要了!
            回復(fù)  更多評論
            


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          主站蜘蛛池模板: 浙江省| 漳平市| 稻城县| 马龙县| 广元市| 陆河县| 万盛区| 西畴县| 手游| 神池县| 台北市| 南投市| 彰化市| 庄浪县| 阿巴嘎旗| 池州市| 原阳县| 丹江口市| 余姚市| 广昌县| 依兰县| 乐都县| 文山县| 龙口市| 山丹县| 远安县| 英吉沙县| 连平县| 云林县| 老河口市| 离岛区| 静乐县| 巴彦县| 苍溪县| 饶河县| 鞍山市| 漯河市| 天长市| 东兰县| 略阳县| 满城县|