我可以将Java struts2 Action validation()函数重写为Action-validation XML吗?

时间:2010-11-01 14:39:40

标签: forms jsp struts validation

我目前正在为登录表单进行验证。

我的操作中有一组 WORKING 验证:

// SNIP!!!!
    @Override
    public void validate() 
    { 
        if (userBean.getUserName().length() == 0)
            { addFieldError("userBean.userName", "Username is required."); }

        if (userBean.getPassword().length() == 0)
            { addFieldError("userBean.password", "Password is required."); }
        else if (!(
                    User
                        .findUser(userBean.getUserName())
                        .getPassword()
                        .equals(userBean.getPassword())
                ))  
            { addFieldError("userBean.password", "Password is invalid."); }
    }
// SNIP!!!!

我想弄清楚我是否可以将其重写为验证XML

这就是我的XML目前的样子

<!-- //src/hu/flux/user/LoginUserAction-validation.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
 <validator type="requiredstring">
  <param name="fieldname">userBean.userName</param>
  <message>Username is required.</message>
 </validator>
 <validator type="requiredstring">
  <param name="fieldname">userBean.password</param>
  <message>Password is required.</message>
 </validator>
  <validator type="fieldexpression">
  <param name="fieldname">userBean.password</param>
  <param name="expression"><![CDATA[
  (
   User
    .findUser(userBean.userName)
    .getPassword()
    .equals(userBean.password)
   ) 
  ]]></param>
        <!-- This error message is just for debugging -->
  <message>

    Password is invalid. 
    ${userBean.password} received. 
    ${(
   User
    .findUser(userBean.userName)
    .getPassword()
    .equals(userBean.password)
   )} expected.
  </message>
 </validator>
</validators>

当我尝试使用XML而不是JAVA,然后提交任何密码时,表单上会显示如下错误消息:

Password is invalid. sdfdsf received. expected.

“sdfdsf”可以是我尝试的任何密码。

是否可以使用验证器XML执行此操作?或者我是否需要使用validation()函数?

(我以为我在XML中使用了这个,但是由于一些莫名其妙的原因它停止了工作。)

0 个答案:

没有答案