首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

struts2源码阅览-三个核心之拦截器

2012-06-28 
struts2源码阅读-三个核心之拦截器晚上回家看struts2 in acion ,这本书也买了快一年了,以前还真没仔细看过

struts2源码阅读-三个核心之拦截器
    晚上回家看struts2 in acion ,这本书也买了快一年了,以前还真没仔细看过,以前看过的strut2的书也只是教怎么快速入门罢了,现在顿觉很有感触。
    用了struts2快一年,从xml配置方式到annotation,比如现在项目遇到的不同的使用struts2的方式,基于modelDriver方式,零配置,注解权限控制,一直只是用,遇到问题跟踪下代码,没怎么去学习里面的东西。


拦截器
  没有拦截器就没有struts2,学好struts2的必须把这个先拿下。
首先拦截器是类似filter,也是责任链方式,但是filter问题在于只要配置,每个链接过来都要处理,拦截器可以给特定的action包下面的拦截器配置上,需要的链接请求或者逻辑才进行处理,省了不少性能


看框架使用:
首先是StrutsPrepareAndExecuteFilter配置在xml中的入口,顺着这个filter再debug下,发现好多东东,远远超过看什么书,然后去积累的强。
  简单的比如:有别的filter要先处理就配置在前面,然后再chain这个,不然会出问题
public class ExceptionInterceptor implements Interceptor {protected final Log log = LogFactory.getLog(ExceptionInterceptor.class);@Overridepublic void init() {log.info("Exception Interceptor start init");}@Overridepublic String intercept(ActionInvocation invocation) throws Exception {String result;try{result=invocation.invoke();}catch (Exception e) { result="exceptionPage";log.error("exception page ", e);}return result;}@Overridepublic void destroy() {log.info("Exception Interceptor destroy");}}
    默认开启的拦截器有很多特色,也导致struts2有了根据项目需要可以有很多使用方式。



 

热点排行