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)
。。。 。。。
复习struts2之输入校验的流程
最新推荐文章于 2020-01-06 22:51:34 发布