gpt4 book ai didi

java - 带有 xml 配置的 Struts2 validator 不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 12:07:02 31 4
gpt4 key购买 nike

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

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

The structure of the project

注册.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";
}
}

注册验证.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>

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

注册成功.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>

最佳答案

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

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

您的操作不是扩展 ActionSupport(实现 ValidationAware 等),也不是手动实现 ValidationAware

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

作为一条黄金法则,始终让您的操作扩展 ActionSupport

更好的是,创建一个扩展 ActionSupport 的 BaseAction,您将在其中放置通用逻辑和设置,例如。区域设置/时区、日期解析格式等...然后使您的操作扩展 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 .

关于java - 带有 xml 配置的 Struts2 validator 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40255534/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com