3

这是我的 Register-validation.xml

<!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>User name is required.</message>
    </validator>

    <validator type="required">
        <param name="fieldname">profileBean.Age</param>
        <message>Age is required.</message>
    </validator>

</validators>

这是我的表格。

<s:form action="register">

        <label>UserName</label>
        <s:textfield cssClass="formfield" name="userBean.username"
            label="User Name" />
        <br />
        <label>Password</label>
        <s:password cssClass="formfield" name="userBean.password"
            label="Password" />
        <br />
        <label>First Name</label>
        <s:textfield cssClass="formfield" name="profileBean.firstName"
            label="First Name" />
        <br />
        <label>Last Name</label>
        <s:textfield cssClass="formfield" name="profileBean.lastName"
            label="Last Name" />
        <br />
        <label>Age</label>
        <s:textfield cssClass="formfield" name="profileBean.age" label="Age" />
        <s:submit />
    </s:form>

即使我没有输入用户名和年龄的值,表单仍然会继续显示在成功页面中。为什么它不工作?

编辑

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

<struts>
    <constant name="struts.devMode" value="false" />
    <constant name="struts.ui.theme" value="simple" />

    <package name="basicstruts2" namespace="/Welcome" extends="struts-default">
        <action name="index">
            <result>WEB-INF/index.jsp</result>
        </action>

        <action name="HelloWorld" class="actions.HelloWorldAction"
            method="execute">
            <result name="success">/HelloWorld.jsp</result>
        </action>
    </package>

    <package name="Registration" namespace="/" extends="struts-default">
        <action name="register" class="actions.Register"
            method="execute">
            <result name="success">/thankyou.jsp</result>
        </action>
    </package>
</struts>

配置文件豆

package model;

public class Profile {
    private String firstName;
    private String lastName;
    private int age;

    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }



}

和用户 Bean

package model;

public class User {
    private String username;
    private String password;
    private int accountType;//Admin  - 0; User - 1;
    private Profile profile;

    public User(){
        accountType = 1;
    }


    public String getUsername() {
        return username;
    }


    public void setUsername(String username) {
        this.username = username;
    }


    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public int getAccountType() {
        return accountType;
    }
    public void setAccountType(int accountType) {
        this.accountType = accountType;
    }
    public Profile getProfile() {
        return profile;
    }

    public void setProfile(Profile profile) {
        this.profile = profile;
    }


}

注册动作

public class Register extends ActionSupport  implements ServletRequestAware{

    public String execute(){

        userBean.setProfile(getProfileBean());
        return "success";
    }

    private User userBean;
    private Profile profileBean;
    HttpServletRequest request;

    public User getUserBean() {
        return userBean;
    }

    public void setUserBean(User userBean) {
        this.userBean = userBean;
    }

    public Profile getProfileBean() {
        return profileBean;
    }

    public void setProfileBean(Profile profileBean) {
        this.profileBean = profileBean;
    }

    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    public HttpServletRequest getServletRequest() {
        return this.request;
    }
}
4

5 回答 5

6

我试图运行你的代码。您的错误是您需要提供一个名为input. 的结果,如果验证失败,将查找此结果。

<package name="Registration" namespace="/" extends="struts-default">
        <action name="register" class="actions.Register"
            method="execute">
          <result name ="input">erropage.jsp</result>  
          <result name="success">/thankyou.jsp</result>
        </action>
    </package>
于 2012-12-18T04:31:31.280 回答
2

在您的 Register-validation.xml 文件中,您可以更改 dtd 定义:

<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">

<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
    "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">

这样您的 Register-validation.xml 看起来像:

<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
    "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>

    <validator type="requiredstring">
        <param name="fieldname">userBean.username</param>
        <message>User name is required.</message>
    </validator>

    <validator type="required">
        <param name="fieldname">profileBean.Age</param>
        <message>Age is required.</message>
    </validator>

</validators> 
于 2012-12-17T16:39:14.057 回答
1

我像这样使用 XML 验证(不同的语法):

  <validators>
      <field name="profileBean.age">
          <field-validator type="required">
              <message><![CDATA[ Age is required ]]></message>
          </field-validator>
      </field>

      <field name="userBean.username">
          <field-validator type="requiredstring">
              <message><![CDATA[ Username is required ]]></message>
          </field-validator>
      </field>
  </validators>

尝试一下。

PS:我在这里写了一些关于这个的东西

编辑:

尝试改变这个

<s:form action="register">

对此

<s:form action="/register">

(或最终到<s:form action="register" namespace="/">

包括命名空间。您在对 AleksandrM 的回答的评论中报告的错误是告诉您命名空间被搞砸了......(考虑一下,您在“/Welcome”上,正在查找位于“/”中的操作而没有指定命名空间...... .这可能会触发一些绕过验证文件的查找机制,只找到动作)

我希望是这样,我已经没有想法了:)

于 2012-12-17T15:02:12.333 回答
0

您正在使用非字段验证器,并且字段名称的参数必须fieldName代替fieldname. 顺便说一句,最好使用字段验证器语法。

<field name="userBean.username">
  <field-validator type="requiredstring">
    <message>User name is required.</message>
  </field-validator>
</field>
于 2012-12-17T17:40:46.900 回答
0

我有同样的问题。我解决了它,将 actionClass-validation.xml 放在我的 actionClass.java 所在的同一文件夹下。

于 2015-08-01T07:48:07.567 回答