复习struts2之输入校验的流程

1。类型转换器对请求参数执行类型转换,并把转换后的值赋给action中的属性。 
2。如果在执行类型转换的过程中出现异常,系统会将异常信息保存到ActionContext,conversionError拦截器将异常信息添加到fieldErrors里。不管类型转换是否出现异常,都会进入第3步。 
3。系统通过反射技术先调用action中的validateXxx()方法,Xxx为方法名。 
4。再调用action中的validate()方法。 
5。经过上面4步,如果系统中的fieldErrors存在错误信息(即存放错误信息的集合的size大于0),系统自动将请求转发至名称为input的视图。如果系统中的fieldErrors没有任何错误信息,系统将执行action中的处理方法。 
例子:在执行类型转换的过程中出现异常,跳转到input视图
HelloWord.java
定义两个属性
	private String username;
	private String mobile;
	private Date birth;
在此省略上面三个属性的set和get方法
public void validateSave() {}
index.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>输入校验</title>
</head>
<body>
	<!-- 用于显示错误信息 -->
	<s:fielderror/>
	<form action="${pageContext.request.contextPath }/test/helloword.do
	" method="post">
		用户名:<input type="text" name="username"/><br/>
		生日:<input type="text" name="birth"/><br/>
		手机号:<input type="text" name="mobile"/><br/>
		<input type="submit" value="提交"/>
	</form>
</body>
</html>

除此之外的配置与上述事例中的是相同的,运行jsp页面,在生日中添加“20011012”,然后点击提交时,会返回input视图,网页信息显示如下:
•	Invalid field value for field "birth".
… …
控制台会抛如下错误:
ognl.MethodFailedException: Method "setBirth" failed for object cn.itcast.action.HelloWordAction@1e3a0ec [java.lang.NoSuchMethodException: cn.itcast.action.HelloWordAction.setBirth([Ljava.lang.String;)]
	at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1292)
	at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:1481)
。。。 。。。
Caused by: java.lang.NoSuchMethodException: cn.itcast.action.HelloWordAction.setBirth([Ljava.lang.String;)
	at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1233)
	... 63 more
/-- Encapsulated exception ------------\
java.lang.NoSuchMethodException: cn.itcast.action.HelloWordAction.setBirth([Ljava.lang.String;)
	at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1233)
。。。 。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值