隨筆-34  評論-1965  文章-0  trackbacks-0
          All Input Is Evil!
          -Writing secure code

          在寫前幾篇文章的時候,有些朋友建議我的寫一篇關(guān)于表單數(shù)據(jù)校驗的文章。 正如文章的開頭所引用的《Writing Secure Code》的名言:“所有的輸入都是罪惡的”,所以我們應(yīng)該對所有的外部輸入進(jìn)行校驗。而表單是應(yīng)用程序最簡單的入口,對其傳進(jìn)來的數(shù)據(jù),我們必須進(jìn)行校驗。

          轉(zhuǎn)換與校驗(Conversion & Validation)

          其實上篇文章,我本來是打算寫表單數(shù)據(jù)校驗的內(nèi)容,但是經(jīng)過再三思考后,還是決定先寫Struts 2.0轉(zhuǎn)換器的內(nèi)容。原因是我認(rèn)為轉(zhuǎn)換是校驗的基礎(chǔ),只有在數(shù)據(jù)被正確地轉(zhuǎn)換成其對應(yīng)的類型后,我們才可以對其取值范圍進(jìn)行校驗。看個例子相信大家可以更清楚。現(xiàn)在我們就來改造一下《轉(zhuǎn)換器(Converter)——Struts 2.0中的魔術(shù)師》的第一個例子。

          首先,從Action開始,修改后的代碼如下:

          package tutorial;

          import java.util.Locale;

          import com.opensymphony.xwork2.ActionSupport;
          import com.opensymphony.xwork2.util.LocalizedTextUtil;

          public class HelloWorld extends ActionSupport {
          ? ?
          private String msg;
          ? ?
          private Locale loc = Locale.US;
          ? ?
          ? ?
          public String getMsg() {
          ? ? ? ?
          return msg; ? ? ? ?
          ? ?}

          ? ?
          ? ?
          public Locale getLoc() {
          ? ? ? ?
          return loc;
          ? ?}

          ? ?
          ? ?
          public void setLoc(Locale loc) {
          ? ? ? ?
          this .loc = loc;
          ? ?}

          ? ?
          ? ?@Override
          ? ?
          public void validate() {
          ? ? ? ?System.out.println(
          " Calling validate() " );
          ? ? ? ?
          if ( ! (loc.equals(Locale.US) || loc.equals(Locale.CHINA))) {
          ? ? ? ? ? ? ? ? ? ?addFieldError(
          " loc " , getText( " validation.loc " ));
          ? ? ? ?}

          ? ?}

          ? ? ? ?
          ? ?
          public void validateExecute() {
          ? ? ? ?System.out.println(
          " Calling validateExecute() by reflection " );
          ? ?}

          ? ?
          ? ?@Override
          ? ?
          public String execute() {
          ? ? ? ?System.out.println(
          " Calling execute() " );
          ? ? ? ?
          // LocalizedTextUtil是Struts 2.0中國際化的工具類,<s:text>標(biāo)志就是通過調(diào)用它實現(xiàn)國際化的
          ?? ?? ? ? ?msg = LocalizedTextUtil.findDefaultText( " HelloWorld " , loc);
          ? ? ? ?
          return SUCCESS;
          ? ?}

          }

          然后,修改Struts.xml中Action的定義指明輸入地址:

          < action name ="HelloWorld" class ="tutorial.HelloWorld" >
          ? ?
          < result > /HelloWorld.jsp </ result >
          ? ?
          < result name ="input" > /HelloWorld.jsp </ result >
          </ action >

          接著,在HelloWorld.jsp中加入錯誤提示:

          <% @ page ?contentType = " text/html; charset=UTF-8 " %>
          <% @taglib prefix = " s " uri = " /struts-tags " %>
          < html >
          < head >
          ? ?
          < title > Hello World </ title >
          </ head >
          < body >
          ? ?
          < div style ="color:red;" >
          ? ? ? ?
          < s:fielderror />
          ? ?
          </ div >
          ? ?
          < s:form action ="HelloWorld" theme ="simple" > ? ? ? ? ? ?
          ? ? ? ? Locale:
          < s:textfield name ="loc" /> &nbsp; < s:submit />
          ? ?
          </ s:form > ? ?
          ? ?
          < h2 >< s:property value ="msg" /></ h2 >
          </ body >
          </ html >

          再修改LocaleConverter.java文件,將內(nèi)容改為:

          package tutorial;

          import java.util.Locale;
          import java.util.Map;
          import java.util.regex.Pattern;

          public class LocaleConverter extends ognl.DefaultTypeConverter {
          ? ?@Override
          ? ?
          public Object convertValue(Map context, Object value, Class toType) {
          ? ? ? ?
          if (toType == Locale. class ) { ? ? ? ? ? ?
          ? ? ? ? ? ?System.out.println(
          " Converting String to Locale " );
          ? ? ? ? ? ?String locale
          = ((String[]) value)[ 0 ];
          ? ? ? ? ? ?
          return new Locale(locale.substring( 0 , 2 ), locale.substring( 3 ));
          ? ? ? ?}
          else if (toType == String. class ) {
          ? ? ? ? ? ?System.out.println(
          " Converting Locale to String " );
          ? ? ? ? ? ?Locale locale
          = (Locale) value;
          ? ? ? ? ? ?
          return locale.toString();
          ? ? ? ?}

          ? ? ? ?
          return null ;
          ? ?}

          }

          之后,修改國際化資源文件,內(nèi)容為:

          HelloWorld = 你好,世界!
          invalid.fieldvalue.loc
          = Locale必須為\ " xx_XX\ " 的格式
          validation.loc
          = 區(qū)域必須為中國或美國
          globalMessages_zh_CN.properties

          HelloWorld = Hello World!
          invalid.fieldvalue.loc
          = Locale must like \ " xx_XX\ "
          validation.loc
          = Locale must be China or USA
          globalMessages_en_US.properties

          發(fā)布運(yùn)行應(yīng)用程序,在瀏覽器中鍵入http://localhost:8080/Struts2_Validation/HelloWorld.action,在Locale中輸入zh_CN,按“Submit”提交,效果如上篇文章所示。而在服務(wù)器控制臺有如下輸出:

          Converting String to Locale...
          Calling validateExecute() by reflection...
          Calling validate()...
          Calling execute()...
          Converting Locale to String...

          上述的輸出說明了Struts 2.0的數(shù)據(jù)校驗工作方式,它需要經(jīng)過下面幾個步驟:

          1. 通過轉(zhuǎn)換器將請求參數(shù)轉(zhuǎn)換成相應(yīng)的Bean屬性;
          2. 判斷轉(zhuǎn)換過程是否出現(xiàn)異常。如果有,則將其保存到ActionContext中,conversionError攔截器再封裝為fieldError;如果沒有,進(jìn)行下一步;
          3. 通過反射(Reflection)來調(diào)用validateXxx()方法(其中,Xxx表示Action的方法名);
          4. 調(diào)用validate()方法;
          5. 如果經(jīng)過上述步驟沒有出現(xiàn)fieldError,則調(diào)用Action方法;如果有,則會跳過Action方法,通過國際化將fieldError輸出到頁面。

          不喜歡看文字的朋友,可以參考下面的圖1。

          圖1 校驗順序圖
          圖1 校驗順序圖

          看到這里可能大家會疑問:“這么多地方可以校驗表單數(shù)據(jù),到底我應(yīng)該在那里做呢?”有選擇是好事,但抉擇的過程往往是痛苦的,往往讓人不知所措。如果大家參照以下幾點(diǎn)建議,相信會比較容易地做出正確的抉擇。

          1. 如果需要轉(zhuǎn)換的數(shù)據(jù),通常做法在轉(zhuǎn)換的時候做格式的校驗,在Action中的校驗方法中校驗取值。假如用戶填錯了格式,我們可以通過在資源文件配置invalid.fieldvalue.xxx(xxx為屬性名)來提示用戶正確的格式,不同的階段出錯顯示不同的信息。具體做法請參考上面的例子;
          2. 至于用validate()還是validateXxx(),我推薦使用validate()。原因是validateXxx()使用了反射,相對來說性能稍差,而validate()則是通過接口com.opensymphony.xwork2.Validateable調(diào)用。當(dāng)然如果你的表單數(shù)據(jù)取值是取決于特定Action方法,則應(yīng)該使用validateXxx()。

          在運(yùn)行上面的例子時,在Locale中輸入zh并提交時出現(xiàn)圖2所示頁面。

          圖2 錯誤格式
          圖2 錯誤格式

          在Locale中輸入de_DE時,出現(xiàn)如圖3所示頁面。

          圖3 取值錯誤
          圖3 取值錯誤

          使用Struts 2.0的校驗框架

          上一節(jié)的內(nèi)容都是關(guān)于如何編程實現(xiàn)校驗,這部分工作大都是單調(diào)的重復(fù)。更多情況下,我們使用Struts 2.0的校驗框架,通過配置實現(xiàn)一些常見的校驗。

          我學(xué)習(xí)編程有個習(xí)慣——喜歡先看輸出結(jié)果,再看代碼實現(xiàn)。這樣學(xué)的好處是先看結(jié)果可以刺激學(xué)習(xí)的激情,也可以在看代碼前自已思考一下如何實現(xiàn),然后帶著問題去看代碼,那就清晰多了。因此下面我們先來做演示。

          首先,在tutorial包下新建ValidationAction.java,代碼如下:

          package tutorial;

          import com.opensymphony.xwork2.ActionSupport;

          public class ValidationAction extends ActionSupport {
          ? ?
          private String reqiuredString;

          ? ?
          public String getReqiuredString() {
          ? ? ? ?
          return reqiuredString;
          ? ?}


          ? ?
          public void setReqiuredString(String reqiuredString) {
          ? ? ? ?
          this .reqiuredString = reqiuredString;
          ? ?}

          ? ?
          ? ?@Override
          ? ?
          public String execute() {
          ? ? ? ?
          return SUCCESS;
          ? ?}
          ? ?
          }

          然后,配置上述所建的Ation,代碼片段如下:

          < action name ="ValidationAction" class ="tutorial.ValidationAction" >
          ? ?
          < result > /Output.jsp </ result >
          ? ?
          < result name ="input" > /Input.jsp </ result >
          </ action >

          接著,創(chuàng)建Input.jsp和Output.jsp,內(nèi)容分別如下:

          <% @ page ?contentType = " text/html; charset=UTF-8 " %>
          <% @taglib prefix = " s " uri = " /struts-tags " %>
          < html >
          < head >
          ? ?
          < title > Hello World </ title >
          ? ?
          <!-- 此標(biāo)志的作用是引入Struts 2.0的常用的Javascript和CSS -->
          ? ?
          < s:head />
          </ head >
          < body >
          ? ?
          < s:form action ="ValidationAction" > ? ? ? ? ? ?
          ? ? ? ?
          < s:textfield name ="reqiuredString" label ="Required String" />
          ? ? ? ?
          < s:submit />
          ? ?
          </ s:form > ? ?
          </ body >
          </ html >
          Input.jsp

          <% @ page ?contentType = " text/html; charset=UTF-8 " %>
          <% @taglib prefix = " s " uri = " /struts-tags " %>
          < html >
          < head >
          ? ?
          < title > Hello World </ title >
          </ head >
          < body >
          ? ? Required String:
          < s:property value ="reqiuredString" /> ? ?
          </ body >
          </ html >
          Output.jsp

          再接下來,在tutorial包下創(chuàng)建ValidationAction的校驗配置文件Xxx-validation.xml(Xxx為Action的類名),在本例中該文件名ValidationAction-validation.xml,內(nèi)容如下:

          <? xml version="1.0" encoding="UTF-8" ?>
          <! DOCTYPE validators PUBLIC?
          ? ? ? ? ? "-//OpenSymphony Group//XWork Validator 1.0//EN"?
          ? ? ? ? ? "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"
          > ? ? ? ? ?
          < validators >
          ? ?
          < field name ="reqiuredString" >
          ? ? ? ?
          < field-validator type ="requiredstring" >
          ? ? ? ? ? ?
          < message > This string is required </ message >
          ? ? ? ?
          </ field-validator >
          ? ?
          </ field >
          </ validators >

          發(fā)布運(yùn)行應(yīng)用程序,在地址欄中鍵入http://localhost:8080/Struts2_Validation/Input.jsp,出現(xiàn)如圖4所示頁面。

          圖4 Input.jsp
          圖4 Input.jsp

          直接點(diǎn)擊“Submit”提交表單,出現(xiàn)圖5所示的頁面。

          圖5 錯誤提示
          圖5 錯誤提示

          在Required String中隨便填點(diǎn)東西,轉(zhuǎn)到Output.jsp頁面,如圖6所示。

          圖6 Output.jsp
          圖6 Output.jsp

          通過上面的例子,大家可以看到使用該校驗框架十分簡單方便。不過,上例還有兩點(diǎn)不足:

          1. 還沒有國際化錯誤消息;
          2. 沒有實現(xiàn)客戶端的校驗。

          當(dāng)然,要完善以上不足,對于Struts 2.0來說,只是小菜一碟。

          1. 在Xxx-validation.xml文件中的<message>元素中加入key屬性;
          2. 在Input.jsp中的<s:form>標(biāo)志中加入validate="true"屬性,就可以在用Javascript在客戶端校驗數(shù)據(jù)。

          下面是具體的實現(xiàn),首先在國際化資源文件中加入錯誤消息,然后按照上面說明實現(xiàn)。因為要使用Javascript在客戶端顯示出錯信息,所以在加載Input.jsp頁面時,Struts 2.0需要獲得國際化的字符串,故我們需要使用Action來訪問Input.jsp頁面,具體實現(xiàn)請參考《在Struts 2.0中國際化(i18n)您的應(yīng)用程序》的最后部分。最后發(fā)布運(yùn)行應(yīng)用程序,直接在頁面中點(diǎn)擊“Submit”,表單沒有被提交并出現(xiàn)錯誤提示,如圖7所示:

          圖7 客戶端校驗
          圖7 客戶端校驗

          校驗框架是通過validation攔截器實現(xiàn),該攔載被注冊到默認(rèn)的攔截器鏈中。它在conversionError攔截器之后,在validateXxx()之前被調(diào)用。這里又出現(xiàn)了一個選擇的問題:到底是應(yīng)該在action中通過validateXxx()或validate()實現(xiàn)校驗,還是使用validation攔截器?絕大多數(shù)情況,我建議大家使用校驗框架,只有當(dāng)框架滿足不了您的要求才自已編寫代碼實現(xiàn)。

          配置文件查找順序

          在上面的例子中,我們通過創(chuàng)建ValidationAction-validation.xml來配置表單校驗。Struts 2.0的校驗框架自動會讀取該文件,但這樣就會引出一個問題——如果我的Action繼承其它的Action類,而這兩個Action類都需要對表單數(shù)據(jù)進(jìn)行校驗,那我是否會在子類的配置文件(Xxx-validation.xml)中復(fù)制父類的配置嗎?

          答案是不,因為Struts 2.0的校驗框架跟《在Struts 2.0中國際化(i18n)您的應(yīng)用程序》提到的“資源文件查找順序”相似,有特定的配置文件查找順序。不同的是校驗框架按照自上而下的順序在類層次查找配置文件。假設(shè)以下條件成立:

          1. 接口 Animal;
          2. 接口 Quadraped 擴(kuò)展了 Animal;
          3. 類 AnimalImpl 實現(xiàn)了 Animal;
          4. 類 QuadrapedImpl 擴(kuò)展了 AnimalImpl 實現(xiàn)了 Quadraped;
          5. 類 Dog 擴(kuò)展了 QuadrapedImpl;

          如果Dog要被校驗,框架方法會查找下面的配置文件(其中別名是Action在struts.xml中定義的別名):

          1. Animal-validation.xml
          2. Animal-別名-validation.xml
          3. AnimalImpl-validation.xml
          4. AnimalImpl-別名-validation.xml
          5. Quadraped-validation.xml
          6. Quadraped-別名-validation.xml
          7. QuadrapedImpl-validation.xml
          8. QuadrapedImpl-別名-validation.xml
          9. Dog-validation.xml
          10. Dog-別名-validation.xml

          已有的校驗器

          Struts 2.0已經(jīng)為您實現(xiàn)很多常用的校驗了,以下在jar的default.xml中的注冊的校驗器。

          < validators >
          ? ?
          < validator name ="required" class ="com.opensymphony.xwork2.validator.validators.RequiredFieldValidator" />
          ? ?
          < validator name ="requiredstring" class ="com.opensymphony.xwork2.validator.validators.RequiredStringValidator" />
          ? ?
          < validator name ="int" class ="com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator" />
          ? ?
          < validator name ="double" class ="com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator" />
          ? ?
          < validator name ="date" class ="com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator" />
          ? ?
          < validator name ="expression" class ="com.opensymphony.xwork2.validator.validators.ExpressionValidator" />
          ? ?
          < validator name ="fieldexpression" class ="com.opensymphony.xwork2.validator.validators.FieldExpressionValidator" />
          ? ?
          < validator name ="email" class ="com.opensymphony.xwork2.validator.validators.EmailValidator" />
          ? ?
          < validator name ="url" class ="com.opensymphony.xwork2.validator.validators.URLValidator" />
          ? ?
          < validator name ="visitor" class ="com.opensymphony.xwork2.validator.validators.VisitorFieldValidator" />
          ? ?
          < validator name ="conversion" class ="com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator" />
          ? ?
          < validator name ="stringlength" class ="com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator" />
          ? ?
          < validator name ="regex" class ="com.opensymphony.xwork2.validator.validators.RegexFieldValidator" />
          </ validators >
          關(guān)于每個校驗器的具體用法,請參考以下鏈接:
          http://wiki.javascud.org/display/ww2cndoc/Validation
          該鏈接中還有一些很有的信息,請大家仔細(xì)閱讀。

          總結(jié)

          使用校驗框架既可以方便地實現(xiàn)表單數(shù)據(jù)校驗,又能夠?qū)⑿r炁cAction分離,故我們應(yīng)該盡可能使用校驗框架。在使用校驗框架時,請不要忘記通過在資源文件加入invalid.fieldvalue.xxx字符串,顯示適合的類型轉(zhuǎn)換出錯信息;或者使用conversion校驗器。

          posted on 2006-11-14 13:38 Max 閱讀(51499) 評論(118)  編輯  收藏 所屬分類: Struts 2.0系列
          評論共2頁: 上一頁 1 2 

          評論:
          # re: 在Struts 2.0中實現(xiàn)表單數(shù)據(jù)校驗(Validation) 2006-11-15 15:51 | ronghai
          你好怎么樣才能聯(lián)系到你,好好了解一些struts2。發(fā)現(xiàn)1.X和2有太大的差別了,剛把1.x弄得差不多,看2覺得很不是理解。  回復(fù)  更多評論
            
          # re: 在Struts 2.0中實現(xiàn)表單數(shù)據(jù)校驗(Validation) 2006-11-16 09:01 | Max
          你可以通過email聯(lián)系我, max.m.yuan@gmail.com  回復(fù)  更多評論
            
          # re: 在Struts 2.0中實現(xiàn)表單數(shù)據(jù)校驗(Validation) 2006-11-22 13:53 | 太行山人
          正在學(xué)2.0,您寫的太好了,收藏下來仔細(xì)研讀  回復(fù)  更多評論
            
          # re: 在Struts 2.0中實現(xiàn)表單數(shù)據(jù)校驗(Validation) 2006-11-27 11:43 | breezedancer
          寫的很好,轉(zhuǎn)到我的論壇www.51forum.uni.cc,如果有什么不當(dāng),請聯(lián)系管理員,謝謝~~~  回復(fù)  更多評論
            
          # re: 在Struts 2.0中實現(xiàn)表單數(shù)據(jù)校驗(Validation) 2006-12-04 22:28 | zjlovezj
          謝謝樓主,hello world是通往實際應(yīng)用的必由之路~

          冒昧提多個要求:
          請寫寫spring2 和 struts2結(jié)合起來使用的HELLO WORLD,最好加上個hibernate3.2  回復(fù)  更多評論
            
          # re: 在Struts 2.0中實現(xiàn)表單數(shù)據(jù)校驗(Validation) 2006-12-06 01:30 | lhj
          嗯,,同樓上,希望老大弄個spring2和struts2甚至hibernate結(jié)合的例子,重點(diǎn)講下配置要點(diǎn)和原理,呵呵,不情之請,見諒!  回復(fù)  更多評論
            
          # re: 在Struts 2.0中實現(xiàn)表單數(shù)據(jù)校驗(Validation) 2006-12-06 09:35 | Max
          謝謝大家的建議,我計劃在以后文章中介紹Struts 2與一些流行的框架的搭配。  回復(fù)  更多評論
            
          # re: 在Struts 2.0中實現(xiàn)表單數(shù)據(jù)校驗(Validation) 2006-12-06 10:32 | sxf
          寫的好,加油!  回復(fù)  更多評論
            
          # re: 在Struts 2.0中實現(xiàn)表單數(shù)據(jù)校驗(Validation) 2007-02-01 15:59 | Sophia
          Max,最近在嘗試Struts2的Validation Annotation。發(fā)現(xiàn)@Annotation是需要引用一個com.opensymphony.xwork2.validator.annotations包下的@interface Validation。這個package被打到了xwork-2.0-beta-1.jar里面,但是struts官網(wǎng)上提供的jar里面卻并未含有這個包。
          我在網(wǎng)上找了一下,在http://ivyrep.opensymphony.com/opensymphony/xwork/ 下面找了個最新的xwork-2.0.0.jar 來用。但是tomcat啟動的時候,報錯struts - cleanup之類的。換回原來的xwork-2.0-beta-1.jar就沒問題。
          想請教一下,用Annotation方法如何實現(xiàn)Validation?
          謝謝。  回復(fù)  更多評論
            
          # re: 在Struts 2.0中實現(xiàn)表單數(shù)據(jù)校驗(Validation) 2007-02-02 12:51 | Sophia
          補(bǔ)充一下,所謂的struts - cleanup報錯是因為在web.xml中配置了這個filter。
          同樣web.xml中配置的struts2的filter也會出錯。
          <code>
          <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>
          </code>
          那個xwork-2.0.0.jar是webwork提供的。
          我將需要用到的幾個包單獨(dú)拉出來打包就可以了。
          com.opensymphony.xwork2.validator
          com.opensymphony.xwork2.validator.annotations
          com.opensymphony.xwork2.validator.metadata
          com.opensymphony.xwork2.validator.validators
          看來是beta版的原因啊!  回復(fù)  更多評論
            
          # re: 在Struts 2.0中實現(xiàn)表單數(shù)據(jù)校驗(Validation) 2007-02-02 12:52 | Sophia
          sorry, 不會貼代碼,嗬嗬,那個<code></code>請省略不看  回復(fù)  更多評論
            
          # re: 在Struts 2.0中實現(xiàn)表單數(shù)據(jù)校驗(Validation) 2007-02-13 01:21 | mos
          如何在struts2中使用js呢?  回復(fù)  更多評論
            
          # re: 在Struts 2.0中實現(xiàn)表單數(shù)據(jù)校驗(Validation) 2007-03-02 16:13 | z
          test
            回復(fù)  更多評論
            
          # re: 在Struts 2.0中實現(xiàn)表單數(shù)據(jù)校驗(Validation) 2007-03-06 16:02 | mz
          請問報這個錯是哪沒有配好?

          Method public java.lang.String com.opensymphony.xwork2.validator.validators.ValidatorSupport.getMessage(java.lang.Object) threw an exception when invoked on com.opensymphony.xwork2.validator.validators.RequiredStringValidator@156f920
          The problematic instruction:
          ----------
          ==> ${validator.getMessage(action)?js_string} [on line 28, column 26 in template/xhtml/form-close-validate.ftl]
          in include "/${parameters.templateDir}/xhtml/form-close-validate.ftl" [on line 3, column 1 in template/xhtml/form-close.ftl]
          ----------  回復(fù)  更多評論
            
          # re: 在Struts 2.0中實現(xiàn)表單數(shù)據(jù)校驗(Validation) 2007-04-18 17:40 | huhuyeah
          看了這篇文章實驗了一下客戶端驗證
          login.jsp:
          <s:fielderror></s:fielderror>
          <p>&nbsp;</p>
          <s:form action="login" validate="true">
          用戶名:<s:textfield name="username"/>
          密 碼:<s:password name="password" />
          <s:submit/>
          </s:form>

          struts.xml:
          <package name="loginout" extends="hello-default" namespace="/">
          <action name="login" class="com.jetch.action.LoginAction">
          <result name="success">/WEB-INF/jsp/main.jsp</result>
          <result name="input">/WEB-INF/jsp/login.jsp</result>
          <interceptor-ref name="defaultStack"></interceptor-ref>
          </action>
          </package>


          校驗的xml配置就是必須要username跟password
          配置之后,服務(wù)器端的驗證已經(jīng)成功了
          不過奇怪的是,看了幫助文件,在form標(biāo)簽里面加入validate="true" 之后,客戶端的校驗卻不起作用
          生產(chǎn)的源文件沒有js也沒有onsubmit,請幫忙看看原因。謝謝   回復(fù)  更多評論
            
          # re: 在Struts 2.0中實現(xiàn)表單數(shù)據(jù)校驗(Validation) 2007-04-18 22:54 | Max
          @huhuyeah
          你可以忘記在<head></head>中加入<s:head />  回復(fù)  更多評論
            
          # re: 在Struts 2.0中實現(xiàn)表單數(shù)據(jù)校驗(Validation) 2007-04-19 09:16 | huhuyeah
          謝博主,加了這后生產(chǎn)的頁面源碼有JS了,如下
          <script language="JavaScript" type="text/javascript">
          // Dojo configuration
          djConfig = {
          baseRelativePath: "/hello/struts/dojo",
          isDebug: false,
          bindEncoding: "UTF-8",
          debugAtAllCosts: true // not needed, but allows the Venkman debugger to work with the includes
          };
          </script>
          <script language="JavaScript" type="text/javascript"
          src="/hello/struts/dojo/dojo.js"></script>
          <script language="JavaScript" type="text/javascript"
          src="/hello/struts/simple/dojoRequire.js"></script>
          但是客戶端驗證還是沒有成功
          生成的form沒有onsubmit,如果把 validate="true" 去掉。
          在form里面有onsubmit="return true"的js生成
          如何調(diào)用生成的JS?
            回復(fù)  更多評論
            
          # re: 在Struts 2.0中實現(xiàn)表單數(shù)據(jù)校驗(Validation)[未登錄] 2007-04-19 17:42 | test
          把validate="true"這個加上之后會出現(xiàn)
          FreeMarker template error!

          Method public java.lang.String com.opensymphony.xwork2.validator.validators.ValidatorSupport.getMessage(java.lang.Object) threw an exception when invoked on com.opensymphony.xwork2.validator.validators.RequiredStringValidator@10699ea
          The problematic instruction:
          ----------
          ==> ${validator.getMessage(action)?js_string} [on line 28, column 26 in template/xhtml/form-close-validate.ftl]
          in include "/${parameters.templateDir}/xhtml/form-close-validate.ftl" [on line 3, column 1 in template/xhtml/form-close.ftl]
          ----------

          Java backtrace for programmers:
          ----------
          freemarker.template.TemplateModelException: Method public java.lang.String com.opensymphony.xwork2.validator.validators.ValidatorSupport.getMessage(java.lang.Object) threw an exception when invoked on com.opensymphony.xwork2.validator.validators.RequiredStringValidator@10699ea.......  回復(fù)  更多評論
            
          評論共2頁: 上一頁 1 2 
          主站蜘蛛池模板: 田阳县| 浦北县| 富裕县| 西乌| 东兰县| 土默特左旗| 海原县| 蓝田县| 沧源| 巴塘县| 昭通市| 当阳市| 长丰县| 怀柔区| 玉树县| 郯城县| 镶黄旗| 修文县| 东辽县| 宜城市| 麻江县| 灯塔市| 遂溪县| 广南县| 翁牛特旗| 五河县| 舞钢市| 兴义市| 峨眉山市| 巴林右旗| 凤阳县| 临颍县| 常熟市| 柳河县| 通化县| 顺平县| 怀来县| 吉隆县| 五指山市| 如东县| 航空|