java Struts2 xml 验证不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13916385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:26:18  来源:igfitidea点击:

Struts2 xml validation is not working

javaformsstruts2tags

提问by user962206

Here is my Register-validation.xml

这是我的 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>

Here is my form.

这是我的表格。

<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>

Even though I not enter a value for username and age the form still keeps going in the success page. why is it not working?

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

EDIT

编辑

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>

Profile Bean

配置文件 Bean

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;
    }



}

and User 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;
    }


}

Register Action

注册操作

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;
    }
}

回答by KyelJmD

I have tried to run your code. your error is that you need to provide a result named input.will look for this result if validation fails.

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

<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>

回答by Viral Patel

In your Register-validation.xml file can you change the dtd definition from:

在您的 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">

to

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

So that your Register-validation.xml looks like:

这样您的 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> 

回答by Andrea Ligios

I use XML Validation like this (different syntax):

我像这样使用 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>

Try it.

试试看。

P.S: I've written something about this here.

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

EDIT:

编辑:

Try changing this

尝试改变这个

<s:form action="register">

to this

对此

<s:form action="/register">

(or eventually to <s:form action="register" namespace="/">)

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

including the namespace. The error you reported in comments to AleksandrM's answer is telling you that namespaces are messed up... (and thinking about it, you are on "/Welcome", looking up for an action located in "/" without specifying a namespace... this could fire some lookup mechanism that bypass the validation file and just find the Action)

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

I hope is this, I've run out of ideas :)

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

回答by Aleksandr M

You are using non-field validator and parameter for field name must be fieldNameinstead of fieldname. BTW it is preferable to use field validator syntax.

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

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

回答by kalai

I had the same issue.I resolved it putting actionClass-validation.xml under the same folder where my actionClass.java was residing.

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