这是源码:一个 action,二个jsp页面,一个validation.xml文件,xwork.xml
package com.mystore;
import com.opensymphony.xwork.ActionSupport;
public class LoginAction extends ActionSupport {
private User user = new User();
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}
}
input.jsp
<%@taglib prefix="ww" uri="webwork"%>
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'input.jsp' starting page</title>
</head>
<body>
<ww:form name="'loginAction'" action="'loginAction'" method="'post'" validate="true">
<ww:textfield name="'user.name'" label="'name'" required="true"/>
<ww:textfield name="'user.email'" label="'email'" required="true"/>
<ww:submit value="'Submit'"/>
</ww:form>
</body>
</html>
result.jsp
<%@taglib prefix="ww" uri="webwork"%>
<html>
<head>hi,the result of validation</head>
<body>
This is my JSP page.
name <ww:property value="user.name"/>
email<ww:property value="user.email"/>
</body>
</html>
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>WebWork 2.0 Example App</display-name>
<servlet>
<servlet-name>webwork</servlet-name>
<servlet-class>
com.opensymphony.webwork.dispatcher.ServletDispatcher
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>webwork</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>input.jsp</welcome-file>
</welcome-file-list>
<taglib>
<taglib-uri>webwork</taglib-uri>
<taglib-location>/WEB-INF/webwork.tld</taglib-location>
</taglib>
</web-app>
xwork.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN" "http://www.opensymphony.com/xwork/xwork-1.0.dtd" >
<xwork>
<include file="webwork-default.xml"></include>
<package name="default" extends="webwork-default">
<action name="loginAction" class="com.mystore.LoginAction">
<result name="input" type="dispatcher">
<param name="location">input.jsp</param>
</result>
<result name="success" type="dispatcher">
<param name="location">result.jsp</param>
</result>
<interceptor-ref name="validationWorkflowStack"></interceptor-ref>
</action>
</package>
</xwork>
LoginAction-validation.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd" >
<validators>
<field name="user.name">
<field-validator type="requiredstring">
<message>name is required</message>
</field-validator>
</field>
<field name="user.email">
<field-validator type="requiredstring">
<message>email is required</message>
</field-validator>
<field-validator type="email">
<message>
email is required or it must be inputed email
addresses!!!
</message>
</field-validator>
</field>
</validators>
package com.mystore;
import java.io.Serializable;
import org.apache.commons.lang.builder.ToStringBuilder;
public class User implements Serializable {
private String name;
private String email;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}