首页 > 代码库 > Struts学习笔记_拦截器
Struts学习笔记_拦截器
1.Struts架构图
2.Struts执行过程分析
3.Interceptor拦截器过程模拟
//main.javapublic class Main { public static void main(String[] args) { new ActionInvocation().invoke(); }}
//ActionInvocation .javapublic class ActionInvocation { List<Interceptor> interceptors = new ArrayList<Interceptor>(); int index = -1; Action a = new Action(); public ActionInvocation() { this.interceptors.add(new FirstInterceptor()); this.interceptors.add(new SecondInterceptor()); } public void invoke() { index ++; if(index >= this.interceptors.size()) { a.execute(); }else { this.interceptors.get(index).intercept(this); } }}
//FirstInterceptor .javapublic class FirstInterceptor implements Interceptor { public void intercept(ActionInvocation invocation) { System.out.println(1); invocation.invoke(); System.out.println(-1); }}
//SecondInterceptor .javapublic class SecondInterceptor implements Interceptor { public void intercept(ActionInvocation invocation) { System.out.println(2); invocation.invoke(); System.out.println(-2); }}
//Interceptor .javapublic interface Interceptor { public void intercept(ActionInvocation invocation) ;}
//Action .javapublic class Action { public void execute() { System.out.println("execute!"); }}
4.定义自己的拦截器(不常用)
//MyInterceptor .javapublic class MyInterceptor implements Interceptor { public void destroy() { // TODO Auto-generated method stub } public void init() { // TODO Auto-generated method stub } public String intercept(ActionInvocation invocation) throws Exception { long start = System.currentTimeMillis(); String r = invocation.invoke(); long end = System.currentTimeMillis(); System.out.println("action time = " + (end - start)); return r; }}
<struts> <constant name="struts.devMode" value="true"></constant> <package name="test" namespace="/" extends="struts-default"> <interceptors> <interceptor name="my" class="com.bjsxt.interceptor.MyInterceptor"></interceptor> </interceptors> <action name="test" class="com.bjsxt.action.TestAction"> <result>/test.jsp</result> <interceptor-ref name="my"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </action> </package></struts>
5.使用token拦截器控制重复提交(很少用)
6. 类型转换
写自己的转换器:
//方法一
public class MyPointConverter extends DefaultTypeConverter{ @Override public Object convertValue(Object value, Class toType) { if(toType == Point.class) { Point p = new Point(); String[] strs = (String[])value; String[] xy = strs[0].split(","); p.x = Integer.parseInt(xy[0]); p.y = Integer.parseInt(xy[1]); return p; } if(toType == String.class) { return value.toString(); } return super.convertValue(value, toType); } }
//方法二
public class MyPointConverter extends StrutsTypeConverter{
@Override
public Object convertFromString(Map context, String[] values, Class toClass) {
Point p = new Point();
String[] strs = (String[])values;
String[] xy = strs[0].split(",");
p.x = Integer.parseInt(xy[0]);
p.y = Integer.parseInt(xy[1]);
return p;
}
@Override
public String convertToString(Map context, Object o) {
// TODO Auto-generated method stub
return o.toString();
}
}
三种注册方式:
i. 局部:XXXAction-conversion.properties
p(属性名称) = converter
ii. 全局:xwork-conversion.properties
com.xxx.XXX(类名)= converter (java.awt.Point=com.bjsxt.converter.MyPointConverter)
iii. Annotation
如果遇到非常麻烦的映射转换
- i. request.setAttribute();
- ii. session
Struts学习笔记_拦截器
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。