我正在构建一个登录应用程序(在 Netbeans 731 中),它基本上确保
username
和 password
的非空值。应用程序运行良好,但验证不起作用,因此即使用户输入空白值,它们也会移至 Thanks.jsp
页面。在尝试将其构建为 Netbeans Web 应用程序后,我使用 Maven 重新构建了该应用程序,目录结构如下:
生成的war文件为:
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
) 似乎不起作用。
必需的验证器是基本验证器类型,它检查
null
字段值。如果您在输入字段中输入空白字符,则可能无法正常工作。请改用 requiredstring
验证器。
<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>
动作类扩展了
ActionSupport
,它具有在动作执行后返回结果所需的Action
接口的默认实现。包扩展了 struts-default
,它具有 <default-interceptor-ref name="defaultStack"/>
并继承到您的包。
此外,自 Struts 2.1.3 起,
FilterDispatcher
已被弃用。