Struts2'必需'字段验证拦截器无法正常工作 [英] Struts2 'Required' field Validation Interceptors not working

查看:100
本文介绍了Struts2'必需'字段验证拦截器无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Building a login application (In Netbeans 731) which basically ensures non blank values for username and pw. The application runs fine except the validation doesn't work, so even if the user enters blank values they are taken to the Thanks.jsp page. After trying to build it as a Netbeans web app, I have rebuilt this application using Maven, with the following directory structure:

生成的war文件为:

pom.xml

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

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

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

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

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

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

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构建应用程序后,我不再收到此错误。

When I built this application manually (with Maven) it gave a stack trace due to wrong version of DTD tags. I no longer get this error after building the application using Maven.

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

When running this application I call the Required1.action. Even if I do not enter a password it skips straight to Action2 and the validation interceptor (RequiredTestAction-validation.xml) does not seem to be working.

推荐答案

必需的验证器是基本验证器类型,它检查 null 字段值。如果在输入字段中输入空白字符,则可能无效。改为使用 requiredstring 验证器。

Required validator is a basic validator type and it checks for null field values. If you enter blank characters to the input fields it may not work. Use requiredstring validator instead.

<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/> 和它继承了你的包。

The action class extends ActionSupport, it has default implementation of the Action interface required to return result after the action execution. And package extends struts-default that has <default-interceptor-ref name="defaultStack"/> and it inherited to your package.

另外, FilterDispatcher

这篇关于Struts2'必需'字段验证拦截器无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆