在用户提交表单的时候,用struts2提供的机制,验证密码是否为空,用户名是否合乎规则。
<body bgColor="lightBlue">
<s:form action="Login">
<s:textfield name="userName" label="User Name" />
<s:password name="password" label="Password" />
<s:submit value="Login" />
</s:form>
</body>
public void validate() {
if (getUserName().length() == 0) {
addFieldError("userName", "UserName.required");
} else if (!getUserName().equals("刘利新")) {
addFieldError("userName", "非法用户名");
}
if (getPassword().length() == 0) {
addFieldError("password", getText("password.required"));
}
}
LoginAction.properties
userName.required=user name is required
password.required=\u5BC6\u7801\u4E0D\u80FD\u4E3A\u7A7A
原文:http://javapostsforlearning.blogspot.ca/2012/07/login-page-with-validation-in-struts-2.html
源代码:http://pan.baidu.com/share/link?shareid=1511807048&uk=3878681452