struts2对action中的方法进行输入校验(2)

时间:2021-08-17 23:59:52

struts2输入校验流程:

1.类型转换器对请求參数运行类型转换,并把转换后的值赋给aciton中的属性

2.假设在运行类型转换的过程中出现异常,系统会将异常信息保存到ActionContext,

conversionError拦截器将异常信息加入到fieldErrors里,无论类型转换是否出现异常,都会进入第三步

3.系统通过反射技术先调用action的validateXXX方法

4.再调用aciton中的validate方法

5.经过上述的4步。假设系统中的fieldErrors存在错误信息,系统自己主动将请求转发至名称为input的视图

假设系统中的fieldErrors没有不论什么错误信息,系统将运行aciton中的处理方法。

也就是说转发至input视图有两个原因:1.类型转换异常

2.输入校验不合法

例如以下:

InvidateAction.java:

package com.itheima.action;

import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;

public class InvidateAction extends ActionSupport{

	private String username;
private String tel;
private Date birthday;
private String msg;
public void setUsername(String username) {
this.username = username;
}
public void setTel(String tel) {
this.tel = tel;
} public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getMsg() {
return msg;
}
public void validate() { }
public String execute1() {
msg = "execute1";
return "success";
}
public String execute2() {
msg = "execute2";
return "success";
}
}

person.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=utf-8">
<title>Insert title here</title>
</head>
<body>
<s:fielderror/>
<form action="${pageContext.request.contextPath }/invidateAction_execute1.action" method="post">
用户名:<input type="text" name="username"><br>
手机号:<input type="text" name="tel"><br>
生日:<input type="text" name="birthday"><br>
<input type="submit" value="提交">
</form>
</body>
</html>

struts2.xml

<action name="invidateAction_*" class="com.itheima.action.InvidateAction" method="{1}">
<result name="success">/success.jsp</result>
<result name="input">/person.jsp</result>
</action>

我在jsp中输入信息例如以下:

struts2对action中的方法进行输入校验(2)

则会提示:struts2对action中的方法进行输入校验(2)

======================================================================================================

解决以上问题能够通过创建Action的类型转换器:

类型转换器教程參见:http://blog.csdn.net/m631521383/article/details/40680723

InvidateAction.java:

package com.itheima.action;

import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;

public class InvidateAction extends ActionSupport{

	private String username;
private String tel;
private Date birthday;
private String msg;
public void setUsername(String username) {
this.username = username;
}
public void setTel(String tel) {
this.tel = tel;
} public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getMsg() {
return msg;
}
public void validate() { }
public String execute1() {
msg = "execute1";
return "success";
}
public String execute2() {
msg = "execute2";
return "success";
}
}

DateTypeConverter.java:

package com.itheima.converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map; import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter; public class DateTypeConverter extends DefaultTypeConverter{ @Override
public Object convertValue(Map<String, Object> context, Object value,
Class toType) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmdd");
if(toType == Date.class) {
String[] strs = (String[])value;
Date date = null;
try {
date = dateFormat.parse(strs[0]);
} catch (ParseException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
return date;
} else if(toType == String.class) {
return dateFormat.format((Date)value);
} return null;
} }

InvidateAction-conversion.properties:

birthday=com.itheima.converter.DateTypeConverter

项目树:

struts2对action中的方法进行输入校验(2)

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbTYzMTUyMTM4Mw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">