1

I am a beginner of Struts2. When I've tried to use the validation framework to validate the input data from the form, I have encountered a problem: the validators don't work at all. I can get the page which is supposed to be returned when you pass the validation with nothing being entered.

The following is my code and package, I have searched it on StackOverFlow, but I have not found a solution that solve my problem.

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

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

register-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>
  • @Roman C Thanks for answering my question, I have read your answer at the other question and it is good answer, it explains how to use the validator. But I think it still doesn't solve my problem. even though I extends the ActionSupport class as Andrea Ligios said, the validator is still dead! it just doesn't work at all. – xiangang wei Oct 28 '16 at 01:04
  • To be honest, what did you do so far to solve your problem? You should know that SO is not providing help and doesn't do solving your problems. It is Q & A site where people ask good questions and receive good answers, so everybody enjoy reading. The questions like yours is solely private, if you have some problems in programming or lack of knowledge in particular area you should learn it outside of SO. SO can't help you if you don't understand the topic of the question. – Roman C Oct 28 '16 at 06:31

1 Answers1

0

Validation is performed by the Validation Interceptor. From its docs:

This interceptor runs the action through the standard validation framework, which in turn checks the action against any validation rules (found in files such as ActionClass-validation.xml) and adds field-level and action-level error messages (provided that the action implements ValidationAware).

Your action is not extending ActionSupport (that implements ValidationAware, among many other things), nor implementing ValidationAware manually.

You've simply not instructed the framework to validate your action.

As a golden rule, always make your actions extend ActionSupport.

Even better, create a BaseAction that extends ActionSupport, where you will put the common logic and settings, eg. Locale / TimeZone, date parsing format etc... and then make your actions extend the BaseAction.


EDIT

In recent Struts2 versions, the DTD has changed; replace this

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

with this

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

as described in the docs.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Did it work ? Please remember to accept the answer if it worked – Andrea Ligios Oct 27 '16 at 07:27
  • Hi, you are right about the thing that I didn't extend the ActionSupport class.So I extends the ActionSupport class, but it seems doesn"t work! The validation still doesn't work. – xiangang wei Oct 28 '16 at 00:53
  • I'm using Struts 2.5.1 and I had to roll my DTD in validators.xml to 1.0 instead of 1.0.3 fr it to work ! Might be of use to someone who's only checked a single version – Dangerous Hamster Jul 28 '17 at 16:12