Struts2的输入校验续一
说明:
遇到
类型转换错误的时候(也就是说不能进行类型转换),
struts2框架自动生成一条错误信息,并且将该错误信息放到addFieldError里面。
类型转换与输入校验的流程:
1. 首先Struts2对客户端传来的数据进行类型转换
2. 类型转换完毕后再进行输入校验
3. 如果类型转换和输入校验都没有错误发生,那么进入execute方法(调用
商业逻辑)
注意:如果类型转换不成功,也同样要进行输入校验
一、修改注册页面register.jsp:
<body>
<table align="center" width="40%">
<tr>
<td style="color:red">
<s:fielderror></s:fielderror>
</td>
</tr>
</table>
<form action="register.action" method="post">
<table align="center" width="40%" border="1">
<tr>
<td>username:</td>
<td><input type="text" name="username" value="${username}"></td>
</tr>
<tr>
<td>password:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td>repassword:</td>
<td><input type="password" name="repassword"></td>
</tr>
<tr>
<td>age:</td>
<td><input type="text" name="age"></td>
</tr>
<tr>
<td>birthday:</td>
<td><input type="text" name="birthday"></td>
</tr>
<tr>
<td>graduation:</td>
<td><input type="text" name="graduation"></td>
</tr>
<tr>
<td><input type="submit" value=" submit "></td>
<td><input type="reset" value=" reset "></td>
</tr>
</table>
</form>
</body>
说明:年龄输入的为字母格式的字符是,struts2会无法进行自动的类型转换,所以会提示“Invalid field value for field "age". ”这样的错误,这种提示信息是struts2默认的提示错误信息,是不够友好的,所以要自己定义新的提示信息,把struts默认的替换掉:
二、在src根目录下创建一个
全局性的国际化的文件message.properties(这种方法还是有缺陷的):
xwork.default.invalid.fieldvalue={0} error
其中:
xwork.default.invalid.fieldvalue是固定的,0表示的是哪个属性发生类型转化错误就显示哪个的属性。
三、在struts.xml文件中进行配置,代码如下:
<constant name="struts.custom.i18n.resources" value="message">
</constant>
这里的message就是上面定义的message.properties文件。
四、要在对应的Action的目录com.test.Action下创建局部的国际化的验证文件RegisterAction.properties:
invalid.fieldvalue.age=age conversion error
还是age的类型转化为例的。
注意:在正式的项目开发中一般使用的都是局部的类型转化验证。
五、用struts2标签来创建注册页面,重新创建一个struts2标签的注册页面register2.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'register2.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<s:form action="register">
<s:textfield name="username" label="username"></s:textfield>
<s:password name="password" label="password"></s:password>
<s:password name="repassword" label="repassword"></s:password>
<s:textfield name="age" label="age"></s:textfield>
<s:textfield name="birthday" label="birthday"></s:textfield>
<s:textfield name="graduation" label="graduation"></s:textfield>
<s:submit value=" submit "></s:submit>
</s:form>
</body>
</html>
六、修改下struts.xml的文件:
<action name="register" class="com.test.action.RegisterAction">
<result name="success">/success.jsp</result>
<result name="input">/register2.jsp</result>
</action>
七、完善验证的信息RegisterAction.properties:
invalid.fieldvalue.age=\u5e74\u9f84\u4fe1\u606f\u8f93\u5165\u4e0d\u6b63\u786e
invalid.fieldvalue.birthday=\u751f\u65e5\u65e5\u671f\u8f93\u5165\u9519\u8bef
invalid.fieldvalue.graduation=\u6bd5\u4e1a\u65f6\u95f4\u65e5\u671f\u8f93\u5165\u9519\u8bef
八、因为验证有不合适的地方(重复),所以修改一下RegisterAction.java的验证:
package com.test.action;
import java.util.Calendar;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
public class RegisterAction extends ActionSupport {
private String username;
private String password;
private String repassword;
private int age;
private Date birthday;
private Date graduation;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRepassword() {
return repassword;
}
public void setRepassword(String repassword) {
this.repassword = repassword;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Date getGraduation() {
return graduation;
}
public void setGraduation(Date graduation) {
this.graduation = graduation;
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
@Override
public void validate() {
if(null == username || username.length() < 6 || username.length() > 10){
this.addFieldError("username", "username invalid");
}
if(null == password || password.length() <6 || password.length() > 10){
this.addFieldError("password", "password invalid");
}
else if(null == repassword || repassword.length() < 6 || repassword.length() > 10){
this.addFieldError("repassword", "repassword invalid");
}
else if(!password.equals(repassword)){
this.addFieldError("password", "two passwords not the same");
}
if(age <= 0 || age > 150){
this.addFieldError("age", "age should be between 1 and 150");
}
// if(null == birthday){
// this.addFieldError("birthday", "birthday invalid");
// }
// if(null == graduation){
// this.addFieldError("graduation", "graduation invalid");
// }
if(null != birthday && null != graduation){
Calendar c1 = Calendar.getInstance();
c1.setTime(birthday);
Calendar c2 = Calendar.getInstance();
c2.setTime(graduation);
if(!c1.before(c2)){
this.addFieldError("birthday", "birthday should be before graduation");
}
}
}
}
最终的文件如下: