具有xml配置的Struts2验证程序不起作用

时间:2016-10-26 06:51:47

标签: java validation jsp struts2 struts2-interceptors

我是Struts2的初学者。当我尝试使用验证框架来验证表单中的输入数据时,我遇到了一个问题:验证器根本不起作用。当你没有输入任何内容时,我可以获得应该返回的页面。

以下是我的代码和包,我在StackOverFlow上搜索过它,但我找不到解决问题的解决方案。

The structure of the project

Register.java

package validation;
/**
 * Created by xiangang.wei on 2016/10/26.
 */
public class Register {
    private String userName;
    private String password;
    private String rpassword;
    private int age;
    private int phone;
    private String email;

    public String getRpassword() {
        return rpassword;
    }

    public void setRpassword(String rpassword) {
        this.rpassword = rpassword;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setPhone(int phone) {
        this.phone = phone;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getUserName() {

        return userName;
    }

    public String getPassword() {
        return password;
    }

    public int getAge() {
        return age;
    }

    public int getPhone() {
        return phone;
    }

    public String getEmail() {
        return email;
    }

    public String execute(){
        return "success";
    }
}

注册-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="stringlength" >
        <param name="fieldName">userName</param>
        <param name="maxLength">10</param>
        <param name="minLength">1</param>
        <message>The length is wrong!</message>
    </validator>
    <validator type="fieldexpression">
        <param name="fieldName">password</param>
        <param name="expression">![CDATA[password == rpassword]]</param>
        <message>The password is not the same!</message>
    </validator>
    <validator type="int">
        <param name="fieldName">age</param>
        <param name="max">130</param>
        <param name="min">1</param>
        <message>The age must be 1 - 130</message>
    </validator>
    <validator type="email">
        <param name="fieldName">email</param>
        <message> Please enter a valid email address!</message>
    </validator>
</validators>

struts.xml中

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<package name="register" extends="struts-default" namespace="/">
    <action name="register" class="validation.Register">
        <result name="success">/register-view/register-success.jsp</result>
        <result name="input">/index.jsp</result>
    </action>
</package>
</struts>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">


    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

的build.gradle

group 'xiangang.wei'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'war'

sourceCompatibility = 1.8

repositories {
    jcenter()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
    compile 'org.apache.struts:struts2-core:2.5'
}

的index.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
  Created by IntelliJ IDEA.
  User: xiangang.wei
  Date: 2016/10/26
  Time: 11:19
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>User Register!</title>
</head>
<body>
<s:form action="register" method="POST">
    <table>
        <tr>
            <td><s:textfield label="Name" name="userName"/></td>
        </tr>
        <tr>
            <td><s:password label="Password" name="password"/></td>
        </tr>
        <tr>
            <td><s:password label="Confirm Password" name="rpassword"/></td>
        </tr>
        <tr>
            <td><s:textfield label="age" name="age"/></td>
        </tr>
        <tr>
            <td><s:textfield label="Phone" name="phone"/></td>
        </tr>
        <tr>
            <td><s:textfield label="Email" name="email"/></td>
        </tr>
        <tr>
            <td>
                <s:submit value="Submit"/>
            </td>
            <td>
                <s:reset value="Reset"/>
            </td>
        </tr>
    </table>
</s:form>
</body>
</html>

寄存器的success.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
  Created by IntelliJ IDEA.
  User: xiangang.wei
  Date: 2016/10/26
  Time: 13:55
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>注册成功!</title>
</head>
<body>
<h3>Congratulation!</h3>
<hr/>
<h5>Here is your registering info:</h5>
Name:<s:property value="userName"/>
Password:<s:property value="password"/>
Age:<s:property value="age"/>
E-mail:<s:property value="email"/>
Phone Number:<s:property value="phone"/>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

验证由Validation Interceptor执行。来自其文档:

  

此拦截器通过标准验证框架运行操作,标准验证框架依次检查针对任何验证规则(在ActionClass-validation.xml等文件中找到)的操作,并添加字段级和操作级错误消息(前提是该操作实现了ValidationAware )。

您的操作不会扩展ActionSupport(实现ValidationAware等许多内容),也不会手动实施ValidationAware

您根本没有指示框架验证您的操作。

作为一条黄金法则,始终让您的行动延伸ActionSupport

更好的是,创建一个扩展ActionSupport的BaseAction,您可以在其中放置常用的逻辑和设置,例如。 Locale / TimeZone,日期解析格式等...然后让您的操作扩展BaseAction。

修改

在最近的Struts2版本中,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">

in the docs所述。