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