UserAction.java
package com.bebig.struts2.user.action;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport  {
    
private String name;


    
public String add() {
        
if(name==null || !name.equals("admin"))
        
{
            
this.addFieldError("name""Name is error!");
            
return ERROR;
        }

        
return SUCCESS;
    }



    
public void setName(String name) {
        
this.name = name;
    }



    
public String getName() {
        
return name;
    }



}

struts.xml代码片断:
<package name="user" namespace="/user" extends="struts-default">
        
<action name="userAdd" class="com.bebig.struts2.user.action.UserAction"
            method
="add">
            
<result>
                /user_add_success.jsp
            
</result>
            
<result name="error">
                /user_add_error.jsp
            
</result>
        
</action>
    
</package>
index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding
="utf-8"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Simple Data Validation</title>

</head>
<body>
<form action="user/userAdd.action" method="post">
<p>姓名:</p>
<input type="text" name="name"><input type="submit"></form>


</body>
</html>
user_add_error.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding
="utf-8"
%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>error</title>
<%
    
String path = request.getContextPath();
    
String basePath = request.getScheme() + "://"
            
+ request.getServerName() + ":" + request.getServerPort()
            
+ path + "/";
%>
<base href="<%=basePath%>" />
<!-- 使用base标签指定本页面所有链接的参照路径 -->
</head>
<body>

<p>User add error.</p>
<s:fielderror fieldName="name"></s:fielderror>
<br>
<s:property value="errors.name[0]"></s:property>
<s:debug></s:debug>
</body>
</html>
user_add_success.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding
="utf-8"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Struts2_Path</title>
<%
    
String path = request.getContextPath();
    
String basePath = request.getScheme() + "://"
            
+ request.getServerName() + ":" + request.getServerPort()
            
+ path + "/";
%>
<base href="<%=basePath%>" /><!-- 使用base标签指定本页面所有链接的参照路径 -->
</head>
<body>

<p>User add success.</p>

</body>
</html>