posts - 297,  comments - 1618,  trackbacks - 0
           

          說(shuō)明:本文經(jīng)過(guò)筆者對(duì)struts2.0.8自帶的實(shí)例struts2-blank-2.0.8學(xué)習(xí)而寫(xiě),對(duì)該例子進(jìn)行了部分修改,希望對(duì)廣大象要學(xué)習(xí)struts2的朋友有所幫助。

          一.             實(shí)例說(shuō)明

           本例是Struts2的簡(jiǎn)單實(shí)例,通過(guò)本工程的學(xué)習(xí),我們將會(huì)對(duì)struts2的表單以及表單驗(yàn)證有一個(gè)初步的認(rèn)識(shí)、了解struts2的配置以及初探Struts2的本地化輸出。

          二.             編碼準(zhǔn)備

          1. 包的引入

          MyEclipseNetBeans中建立web工程,將所需的包放入WebRoot/lib目錄中,本實(shí)例所需的包有:

           commons-logging-1.1.jar

           freemarker-2.3.8.jar

           ognl-2.6.11.jar

           struts2-core-2.0.8.jar

           xwork-2.0.3.jar

           這些包在struts-2.0.8-all\struts-2.0.8\lib目錄下都可以找到,請(qǐng)讀者自行下載。

          2. web.xml的配置

          要使struts能正常工作,需修改web.xml的內(nèi)容,為其增加struts2FilterDispatcher,修改后的web.xml的內(nèi)容如下:

          <?xml version="1.0" encoding="UTF-8"?>
          <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
              
          <display-name>Struts Blank</display-name>
              
          <filter>
                  
          <filter-name>struts2</filter-name>
                  
          <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
              
          </filter>

              
          <filter-mapping>
                  
          <filter-name>struts2</filter-name>
                  
          <url-pattern>/*</url-pattern>
              
          </filter-mapping>

              
          <welcome-file-list>
                  
          <welcome-file>index.html</welcome-file>
              
          </welcome-file-list>

          </web-app>

          3. 建立源碼目錄和jsp存放目錄

           src目錄下建立example文件夾,用于存放本實(shí)例的java文件等。在WebRoot文件夾下建立子文件夾example,用于存放本工程的jsp文件。

          三.             編碼

          1.       Welcome.jspLogin.jsp的編寫(xiě)

          首先我們建立Welcome.jsp,該文件包含兩個(gè)鏈接,點(diǎn)擊“登錄”鏈接后跳轉(zhuǎn)到登錄信息輸入頁(yè)面,點(diǎn)擊“注冊(cè)”按鈕跳轉(zhuǎn)到注冊(cè)頁(yè),為了學(xué)習(xí)struts2配置中的通配符使用,我們暫不實(shí)現(xiàn)注冊(cè)功能。Welcome.jsp的代碼如下:
          <%@ page contentType="text/html; charset=UTF-8" %>
          <%@ taglib prefix="s" uri="/struts-tags" %>
          <html>
          <head>
              
          <title>歡迎界面</title>
              
          <link href="<s:url value="/css/examplecss"/>" rel="stylesheet" type="text/css"/>
          </head>

          <body>
              
          <h3>導(dǎo)航</h3>
              
          <ul>
                  
          <li><href="<s:url value="/example/Login.jsp"/>">登錄</a></li>
                  
          <li><href="<s:url action="Register"/>">注冊(cè)</a></li>
              
          </ul>
          </body>
          </html>
           

          在該頁(yè)面的頂部,我們需要將struts2的標(biāo)簽庫(kù)引入,語(yǔ)句為:<%@ taglib prefix="s" uri="/struts-tags" %>

          在該頁(yè)面,主要用到struts2<s:url>標(biāo)簽,該頁(yè)面主要用到該標(biāo)簽的兩個(gè)屬性,分別為valueaction,其中action屬性表示用action來(lái)產(chǎn)生url,而value表示使用的目標(biāo)值。在頁(yè)面上點(diǎn)擊“查看源文件”按鈕,可看到生成的語(yǔ)句分別變?yōu)椋?/span>

          <link href="/struts2-blank-2.0.8/css/examplecss" rel="stylesheet" type="text/css"/>

          <a href="/struts2-blank-2.0.8/example/Login.jsp ">

          <a href="/struts2-blank-2.0.8/example/Register.action">

          由此可知使用該標(biāo)簽時(shí),struts2會(huì)自動(dòng)為我們帶上下文路徑,對(duì)于加了屬性action<s:url>標(biāo)簽,后面會(huì)自動(dòng)帶上“.action”作為后綴。

           點(diǎn)擊“登錄”鏈接后,跳轉(zhuǎn)到Login.jsp頁(yè)面,該頁(yè)包含一個(gè)登錄表單,讓用戶(hù)輸入用戶(hù)名和密碼信息,用戶(hù)點(diǎn)擊提交按鈕,跳轉(zhuǎn)到指定的Action——Login進(jìn)行處理。Login.jsp的內(nèi)容如下:

          <%@ page contentType="text/html; charset=UTF-8" %>
          <%@ taglib prefix="s" uri="/struts-tags" %>
          <html>
          <head>
              
          <title>登錄</title>
          </head>
          <body>
          <s:form action="Login">
              
          <s:textfield name="username" label="用戶(hù)名"/>
              
          <s:password name="password"  label="密碼"/>
              
          <s:submit/>
          </s:form>
          </body>
          </html>

          該頁(yè)用到Struts2的表單標(biāo)簽<s:form><s:textfield><s:password>

           <s:form>action屬性表示表單提交后跳轉(zhuǎn)的action的名稱(chēng),此處為Login,該標(biāo)簽最終將生成HTMLform

           <s:textfield>標(biāo)簽類(lèi)同于HTML<input type=”text” …>,其中name表示屬性域的名稱(chēng),label表示其前的提示名;

           <s:password>標(biāo)簽類(lèi)同于HTML<input type=”password” …>,其namelabel類(lèi)同于<s:textfield>,在此略。

          2.       配置文件struts.xmlexample.xml

          在上述jsp頁(yè)面,我們需跳轉(zhuǎn)到兩個(gè)Action地址,需在struts2的配置文件中配置,因當(dāng)工程變大時(shí),一個(gè)龐大的struts2的配置極難維護(hù),建議按包路徑分開(kāi)配置文件,所以本實(shí)例除了struts.xml配置文件外,還新增了一個(gè)額外的配置文件example.xml。該文件在struts.xml中引用。struts.xml放在src目錄下,該文件的內(nèi)容如下:
          <?xml version="1.0" encoding="UTF-8" ?>
          <!DOCTYPE struts PUBLIC
              "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
              "http://struts.apache.org/dtds/struts-2.0.dtd"
          >

          <struts>
              
          <constant name="struts.enable.DynamicMethodInvocation" value="false" />
              
          <constant name="struts.devMode" value="false" />
              
          <include file="example.xml"/>
              
          <!-- Add packages here -->
          </struts>

          可看到該文件通過(guò)<include file="example.xml"/>example.xml也作為struts2的配置文件。

          接下來(lái),讓我們看看example.xml的配置:

          <?xml version="1.0" encoding="UTF-8" ?>
          <!DOCTYPE struts PUBLIC
                  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
                  "http://struts.apache.org/dtds/struts-2.0.dtd"
          >
          <struts>
          <package name="example" namespace="/example" extends="struts-default">
                  
          <action name="Login_input" class="example.Login">
                      
          <result name="input">/example/Login.jsp</result>
                      
          <result type="redirect-action">Menu</result>
                  
          </action>
                  
          <!-- 為學(xué)習(xí)struts2配置文件中通配符的使用,我們將未定義的action的引用都定向到example.ExampleSupport這個(gè)Action中, -->
                  
          <!-- 需定向的Action的名字傳到{1}中,eg.若請(qǐng)求Register這個(gè)action,當(dāng)ExampleSupport返回success時(shí),跳轉(zhuǎn)到/example/Register.jsp -->
                  
          <action name="*" class="example.ExampleSupport">
                      
          <result>/example/{1}.jsp</result>
                  
          </action>
              
          </package>
          </struts>

          3.       LoginExampleSupport類(lèi)以及驗(yàn)證配置類(lèi)Login-validation.xml的編寫(xiě)

          在配置文件example.xml中,定義了兩個(gè)Action,下面我們用代碼來(lái)實(shí)現(xiàn)這兩個(gè)Action

          首先讓我們來(lái)看看ExampleSupport這個(gè)Action,這個(gè)Action不做任何操作,集成自ActionSupport,是本工程的各Action類(lèi)的基類(lèi),該類(lèi)的代碼如下:

          package example;
          import com.opensymphony.xwork2.ActionSupport;
          public class ExampleSupport extends ActionSupport {
          }


          接著讓我們來(lái)看看Login這個(gè)Action,該類(lèi)繼承自ExampleSupport類(lèi),該Action需實(shí)現(xiàn)的業(yè)務(wù)邏輯如下:

          1)       當(dāng)用戶(hù)名(username)或密碼(password)有一者或兩者為空時(shí),登錄不成功,跳轉(zhuǎn)到登錄信息輸入頁(yè)面;

          2)       當(dāng)用戶(hù)名(username)和密碼(password)都不為空時(shí),登錄成功,跳轉(zhuǎn)到主菜單頁(yè)。

           對(duì)于用戶(hù)名和密碼的驗(yàn)證,我們可以先考慮在Login類(lèi)中用代碼實(shí)現(xiàn)的方式,此時(shí)該類(lèi)的代碼如下:
          package example;

          public class Login extends ExampleSupport {
              
          public String execute() throws Exception {
                  
          if (isInvalid(getUsername())) 
                      
          return INPUT;
                  
          if (isInvalid(getPassword())) 
                      
          return INPUT;
                  
          return SUCCESS;
              }


              
          private boolean isInvalid(String value) {
                  
          return (value == null || value.length() == 0);
              }


              
          private String username;

              
          public String getUsername() {
                  
          return username;
              }


              
          public void setUsername(String username) {
                  
          this.username = username;
              }


              
          private String password;

              
          public String getPassword() {
                  
          return password;
              }


              
          public void setPassword(String password) {
                  
          this.password = password;
              }


          }


          當(dāng)工程變得愈發(fā)復(fù)雜時(shí),這一小段一小段驗(yàn)證代碼將會(huì)變得難以維護(hù),出于此原因,我們可以考慮采用struts2提供的驗(yàn)證機(jī)制來(lái)實(shí)現(xiàn)。在src目錄下建立實(shí)現(xiàn)驗(yàn)證的xml文件Login-validation.xml,為Login Action中的usernamepassword屬性增加非空驗(yàn)證,驗(yàn)證配置如下:

          <!DOCTYPE validators PUBLIC
                  "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
                  "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"
          >

          <validators>
              
          <field name="username">
                  
          <field-validator type="requiredstring">
                      
          <message key="requiredstring"/>
                  
          </field-validator>
              
          </field>
              
          <field name="password">
                  
          <field-validator type="requiredstring">
                      
          <message key="requiredstring"/>
                  
          </field-validator>
              
          </field>
          </validators>

          當(dāng)驗(yàn)證未通過(guò)時(shí),將不會(huì)進(jìn)入Login Action中的execute方法,此時(shí)可刪除掉Login這個(gè)Action中的驗(yàn)證內(nèi)容,該類(lèi)的execute方法直接跳轉(zhuǎn)到SUCCESS即可,修改后的代碼略。

          4. 本地化輸出——資源文件package.properties

          為了本地化的輸出驗(yàn)證錯(cuò)誤信息,我們可以將參數(shù)信息和錯(cuò)誤信息放入資源文件中,資源文件package.properties位于src/example目錄下,內(nèi)容如下:

          requiredstring = ${getText(fieldName)}不能為空.

          password = 密碼

          username = 用戶(hù)名

          Missing.message = 該部分尚未構(gòu)建,請(qǐng)稍候訪(fǎng)問(wèn)...

          src/example目下下建立對(duì)應(yīng)的中文資源文件package_zh_CN.properties,為了避免中文亂碼問(wèn)題,我們編寫(xiě)一個(gè)批處理文件code.bat來(lái)對(duì)package.properties進(jìn)行編碼處理,主要用到native2ascii命令,其內(nèi)容如下:

          del package_zh_CN.properties

          copy package.properties package_zh_CN.properties.gbk

          native2ascii -encoding GBK package_zh_CN.properties.gbk package_zh_CN.properties

          del package_zh_CN.properties.gbk

          del *.bak

          運(yùn)行該批處理文件,可在package_zh_CN.properties中可看到編碼后的資源文件信息,如下:

          requiredstring = ${getText(fieldName)}\u4e0d\u80fd\u4e3a\u7a7a.

          password = \u5bc6\u7801

          username = \u7528\u6237\u540d

          Missing.message = \u8be5\u90e8\u5206\u5c1a\u672a\u6784\u5efa\uff0c\u8bf7\u7a0d\u5019\u8bbf\u95ee...

          5. Register.jspMissing.jsp的編寫(xiě)

          2中的example.xml中,我們配置了通配符映射,在Welcome.jsp中,我們使用

          <a href="<s:url action="Register"/>">注冊(cè)</a>

          其中的Registerexample中找不到相關(guān)映射,于是在用戶(hù)點(diǎn)擊“注冊(cè)”按鈕時(shí),將映射到通配符所映射的Actionexample. ExampleSupport.而后跳轉(zhuǎn)到Register.jsp頁(yè)面,其代碼如下:
          <%@ page contentType="text/html; charset=UTF-8" %>
          <%@ taglib prefix="s" uri="/struts-tags" %>
          <s:include value="Missing.jsp"/>

          該頁(yè)面包含Missing.jsp頁(yè)面,其代碼如下:

          <%@ page contentType="text/html; charset=UTF-8" %>
          <%@ taglib prefix="s" uri="/struts-tags" %>
          <html>
          <head><title>未構(gòu)建頁(yè)面</title></head>

          <body>
          <p>
              
          <!-- 讀取配置文件中的對(duì)應(yīng)信息. -->
              
          <s:text name="Missing.message"/>
          </p>
          </body>
          </html>

          四.總結(jié)

           到此為止,我們的實(shí)例已構(gòu)建完,可通過(guò)瀏覽器訪(fǎng)問(wèn):http://localhost:8080/struts2-blank-2.0.8/example/Welcome.jsp看到我們的勞動(dòng)成果。下面總結(jié)一下我們?cè)谠搶?shí)例中學(xué)到的東西。

          1. 常用的一些標(biāo)簽的使用

          在本實(shí)例中,我們學(xué)習(xí)了<s:textfield><s:password><s:form>以及<s:url>的使用。

          2. 表單數(shù)據(jù)的驗(yàn)證

          本文描述了一個(gè)簡(jiǎn)單的表單的數(shù)據(jù)非空驗(yàn)證。

          3. 本地化輸出錯(cuò)誤提示信息

          在驗(yàn)證失敗時(shí),用資源文件來(lái)輸出錯(cuò)誤提示信息。

          4Strut2配置文件中通配符的使用

          對(duì)于某些地址,我們可以使用在struts2的配置通配符來(lái)使其映射到某個(gè)Action中去。

          下一篇:實(shí)例篇:struts2-mailreader-2.0.8實(shí)例學(xué)習(xí)

          上一篇:基礎(chǔ)篇:本地化輸出

          posted on 2007-08-02 15:25 阿蜜果 閱讀(10789) 評(píng)論(13)  編輯  收藏 所屬分類(lèi): Struts2


          FeedBack:
          # re: Struts2學(xué)習(xí)進(jìn)階(實(shí)例篇一)——struts2-blank-2.0.8實(shí)例學(xué)習(xí)
          2007-08-03 11:59 | ehe
          hehe....  回復(fù)  更多評(píng)論
            
          # re: Struts2學(xué)習(xí)進(jìn)階(實(shí)例篇一)——struts2-blank-2.0.8實(shí)例學(xué)習(xí)
          2007-08-03 17:11 | Thomas
          非常棒,繼續(xù)努力,加油!  回復(fù)  更多評(píng)論
            
          # re: Struts2學(xué)習(xí)進(jìn)階(實(shí)例篇一)——struts2-blank-2.0.8實(shí)例學(xué)習(xí)
          2007-08-06 11:39 | 五星-六日
          摟主寫(xiě)的東西對(duì)我的工作幫助很大。感謝的說(shuō)  回復(fù)  更多評(píng)論
            
          # re: Struts2學(xué)習(xí)進(jìn)階(實(shí)例篇一)——struts2-blank-2.0.8實(shí)例學(xué)習(xí)
          2007-08-22 12:15 | 何紹親
          很好,我也正在學(xué)習(xí)  回復(fù)  更多評(píng)論
            
          # re: Struts2學(xué)習(xí)進(jìn)階(實(shí)例篇一)——struts2-blank-2.0.8實(shí)例學(xué)習(xí)
          2007-08-29 16:21 | 阿干
          不錯(cuò)。。。!
          謝謝 謝星星。!
          ^_^!
          luky-52@163.com  回復(fù)  更多評(píng)論
            
          # re: Struts2學(xué)習(xí)進(jìn)階(實(shí)例篇一)——struts2-blank-2.0.8實(shí)例學(xué)習(xí)
          2007-09-04 16:42 | tlb
          我覺(jué)得,因該把學(xué)習(xí)中的BUG提出來(lái),我學(xué)習(xí)這個(gè)例子遇到好多困難,最后還是各個(gè)擊破了。現(xiàn)在spring struts2 結(jié)合起來(lái),遇到問(wèn)題,怎樣定制錯(cuò)誤葉面,根據(jù)exception 不同而現(xiàn)實(shí)msg,有興趣可以討論下 tlbfpx@163.com  回復(fù)  更多評(píng)論
            
          # re: Struts2學(xué)習(xí)進(jìn)階(實(shí)例篇一)——struts2-blank-2.0.8實(shí)例學(xué)習(xí)
          2007-09-22 08:24 | 阿信
          樓主JJ,你好啊,有幾個(gè)問(wèn)題不懂,向你請(qǐng)教。

          1:Welcome.jsp中,
          <link href="<s:url value="/css/examplecss"/>" rel="stylesheet" type="text/css"/>
          是不是要找一個(gè)CSS文件,好象沒(méi)有啊。

          2:example.xml中的{1}代表什么意思啊?
          method="{1}" ,<result>/example/{1}.jsp</result>

          3:為什么Login.jsp中的<s:form action="Login">的action名字和 .xml文件中<action name="Login_input" method="{1}" class="example.Login">不一樣呢。  回復(fù)  更多評(píng)論
            
          # re: Struts2學(xué)習(xí)進(jìn)階(實(shí)例篇一)——struts2-blank-2.0.8實(shí)例學(xué)習(xí)
          2007-10-10 20:59 | lancy
          提出樓主給的這個(gè)例子的幾個(gè)錯(cuò)誤:驗(yàn)證的xml文件應(yīng)該放在包中;那個(gè)method={1}在這里應(yīng)該去掉,否則出錯(cuò);還有就是jsp中的那個(gè)action應(yīng)該與xml中的action中的那個(gè)name一樣,否則也會(huì)出錯(cuò)。
            回復(fù)  更多評(píng)論
            
          # re: Struts2學(xué)習(xí)進(jìn)階(實(shí)例篇一)——struts2-blank-2.0.8實(shí)例學(xué)習(xí)[未登錄](méi)
          2007-10-11 08:45 | 阿蜜果
          @lancy
          這篇是對(duì)struts2.0.8自帶的實(shí)例struts2-blank-2.0.8學(xué)習(xí)而寫(xiě),驗(yàn)證的xml文件應(yīng)該放在包中,忘了說(shuō)明了,呵呵
          那個(gè)method={1}的是要去掉的,寫(xiě)文章的時(shí)候忘了
          多謝提醒阿  回復(fù)  更多評(píng)論
            
          # re: Struts2學(xué)習(xí)進(jìn)階(實(shí)例篇一)——struts2-blank-2.0.8實(shí)例學(xué)習(xí)
          2007-11-01 18:29 | 中華信鴿
          寫(xiě)的真不錯(cuò)呀.  回復(fù)  更多評(píng)論
            
          # re: Struts2學(xué)習(xí)進(jìn)階(實(shí)例篇一)——struts2-blank-2.0.8實(shí)例學(xué)習(xí)
          2007-11-10 16:33 | arewei
          很不錯(cuò)!
          謝謝啦!!  回復(fù)  更多評(píng)論
            
          # re: Struts2學(xué)習(xí)進(jìn)階(實(shí)例篇一)——struts2-blank-2.0.8實(shí)例學(xué)習(xí)
          2008-03-19 18:12 | tarzan
          謝謝,做得很好  回復(fù)  更多評(píng)論
            
          # re: Struts2學(xué)習(xí)進(jìn)階(實(shí)例篇一)——struts2-blank-2.0.8實(shí)例學(xué)習(xí)
          2008-03-20 09:30 | df
          @ehe
          hehe  回復(fù)  更多評(píng)論
            
          <2007年8月>
          2930311234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

                生活將我們磨圓,是為了讓我們滾得更遠(yuǎn)——“圓”來(lái)如此。
                我的作品:
                玩轉(zhuǎn)Axure RP  (2015年12月出版)
                

                Power Designer系統(tǒng)分析與建模實(shí)戰(zhàn)  (2015年7月出版)
                
               Struts2+Hibernate3+Spring2   (2010年5月出版)
               

          留言簿(263)

          隨筆分類(lèi)

          隨筆檔案

          文章分類(lèi)

          相冊(cè)

          關(guān)注blog

          積分與排名

          • 積分 - 2296368
          • 排名 - 3

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 苏尼特左旗| 平罗县| 惠水县| 隆化县| 远安县| 万全县| 常山县| 高尔夫| 上饶县| 鱼台县| 堆龙德庆县| 朝阳市| 多伦县| 运城市| 台北市| 通渭县| 磴口县| 古浪县| 凯里市| 临湘市| 泰州市| 玉龙| 兴山县| 宣城市| 沾益县| 嘉荫县| 德惠市| 兴安县| 鄂伦春自治旗| 霞浦县| 岑溪市| 鸡西市| 无棣县| 沁源县| 绥化市| 修武县| 永仁县| 禄丰县| 威信县| 年辖:市辖区| 鄂温|