呆呆向前沖的blog

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            78 隨筆 :: 43 文章 :: 5 評論 :: 74 Trackbacks

          步驟:

          1 環境配置:首先要為struts加入Validator支持
                lib包:加入validator所需的jar包
                配置文件:struts-config.xml里加入

            <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
              
          <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
            
          </plug-in>

                這里所需的validator-rules.xml和validation.xml也需要放到/WEB-INF/下

          2 配置actionForm
                2.1 修改Form基類:將actionForm繼承的基類改為ValidatorForm      
                2.2 配置struts-config.xml文件:把要驗證的form的validator屬性設為true

          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
          <struts-config>
            
          <form-beans>
              
          <form-bean name="userForm" type="com.satesoft.test.UserForm" />
            
          </form-beans>
            
          <action-mappings>
              
          <action input="/index.jsp" name="userForm" path="/show" scope="request" type="com.satesoft.test.ShowAction" validate="true">
                
          <forward name="forward" path="/show.jsp" />
              
          </action>
            
          </action-mappings>
            
          <message-resources parameter="ApplicationResources" />
            
          <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
              
          <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
            
          </plug-in>
          </struts-config>

                注意:一定要配置對應的Action里的input屬性,否則struts找不到要返回的路徑    

                2.3  配置validator.xml文件:在validation.xml里加入對要驗證的actionform的定義

          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN" "http://jakarta.apache.org/commons/dtds/validator_1_0.dtd">
          <form-validation>
            
          <formset>
              
          <form name="userForm">
                
          <field property="name" depends="required">
                  
          <arg0 key="user.name.required"/>
                
          </field>
              
          </form>
            
          </formset>
          </form-validation>

                這里同時要配置的還有消息資源。在資源文件中加入上述配置文件中所需的消息

          errors.header=<H3><font color="blue">errors</font><br>
          errors.footer=
          <p></p>
          errors.required={0} is required.
          errors.minlength={0} cannot be less than {1} characters.
          errors.maxlength={0} cannot be greater than {1} characters.
          errors.invalid={0} is invalid.
          errors.byte={0} must be a byte.
          errors.short={0} must be a short.
          errors.integer={0} must be an integer.
          errors.long={0} must be a long.
          errors.float={0} must be a float.
          errors.double={0} must be a double.
          errors.date={0} is not a date.
          errors.range={0} is not in the range {1} through {2}.
          errors.creditcard={0} is not a valid credit card number.
          errors.email={0} is an invalid email address
          user.name.required=user name

           2.4 配置作為input的jsp頁面: 

          <%@ page contentType="text/html; charset=GBK" %>
          <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
          <html>
          <head>
          <title>
          index
          </title>
          </head>
          <body bgcolor="#ffffff">
          <html:form action="show.do" method="post">
            
          <html:text property="name"/>
            
          <html:submit title="確定"/>
            
          <html:cancel title="取消">
            
          </html:cancel>
          </html:form>
          <html:errors/>

          </body>
          </html>

                如果需要加入javascript支持,則:

          <%@ page contentType="text/html; charset=GBK" %>
          <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
          <html>
          <head>
          <title>
          index
          </title>
          </head>
          <body bgcolor="#ffffff">
          <html:form action="show.do" method="post" onsubmit="return validateUserForm(this);">
            
          <html:text property="name"/>
            
          <html:submit title="確定"/>
            
          <html:cancel title="取消">
            
          </html:cancel>
          </html:form>
          <html:javascript formName="userForm"/>
          </body>
          </html>

                注意:onsubmit里的validatorUserForm要和下面的formName里的名字一致,只不過首字母大寫


          最后要注意:這只是客戶端自動生成Javascript的驗證。還要在服務器端進行驗證。

          posted on 2005-08-27 22:37 呆呆向前沖的blog 閱讀(551) 評論(0)  編輯  收藏 所屬分類: 工作:Java技術
          主站蜘蛛池模板: 赣州市| 高要市| 仙桃市| 孝感市| 调兵山市| 湘乡市| 秦安县| 社会| 灵丘县| 新巴尔虎右旗| 新绛县| 吕梁市| 黎城县| 新津县| 永川市| 大田县| 安图县| 皋兰县| 南乐县| 东城区| 鱼台县| 柘荣县| 合阳县| 浦东新区| 呼图壁县| 河源市| 蒙自县| 麻阳| 镶黄旗| 德格县| 礼泉县| 财经| 鄂伦春自治旗| 武功县| 临夏县| 鸡泽县| 长阳| 洛阳市| 建宁县| 东乡族自治县| 临漳县|