Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

java - Struts2 'Required' field Validation Interceptors not working

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:

enter image description here

The generated war file is:

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> 

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

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.

Also, the FilterDispatcher is deprecated since Struts 2.1.3.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...