回 帖 发 新 帖 刷新版面

主题:struts1.2验证问题

validation.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE form-validation PUBLIC
          "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN"
          "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">
          
  <form-validation>
      <formset>
          <form name="loginForm">
              <field property="name" depends="required">
                  <arg0 key="name"/>
              </field>
              <field property="password" depends="required">
                  <arg0 key="password"/>
              </field>
          </form>
      </formset>
      
      <formset>
          <form name="CheckForm">
              <field property="userName" depends="required">
                  <arg0 key="userName"/>
              </field>
              <field property="passWord" depends="required">
                  <arg0 key="passWord"/>    
              </field>
              <field property="age" depends="rang">
                  <arg0 key="age"/>
                  <arg1 key="18" resource="false"/>
                  <arg2 key="30" resource="false"/>
                  <var>
                      <var-name>min</var-name>
                      <var-value>18</var-value>
                  </var>
                  <var>
                      <var-name>max</var-name>
                      <var-value>30</var-value>
                  </var>
              </field>
              <field property="email" depends="email">
                  <arg0 key="email"/>
              </field>
          </form>
      </formset>
  </form-validation>

struts-config.xml的代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
  <data-sources />
  <form-beans >
    <form-bean name="checkForm" type="com.kmust.struts.form.CheckForm" />
  </form-beans>

  <global-exceptions />
  <global-forwards />
  <action-mappings >
    
    <action
      attribute="checkForm"
      input="/user.jsp"
      name="checkForm"
      path="/check"
      scope="request"
      type="com.kmust.struts.action.CheckAction" 
      validate="true">
      <forward name="success" path="/user.jsp"></forward>
</action>
  </action-mappings>

  <message-resources parameter="com.kmust.struts.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>

user.jsp代码
.......
<body>
        <html:form action="/check">
            userName : <html:text property="userName"/><html:errors property="userName"/><br/>
            age : <html:text property="age"/><html:errors property="age"/><br/>
            email : <html:text property="email"/><html:errors property="email"/><br/>
            passWord : <html:text property="passWord"/><html:errors property="passWord"/><br/>
            <html:submit/><html:cancel/>
        </html:form>
    </body>
.......

ApplicationResources.properties里面的:
# Resources for parameter 'com.kmust.struts.ApplicationResources'
# Project webtest


userName=姓名
passWord=密码
age=年龄
email=邮箱

errors.email={0}不是合法的email地址
errors.rang={0}必须大于{1}小于{2}
errors.required={0}必须输入

为什么输入不合法的内容后,没有提示错误信息啊?

回复列表 (共2个回复)

沙发

<form name="CheckForm">  <!-- 你把CheckForm的开头的C改小写c看看 -->
              <field property="userName" depends="required">
                  <arg0 key="userName"/>
              </field>
              <field property="passWord" depends="required">
                  <arg0 key="passWord"/>    
              </field>
              <field property="age" depends="rang">
                  <arg0 key="age"/>
                  <arg1 key="18" resource="false"/><!--这里这样写不不大记得对不对了-->
                  <arg2 key="30" resource="false"/>
                  <var>
                      <var-name>min</var-name>
                      <var-value>18</var-value>
                  </var>
                  <var>
                      <var-name>max</var-name>
                      <var-value>30</var-value>
                  </var>
              </field>
              <field property="email" depends="email">
                  <arg0 key="email"/>
              </field>
          </form>
      </formset>

板凳


我的Form的名字就叫CheckForm,不能改小写吧

我来回复

您尚未登录,请登录后再回复。点此登录或注册