我认为我在 struts 验证中遇到了一个奇怪的情况,因为有时它有效,有时无效。我使用maven。在第一次安装时,我得到它的工作。但是在 maven clean 之后,它再也不会起作用了。
- 当我安装我的项目(maven 安装)时,xml 验证(CustomerAction-validation.xml)不会复制到目标文件夹中的操作类包中。我不知道为什么。
- 如果我
<interceptor-ref name="prepare"/>
在“客户表单”操作中删除它会出错。虽然我使用默认拦截器。没有定制。 - 关于登录表单的其他问题(与验证无关)。如何将表单发布到另一个命名空间。我尝试使用
namespace="/web"
,但它不起作用。这就是为什么我尝试使用s:url
,但它使我无法使用s:form
这是我的项目结构
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 类
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>