gpt4 book ai didi

java - Struts2 'Required' 字段验证拦截器不工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:08:18 25 4
gpt4 key购买 nike

构建一个登录应用程序(在 Netbeans 731 中),它基本上确保用户名和密码的非空值。该应用程序运行良好,但验证不起作用,因此即使用户输入空白值,他们也会被带到 Thanks.jsp 页面。在尝试将其构建为 Netbeans Web 应用程序后,我使用 Maven 重建了该应用程序,目录结构如下:

enter image description here

生成的war文件为:

enter image description here

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.manaar</groupId>
<artifactId>Maven8a2</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>Maven8a2</name>

<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>


<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.15.1</version>
</dependency>


<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>


</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>6.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>

web/jsp/Required.jsp:

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>required Validator Example</title>
<style type="text/css">@import url(css/main.css);</style>
<style>
.errorMessage {
color:red;
}
</style>
</head>
<body>
<div id="global" style="width:350px">
<h3>Enter user name and password</h3>
<s:fielderror/>
<s:form action="Required2">
<s:textfield name="userName" label="User Name"/>

<s:password name="password" label="Password"/>
<s:submit/>
</s:form>
</div>
</body>
</html>

web/jsp/Thanks.jsp:

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Thank you</title>
<style type="text/css">@import url(css/main.css);</style>
</head>
<body>
<div id="global">
Thank you
</div>
</body>
</html>

WEB-INF/classes/app08a/RequiredTestAction.class:

package app08a;
import com.opensymphony.xwork2.ActionSupport;

public class RequiredTestAction extends ActionSupport {
private String userName;
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}

WEB-INF/classes/app08a/RequiredTestAction-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>
<field name="userName">
<field-validator type="requiredstring">
<message>Please enter a user name</message>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<message>Please enter a password</message>
</field-validator>
</field>
</validators>

WEB-INF/classes/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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />

<package name="app08a" extends="struts-default">

<action name="Required1">
<result>/jsp/Required.jsp</result>
</action>
<action name="Required2" class="app08a.RequiredTestAction">
<result name="input">/jsp/Required.jsp</result>
<result>/jsp/Thanks.jsp</result>
</action>

</package>

</struts>

WEB-INF/web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee /web-app_2_5.xsd"
version="2.5">

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

<!-- Restrict direct access to JSPs.
For the security constraint to work, the auth-constraint
and login-config elements must be present -->
<security-constraint>
<web-resource-collection>
<web-resource-name>JSPs</web-resource-name>
<url-pattern>/jsp/*</url-pattern>
</web-resource-collection>
<auth-constraint/>
</security-constraint>

<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>

当我手动(使用 Maven)构建此应用程序时,由于 DTD 标记版本错误,它给出了堆栈跟踪。使用 Maven 构建应用程序后,我不再收到此错误。

运行此应用程序时,我调用 Required1.action。即使我不输入密码,它也会直接跳到 Action2,验证拦截器 (RequiredTestAction-validation.xml) 似乎不起作用。

最佳答案

Required validator 是一种基本的 validator 类型,它检查 null字段值。如果您在输入字段中输入空白字符,它可能不起作用。使用 requiredstring validator 代替。

<field name="userName">
<field-validator type="requiredstring">
<message>Please enter a user name</message>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<message>Please enter a password</message>
</field-validator>
</field>

Action 类扩展ActionSupport , 它有 Action 的默认实现 Action 执行后需要返回结果的接口(interface)。并且包扩展了 struts-default<default-interceptor-ref name="defaultStack"/>并将它继承到您的包中。

另外, FilterDispatcher is deprecated since Struts 2.1.3 .

关于java - Struts2 'Required' 字段验证拦截器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18038964/

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