-1

I think I get a strange condition in struts validation, because sometimes it works and sometimes do not work. I using maven. On the first install, i get it work. But after maven clean, it never work again.

  • When I install my project (maven install), xml validation (CustomerAction-validation.xml) is not copied to action class package in the target folder. I don't know why.
  • It will error if I remove <interceptor-ref name="prepare"/> in "customer-form" action. Although I use default-interceptor. No customization.
  • Other question about login form (unrelated to validation). How to post the form to another namespace. I try by using namespace="/web", but it not work. That's why i try by using s:url, but it make i can't use s:form

This is my project structure

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 class

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

Customer Form

<%@ 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>

Login form

<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>

My complete project I uploaded here

Mahadi Siregar
  • 615
  • 3
  • 17
  • 38

2 Answers2

0

In Maven projects, non-Java artifacts intended to be deployed on the classpath need to be in:

src/main/resources

Because you've placed them in the Java source directory tree they will not be deployed.

In your specific case:

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

This is the same pattern as the other XML files in the resources directory, deployed to the classpath.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • I have try, and xml validation now exist on class package. But it still do not work. Is there another mistakes in my project? – Mahadi Siregar Jun 24 '13 at 01:28
0

Just ran into the problem where my validation files were not copying over. To fix this problem, you need to add in the maven-war-plugin and configure it properly to include the XML files. Here is what I added to my pom.xml file to get it working in my project:

        </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>

You can read up more on how the maven-war-plugin works here.

kent_e
  • 111
  • 1
  • 7