public class User {
private Integer id;
private String name;
private String pwd;
getter();
setter();
}
和此类(User) 放在同一个目录下User-userLoginContext-validation.xml这个文件!
User-userLoginContext-validation.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="name">
<field-validator type="requiredstring" short-circuit="true">
<message>姓名是必填的</message>
</field-validator>
<field-validator type="regex">
<param name="expression"><![CDATA[(^[a-zA-Z_]/w{3,9}$)]]></param>
<message>姓名不合法</message>
</field-validator>
</field>
<field name="pwd">
<field-validator type="requiredstring" short-circuit="true">
<message>密码是必填的</message>
</field-validator>
</field>
</validators>
action 如下
public class UserAction extends ActionSupport {
private static final long serialVersionUID = -2554018432709689579L;
private User user; //自定义类型Field
public String login() throws Exception{
return SUCCESS;
}
}
<package name="my" extends="struts-default" namespace="/">
<action name="user_*" class="com.liuc.web.action.UserAction" method="{1}">
<result name="success">/info.jsp</result>
<result name="input">/user_{1}.jsp</result>
</action>
</package>
页面:
<div style="color:red"><s:fielderror/></div>
<form action="user_login.action" method="post">
<table>
<tr>
<td>登录名</td>
<td><input type="text" name="user.name" value="${param['user.name']}"/></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="user.pwd"/></td>
</tr>
<tr><td colspan="2"><input type="submit" value=" 提交 "/></td></tr>
</table>
</form>