Struts验证不起作用[奇怪]

时间:2013-06-21 04:48:30

标签: struts2 struts struts-validation

我认为我在struts验证中遇到了一个奇怪的情况,因为有时它会起作用,有时候不起作用。我用maven。在第一次安装时,我得到它的工作。但是在maven清理之后,它再也不会工作了。

  • 当我安装项目(maven install)时,xml验证(CustomerAction-validation.xml)不会复制到目标文件夹中的操作类包。我不知道为什么。
  • 如果我在“客户表单”操作中删除<interceptor-ref name="prepare"/>,则会出错。虽然我使用default-interceptor。没有定制。
  • 有关登录表单的其他问题(与验证无关)。如何将表单发布到另一个名称空间。我尝试使用namespace="/web",但它不起作用。这就是为什么我尝试使用s:url,但它让我无法使用s:form

这是我的项目结构

project structure

的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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>struts.test</groupId>
<artifactId>validation</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>validation Maven Webapp</name>
<url>http://maven.apache.org</url>

<properties>
    <junit.version>3.8.1</junit.version>
    <struts2.version>2.3.1.2</struts2.version>
    <struts2-tiles-plugin.version>2.3.14</struts2-tiles-plugin.version>
    <tiles.version>2.0.7</tiles.version>
    <commons-logging.version>1.1.3</commons-logging.version>
    <commons-collection.version>3.2.1</commons-collection.version>
    <xwork.version>2.1.3</xwork.version>
    <dojo.version>2.1.8</dojo.version>
</properties>
<dependencies>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>

    <!-- Struts 2 -->
    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-core</artifactId>
        <version>${struts2.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-tiles-plugin</artifactId>
        <version>${struts2-tiles-plugin.version}</version>
    </dependency>                   

    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-api</artifactId>
        <version>${tiles.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-core</artifactId>
        <version>${tiles.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-jsp</artifactId>
        <version>${tiles.version}</version>
    </dependency>


    <!-- Struts 2 Dojo Ajax Tags -->
    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-dojo-plugin</artifactId>
        <version>${dojo.version}</version>
    </dependency>

    <!-- Struts 2 JSON Plugins -->
    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-json-plugin</artifactId>
        <version>${dojo.version}</version>
    </dependency>

    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>${commons-logging.version}</version>
    </dependency>

    <dependency>
        <groupId>com.opensymphony</groupId>
        <artifactId>xwork</artifactId>
        <version>${xwork.version}</version>
    </dependency>

    <dependency>
        <groupId>org.freemarker</groupId>
        <artifactId>freemarker</artifactId>
        <version>2.3.19</version>
    </dependency>

    <dependency>
        <groupId>ognl</groupId>
        <artifactId>ognl</artifactId>
        <version>3.0.6</version>
    </dependency>

    <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
        <version>1.8.3</version>
    </dependency>

    <dependency>
        <groupId>commons-digester</groupId>
        <artifactId>commons-digester</artifactId>
        <version>2.1</version>
    </dependency>

</dependencies>

<build>
    <finalName>validation</finalName>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
    </plugins>
</build>

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>

<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<constant name="struts.custom.i18n.resources" value="ApplicationResources" />

<package name="default" extends="struts-default" namespace="/web">
    <result-types>
        <result-type name="tiles"
            class="org.apache.struts2.views.tiles.TilesResult" />
    </result-types>
    <action name="login" class="struts.test.validation.action.LoginAction">
        <result name="success" type="tiles">/welcome.tiles</result>
        <result name="error">Login.jsp</result>
    </action>
    <action name="savecustomer" class="struts.test.validation.action.CustomerAction" method="saveCustomer">
        <result name="success" type="tiles">/customer.success.tiles</result>
        <result name="input" type="tiles">/customer.tiles</result>
    </action>
    <action name="customer-form" class="struts.test.validation.action.CustomerAction" method="addCustomer">
        <interceptor-ref name="prepare"/>
        <result name="success" type="tiles">/customer.tiles</result>
        <result name="input" type="tiles">/customer.tiles</result>
    </action>
</package>

</struts>

CustomerAction-validation.xml中

<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
 <validators>
<field name="customer.name">
    <field-validator type="requiredstring">
        <param name="trim">true</param>
        <message key="errors.required" />
    </field-validator>
</field>
<field name="customer.email">
    <field-validator type="requiredstring">
        <param name="trim">true</param>
        <message key="errors.required" />
    </field-validator>
</field>
<field name="customer.type">
    <field-validator type="requiredstring">
        <param name="trim">true</param>
        <message key="errors.required" />
    </field-validator>
</field>
</validators>

CustomerAction类

public class CustomerAction extends ActionSupport{
/**
 * 
 */
private static final long serialVersionUID = 1L;
private CustomerDto customer;
private List<String > listCustoType;

public String addCustomer(){
    listCustoType = new ArrayList<String>();
    listCustoType.add("Type1");
    listCustoType.add("Type2");
    return SUCCESS;
}

public String saveCustomer() {
    System.out.println(customer.getName()+" "+customer.getEmail());
    return SUCCESS;
}   
public CustomerDto getCustomer() {
    return customer;
}
public void setCustomer(CustomerDto customer) {
    this.customer = customer;
}
public List<String> getListCustoType() {
    return listCustoType;
}
public void setListCustoType(List<String> listCustoType) {
    this.listCustoType = listCustoType;
}

}

CustomerDto class

public class CustomerDto {

private Integer id;
private String name;
private String email;
private String type;
    //getter & setter

客户表格

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<body>
<h2>Customer Form</h2>
<s:form action="savecustomer" method="post" validate="true" namespace="/web">
<s:select
   label="Customer Type"
   list="listCustoType"
   name="customer.type"
   headerKey="-1"
   headerValue=""/>
<s:textfield name="customer.name" key="customer.name" size="20" />
<s:textfield name="customer.email" key="email" size="20" />
<s:submit method="addCustomer" key="label.add.customer" align="center" />
</s:form>
</body>
</html>

登录表单

<form action='<s:url value='/'/>web/login' method="post">
<s:textfield name="username" key="label.username" size="20" />
<s:password name="password" key="label.password" size="20" />
<s:submit method="execute" key="label.login" align="center" />
</form>

我上传的完整项目 here

2 个答案:

答案 0 :(得分:0)

在Maven项目中,要部署在类路径上的非Java工件需要位于:

src/main/resources

因为您已将它们放在Java源目录树中,所以不会部署它们。

在您的具体案例中:

src/main/resources/struts/test/validation/action/CustomerValidation-validation.xml

这与部署到类路径的资源目录中的其他 XML文件的模式相同。

答案 1 :(得分:0)

刚遇到我的验证文件未复制的问题。要解决此问题,您需要添加maven-war-plugin并正确配置它以包含XML文件。以下是我添加到我的pom.xml文件中以使其在我的项目中工作的内容:

        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <webResources>
                    <resource>
                        <!-- this is relative to the pom.xml directory -->
                        <directory>src/main/java/com/project/controller/</directory>
                        <targetPath>WEB-INF/classes/com/project/controller/</targetPath>
                        <includes>
                            <include>**/*.xml</include>
                        </includes>
                    </resource>
                </webResources>
            </configuration>
        </plugin>
    </plugins>
</build>

您可以详细了解maven-war-plugin的工作原理here