首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

struts2学习札记十(第10讲.Struts2的核心 拦截器)

2012-08-22 
struts2学习笔记十(第10讲.Struts2的核心 拦截器)Struts2的核心 拦截器一、修改RegisterAction.java类,在va

struts2学习笔记十(第10讲.Struts2的核心 拦截器)
Struts2的核心 拦截器

一、修改RegisterAction.java类,在validate方法中添加一条错误信息语句this.addFieldError("username", "aaaaaaaaaaaaaaa");:

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;}@Overridepublic String execute() throws Exception {return SUCCESS;}//public String abc() throws Exception {////System.out.println("abc() method invoked");//return SUCCESS;//}////public void validateAbc(){////System.out.println("validateAbc() invoked");//}@Overridepublic void validate() {System.out.println("validate~~~~~~~~~~~~~~");this.addFieldError("username", "aaaaaaaaaaaaaaa");//if(null == username || username.length() < 6 || username.length() > 10){//this.addActionError("username invalid");//}//if(null == password || password.length() <6 || password.length() > 10){//this.addActionError("password invalid");//}//else if(null == repassword || repassword.length() < 6 || repassword.length() > 10){//this.addActionError("repassword invalid");//}//else if(!password.equals(repassword)){//this.addFieldError("password", "two passwords not the same");//}//if(age <= 0 || age > 150){//this.addActionError("age should be between 1 and 150");//}////if(null != birthday && null != graduation){//Calendar c1 = Calendar.getInstance();//c1.setTime(birthday);////Calendar c2 = Calendar.getInstance();//c2.setTime(graduation);////if(!c1.before(c2)){//this.addActionError("birthday should be before graduation");//}//}}}

当表单所有的输入都为空,会出现下面的效果:


说明:不管是何种验证方式,最终的错误信息都会放到fielderror里面去,最终出现验证重复的现象。
笔记:
1、真正存放field级别的错误信息的对象是LinkedHashMap
2、该LinkedHashMap的key是String类型的,value是ArrayList类型的
3、对于Action级别的错误信息实际上是放置在ArrayList中的
4、拦截器:Interceptor
功能:拦截器的实现机制:
二、在src下创建com.interceptor包,并在这个包下创建一个Target类和一个TargetInterface接口:
Target.java:
package com.interceptor;public class Target implements TargetInterface{public void doSomething(){System.out.println("do something");}}

TargetInterface.java:
package com.interceptor;public interface TargetInterface {public void doSomething();}

三、在同一目录下创建一个拦截器Interceptor.java:
package com.interceptor;public class Intercepoter {public void before(){System.out.println("before");}public void after(){System.out.println("after");}}

四、在同一目录下再创建一个处理器类MyHandker.java:
package com.interceptor;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;public class MyHandler implements InvocationHandler {private Object object;private Interceptor interceptor = new Interceptor();public void setObject(Object object){this.object=object;}public Object invoke(Object proxy, Method method, Object[] args)throws Throwable {Object result = null;interceptor.before();result = method.invoke(object, args);interceptor.after();return result;}}

五、再创建一个代理类MyProxy.java类(作用是为了产生一个代理):
package com.interceptor;import java.lang.reflect.Proxy;public class MyProxy {public Object getProxy(Object object){MyHandler myHandler = new MyHandler();myHandler.setObject(object);return Proxy.newProxyInstance(Target.class.getClassLoader(), object.getClass().getInterfaces(), myHandler);}}

六、再创建一个Client.java类:
package com.interceptor;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;public class MyHandler implements InvocationHandler {private Object object;private Interceptor interceptor = new Interceptor();public void setObject(Object object){this.object=object;}public Object invoke(Object proxy, Method method, Object[] args)throws Throwable {Object result = null;interceptor.before();result = method.invoke(object, args);interceptor.after();return result;}}

热点排行