隨筆 - 170  文章 - 536  trackbacks - 0
          <2006年1月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          常用鏈接

          我參與的團隊

          隨筆分類(103)

          搜索

          •  

          積分與排名

          • 積分 - 413933
          • 排名 - 135

          最新評論

          閱讀排行榜

                   JSF中默認并沒有提供客戶端驗證機制,這就需要使用其他的方法來驗證,Shale已經為我們做好了,通過封裝commonsValidator,實現了非常方便易用的客戶端驗證。

               想必大家對Shale也有所耳聞,在JavaOne2005中的Web Framework的評比中,在Technical Features環節以4.95分的高分超越JSF, Tapestry, WebworkStruts奪得桂冠。(可參考:http://www.aygfsteel.com/steelhand/archive/2005/07/05/7114.aspx

               當然一個優秀的框架可以為我們去做很多,Shale也一樣,它為我們提供了很多實用的特性:View ControllerDialog ManagerDialog ManagerValidationRemotingSpring IntegrationReusable ViewsTest Framework。我們這次就來用用看它的Validation

               因為Shale到現在還沒有正式的release版本,我們可以通過它的Development Releasehttp://svn.apache.org/dist/struts/shale/v1.0.0/)或者Nightly Buildhttp://cvs.apache.org/builds/struts/nightly/struts-shale/)進行,雖然沒有達到產品級的穩定性,但是基本功能已經基本上有了。

          使用ShaleValidation非常簡單,首先,我們得去下一個Commons Validator 1.2http://jakarta.apache.org/site/downloads/downloads_commons-validator.cgi ),過去我們在struts用也可以使用它來進行方便的客戶端驗證,同樣在JSF中也一樣,或許更簡單。

               我們只需要通過簡單的三步,即可完成一個完整的Validation。(當然,所有的包要放在WEB-INF/lib下)

               首先,在<h:form>中加入這條屬性,onsubmit="return validateForm(this);"

               接下來,使用<s:commonsValidator>對頁面指定的元素如<h:inputText>進行驗證。

               最后,在<h:form>前加上<s:validatorScript>

               這樣一個完整的Validation就配好了,是不是很簡單呢?比struts中使用Validator還要方便,因為配置啊什么的都不要做了。

               這樣頁面看起來就像這樣:

          <%@ taglib uri="http://struts.apache.org/shale/core" prefix="s" %>

          <h:form onsubmit="return validateForm(this);">

              
          <h:inputText id="creditCardNumber" 
                                size
          ="16"
                             value
          ="#{userContext.creditCardNumber}">

                  
          <s:commonsValidator type="required"
                                                           arg
          ="#{msgs.creditCardNumberPrompt}"
                                                     server
          ="true"
                                                      client
          ="true"/>

                  
          <s:commonsValidator type="mask"
                                                       mask
          ="[4-6].*"
                                                           arg
          ="#{msgs.creditCardNumberPrompt}"
                                                     server
          ="true"
                                                      client
          ="true"/>

                  
          <s:commonsValidator type="creditCard" 
                                                       arg
          ="#{msgs.creditCardNumberPrompt}" 
                                                     server
          ="true">
              
          </h:inputText> 

              
          <h:message for="creditCardNumber" styleClass="errors"/> 

              
          <s:validatorScript functionName="validateForm"/>
          </h:form>


               <s:commonsValidator>中的type屬性對應了commons Validator中的驗證方法,具體可以參考commons Validator官方網站的相關文檔(http://jakarta.apache.org/commons/validator/index.html

          posted on 2006-01-05 09:25 steady 閱讀(4551) 評論(7)  編輯  收藏 所屬分類: JSF & Myfaces

          FeedBack:
          # re: 在JSF中使用Shale進行客戶端驗證 2006-03-06 10:46 哈哈的日子
          試用一下,JSF 非常需要這個功能。  回復  更多評論
            
          # re: 在JSF中使用Shale進行客戶端驗證 2006-03-06 11:36 哈哈的日子
          對中文的支持有點問題,JSF 會把中文變成類似于 &#20013;&#22269; 這樣的東西,但在 JavaScript 里面的提示信息,這樣的字符無法正確顯示為中文。  回復  更多評論
            
          # re: 在JSF中使用Shale進行客戶端驗證 2006-03-09 22:55 哈哈的日子
          解決方法,在后面加上一段腳本就可以了。還算是無侵入。
          window._alert=window.alert;
          window.alert=function(s) {
          _alert(s.replace(/&#(\d{5});/g,function(r,r1) {return String.fromCharCode(r1);}));
          };  回復  更多評論
            
          # re: 在JSF中使用Shale進行客戶端驗證 2006-07-24 12:45 hd-baby
          那如果我提交的是多語言的,上面的解決方案還成嗎?如果解決?  回復  更多評論
            
          # re: 在JSF中使用Shale進行客戶端驗證 2006-07-24 12:47 hd-baby
          @hd-baby
          指的是驗證的提示信息  回復  更多評論
            
          # re: 在JSF中使用Shale進行客戶端驗證 2007-11-04 01:08 cooc
          我運行你代碼后出現

          org.apache.jasper.JasperException: The absolute uri: http://struts.apache.org/shale/core cannot be resolved in either web.xml or the jar files deployed with this application


          怎么回事啊  回復  更多評論
            
          # re: 在JSF中使用Shale進行客戶端驗證 2007-11-30 16:22 fengbin
          你好!
          我按照你的配置怎么就是跑不了.希望能貼個完整的代碼!  回復  更多評論
            
          主站蜘蛛池模板: 南和县| 永安市| 温州市| 莒南县| 万年县| 白银市| 满城县| 河池市| 钟山县| 南平市| 鹤岗市| 育儿| 拉萨市| 铁力市| 高台县| 台山市| 闵行区| 丹巴县| 武川县| 大冶市| 西安市| 长白| 邢台市| 五华县| 安龙县| 巴马| 开鲁县| 栾城县| 陈巴尔虎旗| 大足县| 剑阁县| 安乡县| 夏津县| 襄城县| 荆州市| 桐庐县| 广平县| 札达县| 龙口市| 邢台县| 望城县|