java web项目整体异常处理机制
在实际的j2ee项目中,系统内部难免会出现一些异常,如果把异常放任不管直接打印到浏览器可能会让用户感觉莫名其妙,也有可能让某些用户找到破解系统的方法。
出来工作一年时间了,我也大概对异常处理有了一些了解,在这呢小弟简单介绍下个人对异常处理的见解,抛砖引玉,希望各位大神提出宝贵的意见和建议。
就拿spring+struts2+hibernate项目说明:通常一个页面请求到后台以后,首先是到action(也就是所谓mvc的controller),在action层会调用业务逻辑service,servce层会调用持久层dao获取数据。最后执行结果会汇总到action,然后通过action控制转发到指定页面,执行流程如下图所示:
而这三层其实都有可能发生异常,比如dao层可能会有SQLException,service可能会有NullPointException,action可能会有IOException,一但发生异常并且程序员未做处理,那么该层不会再往下执行,而是向调用自己的方法抛出异常,如果dao、service、action层都未处理异常的话,异常信息会抛到服务器,然后服务器会把异常直接打印到页面,结果就会如下图所示:
其实这种错误对于客户来说毫无意义,因为他们通常是看不懂这是什么意思的。
刚学java的时候,我们处理异常通常两种方法:①直接throws,放任不管;②写try...catch,在catch块中不作任何操作,或者仅仅printStackTrace()把异常打印到控制台。第一种方法最后就造就了上图的结果;而第二种方法更杯具:页面不报错,但是也不执行用户的请求,简单的说,其实这就是bug(委婉点:通常是这样)!
那么发生异常到底应该怎么办呢?我想在大家对java异常有一定了解以后,会知道:异常应该在action控制转发之前尽量处理,同时记录log日志,然后在页面以友好的错误提示告诉用户出错了。大家看下面的代码:
- //创建日志对象
- Log log = LogFactory.getLog(this.getClass());
- //action层执行数据添加操作
- public String save(){
- try{
- //调用service的save方法
- service.save(obj);
- }catch(Exception e){
- log.error(...); //记录log日志
- return "error"; 到指定error页面
- }
- return "success";
- }
如果按照上面的方式处理异常以后,我们用户最后看到的页面可能就会是下面这种形式(我想这种错误提示应该稍微友好点了吧):
然后我们回到刚才处理异常的地方,如果大家积累了一些项目经验以后会发现使用上面那种处理异常的方式可能还不够灵活:
①因为spring把大多数非运行时异常都转换成运行时异常(RuntimeException)最后导致程序员根本不知道什么地方应该进行try...catch操作
②每个方法都重复写try...catch,而且catch块内的代码都很相似,这明显做了很多重复工作而且还很容易出错,同时也加大了单元测试的用例数(项目经理通常喜欢根据代码行来估算UT case)
③发生异常有很多种情况:可能有数据库增删改查错误,可能是文件读写错误,等等。用户觉得每次发生异常都是“访问过程中产生错误,请重试”的提示完全不能说明错误情况,他们希望让异常信息更详尽些,比如:在执行数据删除时发生错误,这样他们可以更准确地给维护人员提供bug信息。
如何解决上面的问题呢?我是这样做的:JDK异常或自定义异常+异常拦截器
struts2拦截器的作用在网上有很多资料,在此不再赘述,我的异常拦截器原理如下图所示:
首先我的action类、service类和dao类如果有必要捕获异常,我都会try...catch,catch块内不记录log,通常是抛出一个新异常,并且注明错误信息:
- //action层执行数据添加操作
- public String save(){
- try{
- //调用service的save方法
- service.save(obj);
- }catch(Exception e){
- //你问我为什么抛出Runtime异常?因为我懒得在方法后写throws xx
- throw new RuntimeException("添加数据时发生错误!",e);
- }
- return "success";
- }
然后在异常拦截器对异常进行处理,看下面的代码:
- public String intercept(ActionInvocation actioninvocation) {
- String result = null; // Action的返回值
- try {
- // 运行被拦截的Action,期间如果发生异常会被catch住
- result = actioninvocation.invoke();
- return result;
- } catch (Exception e) {
- /**
- * 处理异常
- */
- String errorMsg = "未知错误!";
- //通过instanceof判断到底是什么异常类型
- if (e instanceof BaseException) {
- BaseException be = (BaseException) e;
- be.printStackTrace(); //开发时打印异常信息,方便调试
- if(be.getMessage()!=null||Constants.BLANK.equals(be.getMessage().trim())){
- //获得错误信息
- errorMsg = be.getMessage().trim();
- }
- } else if(e instanceof RuntimeException){
- //未知的运行时异常
- RuntimeException re = (RuntimeException)e;
- re.printStackTrace();
- } else{
- //未知的严重异常
- e.printStackTrace();
- }
- //把自定义错误信息
- HttpServletRequest request = (HttpServletRequest) actioninvocation
- .getInvocationContext().get(StrutsStatics.HTTP_REQUEST);
- /**
- * 发送错误消息到页面
- */
- request.setAttribute("errorMsg", errorMsg);
- /**
- * log4j记录日志
- */
- Log log = LogFactory
- .getLog(actioninvocation.getAction().getClass());
- if (e.getCause() != null){
- log.error(errorMsg, e);
- }else{
- log.error(errorMsg, e);
- }
- return "error";
- }// ...end of catch
- }
需要注意的是:在使用instanceof判断异常类型的时候一定要从子到父依次找,比如BaseException继承与RuntimeException,则必须首先判断是否是BaseException再判断是否是RuntimeException。
最后在error JSP页面显示具体的错误消息即可:
- <body>
- <s:if test="%{#request.errorMsg==null}">
- <p>对不起,系统发生了未知的错误</p>
- </s:if>
- <s:else>
- <p>${requestScope.errorMsg}</p>
- </s:else>
- </body>
以上方式可以拦截后台代码所有的异常,但如果出现数据库连接异常时不能被捕获的,大家可以使用struts2的全局异常处理机制来处理:
- <global-results>
- <result name="error" >/Web/common/page/error.jsp</result>
- </global-results>
- <global-exception-mappings>
- <exception-mapping result="error" exception="java.lang.Exception"></exception-mapping>
- </global-exception-mappings>
上面这是一个很简单的异常拦截器,大家可以使用自定义异常,那样会更灵活一些。
以上异常拦截器可以使用其它很多技术替换:比如spring aop,servlet filter等,根据项目实际情况处理。
【补充】ajax也可以进行拦截,但是因为ajax属于异步操作,action通过response形式直接把数据返回给ajax回调函数,如果发生异常,ajax是不会执行页面跳转的,所以必须把错误信息返回给回调函数,我针对json数据的ajax是这样做的:
- /**
- * 读取文件,获取对应错误消息
- */
- HttpServletResponse response = (HttpServletResponse)actioninvocation.getInvocationContext().get(StrutsStatics.HTTP_RESPONSE);
- response.setCharacterEncoding(Constants.ENCODING_UTF8);
- /**
- * 发送错误消息到页面
- */
- PrintWriter out;
- try {
- out = response.getWriter();
- Message msg = new Message(errorMsg);
- //把异常信息转换成json格式返回给前台
- out.print(JSONObject.fromObject(msg).toString());
- } catch (IOException e1) {
- throw e;
- }
java web 中几种异常处理
全局异常映射:
<global-exception-mappings>
<exception-mapping result="error"exception="java.lang.Exception"/>
</global-exception-mappings>
局部异常映射:
<exception-mappings>
<exception-mapping result="error"exception="java.lang.Exception"/>
</exception-mappings>
exception: 异常类型
result:指定Action出现该异常时,系统转入result属性所指向的结果。
异常信息的输出:
<s:propertyvalue="exception"/>输出异常对象本身。
<s:propertyvalue="exception.message"/>输出异常对象的信息。
<s:propertyvalue="exceptionStack"/>输出异常对象本身。
1.业务异常类
1.所以业务异常类派生于BusinessException基类。
2.原则上,要进行相同处理的异常分为一类,用ERROR_CODE标识不同。
3.出错信息统一写在errors.properties,以ERROR_CODE为主键,支持i18N,由基类提供默认的getMessage()函数。
参考BussinessException.java和OrderException.java。
2.Servlet规范里的异常控制
2.1按error-code统一定义错误页面
<error-page>
<error-code>404</error-code>
<location>/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
2.2按异常类型定义单独错误页面
<error-page>
<exception-type>org.sprngside.bookstore.UserNotFound</exception-type>
<location>/userNotFound.jsp</location>
</error-page>
2.3 在JSP里单独定义错误页面
<@ errorPage="error.jsp">
3.Spring MVC里的异常控制
spring-mvc可在xxx-serverlet.xml里定义default和 按Excepiton类型影射的错误页面,和Servlet规范比,主要作了Spring特色的JSP路径转向和日志记录.参见bookstore-servlet.xml
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView" value="/error.jsp"/>
<property name="exceptionMappings">
<props>
<prop key="org.springside.framework.base.BusinessException">/businessError.jsp</prop>
</props>
</property>
</bean>
4. error.jsp的处理
error.jsp会同时处理jsp,servlet,和spring抛过来的异常
其中jsp的异常在exception 变量中.
servlet的异常在(Exception)request.getAttribute("javax.servlet.error.exception")
spring的异常在(Exception) request.getAttribute("exception")
使用 (String)request.getAttribute("javax.servlet.error.request_uri")获得request_uri
使用logger.error(exception.getMessage(), exception); 记录整个异常栈
struts2 全局异常开启日志功能
通常我们在struts.xml的配置中总是会配置全局的异常,然后跳转到某个页面,但是很多情况的时候的异常信息并没有记录到log4j的日志信息中
,这是为什么呢,是因为全局异常对应的拦截器默认日志功能是没有开启的,所以要进行配置才能开启。如下:
- <package name="struts-rootException" extends="struts-default">
- <interceptors>
- <interceptor-stack name="myGlobalExceptionStack">
- <interceptor-ref name="defaultStack">
- <param name="exception.logEnabled">true</param>
- <param name="exception.logLevel">ERROR</param>
- </interceptor-ref>
- </interceptor-stack>
- </interceptors>
- <default-interceptor-ref name="myGlobalExceptionStack"></default-interceptor-ref>
- <global-results>
- <result name="root-exception">/WEB-INF/pages/404.jsp
- </result>
- </global-results>
- <global-exception-mappings>
- <exception-mapping result="root-exception"
- exception="java.lang.Exception"></exception-mapping>
- </global-exception-mappings>
- </package>
<package name="struts-rootException" extends="struts-default">
<interceptors>
<interceptor-stack name="myGlobalExceptionStack">
<interceptor-ref name="defaultStack">
<param name="exception.logEnabled">true</param>
<param name="exception.logLevel">ERROR</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myGlobalExceptionStack"></default-interceptor-ref>
<global-results>
<result name="root-exception">/WEB-INF/pages/404.jsp
</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="root-exception"
exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>
</package>
这里就是设置默认拦截器栈中的开启日志属性,并且设置日志级别是error。
- <interceptor-stack name="myGlobalExceptionStack">
- <interceptor-ref name="defaultStack">
- <param name="exception.logEnabled">true</param>
- <param name="exception.logLevel">ERROR</param>
- </interceptor-ref>
- </interceptor-stack>
<interceptor-stack name="myGlobalExceptionStack">
<interceptor-ref name="defaultStack">
<param name="exception.logEnabled">true</param>
<param name="exception.logLevel">ERROR</param>
</interceptor-ref>
</interceptor-stack>
设置后再将原来的默认拦截器栈起一个名字“myGlobalExceptionStack”,然后再将“myGlobalExceptionStack”设置为默认的拦截器栈:
- <default-interceptor-ref name="myGlobalExceptionStack"></default-interceptor-ref>
<default-interceptor-ref name="myGlobalExceptionStack"></default-interceptor-ref>
此时所有继承这个包“struts-rootException”的其他struts-xxxx.xml文件中的默认拦截器栈就是新的了。
struts2配置session超时,权限访问,异常日志等拦截器
1.Struts配置文件
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 国际化信息配置文件,resource.properties包下的message_info.properties和message_error.properties文件 -->
<constant name="struts.custom.i18n.resources" value="resource.properties.message_error,resource.properties.message_info"></constant>
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<constant name="struts.objectFactory" value="spring"></constant>
<!-- Configuration for the default package. -->
<package name="TJStruts-default" extends="struts-default">
<interceptors>
<!-- session超时拦截 -->
<interceptor name="sessionInterceptor"
class="**.interceptor.SessionInterceptor">
</interceptor>
<!-- 异常日志拦截 -->
<interceptor name="exceptionLogIntercepter"
class="**.interceptor.ExceptionLogIntercepter">
</interceptor>
<!-- url权限拦截 -->
<interceptor name="urlAccessInterceptor"
class="**.interceptor.UrlAccessInterceptor">
</interceptor>
<interceptor-stack name="defaultInteceptorStack">
<interceptor-ref name="exceptionLogIntercepter" />
<interceptor-ref name="sessionInterceptor"/>
<!-- 配置需要进行权限控制的action或模块菜单 -->
<interceptor-ref name="urlAccessInterceptor">
<param name="action">/*/*.action,/*/*.action</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="defaultInteceptorStack" />
<global-results>
<!-- 出错页面跳转 -->
<result name="error">/error.jsp</result>
<!-- 成功页面跳转 -->
<result name="success">/success.jsp</result>
<!-- session超时页面跳转 -->
<result name="sessionTimeout">/sessionTimeout.jsp</result>
<!-- action返回json格式数据的配置 -->
<result name="response-json" type="stream">
<param name="contentType">text/html</param>
<param name="bufferSize">1024</param>
<param name="allowCaching">false</param>
</result>
</global-results>
</package>
<!-- 首页 -->
<!-- 引入其他action配置文件 -->
<include file="**action.xml"></include>
<include file="**action.xml"></include>
</struts>
2.SessionInterceptor超时拦截类
/**
* <p>Description:session超时拦截</p>
*/
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.StrutsStatics;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class SessionInterceptorextends AbstractInterceptor {
private static final long serialVersionUID = 1L;
@Override
public String intercept(ActionInvocation ai) throws Exception {
ActionContext actionContext = ai.getInvocationContext();
//HttpServletResponse response = (HttpServletResponse) actionContext.get(StrutsStatics.HTTP_RESPONSE);
HttpServletRequest request = (HttpServletRequest) actionContext.get(StrutsStatics.HTTP_REQUEST);
HttpSession session = request.getSession();
String path = request.getContextPath();
String uri = request.getRequestURI();
//如果是登录或注销,则不进行判断
String loginAction = path+"/login_action";
String loginoutAction = path+"/loginout.action";
if(uri.startsWith(loginAction) || uri.startsWith(loginoutAction)){
ai.invoke();
}
//session为空 或 登陆用户信息为空
boolean empty = (session==null || session.getAttribute("LOGIN_USER_INFO")==null);
if(empty){
return "sessionTimeout";
}
return ai.invoke();
}
}
注: 权限拦截类和session超时拦截类类似。
Struts2.0中ActionInvocation使用
Interceptor的接口定义没有什么特别的地方,除了init和destory方法以外,intercept方法是实现整个拦截器机制的核心方法。而它所依赖的参数ActionInvocation则是我们之前章节中曾经提到过的著名的Action调度者。
我在这里需要指出的是一个很重要的方法invocation.invoke()。这是ActionInvocation中的方法,而ActionInvocation是Action调度者,所以这个方法具备以下2层含义(详细看DefaultActionInvocation源代码):
1. 如果拦截器堆栈中还有其他的Interceptor,那么invocation.invoke()将调用堆栈中下一个Interceptor的执行。
2. 如果拦截器堆栈中只有Action了,那么invocation.invoke()将调用Action执行。
3.
DefaultActionInvocation部分源代码:
if (interceptors.hasNext()) {
final InterceptorMapping interceptor = (InterceptorMapping) interceptors.next();
UtilTimerStack.profile("interceptor: "+interceptor.getName(),
new UtilTimerStack.ProfilingBlock<String>() {
public String doProfiling() throws Exception {
resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this);//递归调用拦截器
return null;
}
});
} else {
resultCode = invokeActionOnly();
}
每个拦截器中的代码的执行顺序,在Action之前,拦截器的执行顺序与堆栈中定义的一致;而在Action和Result之后,拦截器的执行顺序与堆栈中定义的顺序相反。
Interceptor拦截类型
从上面的分析,我们知道,整个拦截器的核心部分是invocation.invoke()这个函数的调用位置。事实上,我们也正式根据这句代码的调用位置,来进行拦截类型的区分的。在Struts2中,Interceptor的拦截类型,分成以下三类:
1. before
before拦截,是指在拦截器中定义的代码,它们存在于invocation.invoke()代码执行之前。这些代码,将依照拦截器定义的顺序,顺序执行。
2. after
after拦截,是指在拦截器中定义的代码,它们存在于invocation.invoke()代码执行之后。这些代码,将一招拦截器定义的顺序,逆序执行。
PreResultListener
有的时候,before拦截和after拦截对我们来说是不够的,因为我们需要在Action执行完之后,但是还没有回到视图层之前,做一些事情。Struts2同样支持这样的拦截,这种拦截方式,是通过在拦截器中注册一个PreResultListener的接口来实现的。
如:在拦截器中使用如下代码,其中MyPreResultListener实现了PreResultListener 接口并在beforeResult方法中做了一些事情然后在拦截器类中加入action.addPreResultListener(new MyPreResultListener());
从源码中,我们可以看到,我们之前提到的Struts2的Action层的4个不同的层次,在这个方法中都有体现,他们分别是:拦截器(Interceptor)、Action、PreResultListener和Result。在这个方法中,保证了这些层次的有序调用和执行
问题
使用Struts2作为web框架,知道它的拦截器(Interceptor)机制,类似与Filter和Spring的AOP,于是实现了一个为Action增加自定义前置(before)动作和后置动作(after)的拦截器(曰:WInterceptor),不过用一段时间发现,在WInterceptor的after中,对Action对象的属性修改在页面看不到,对请求对象的属性设置也无效。为什么在调用了Action之后(invokeAction())之后,request就不能使用了呢,拦截器不能改变Action的Result么?
问题的关键在于,在调用actionInvocation.invoke()的之后,不仅执行类Action,也执行类Result。因而,等退回到拦截器的调用代码时,Result已经生成,View已经确定,这时你再修改模型(Action的属性)或请求对象的属性,对视图不会有任何影响。
解决办法:
方法一:使用现成的PreResultListener监听器事件
搞清楚原因,卷起袖子干吧,只要让WInterpretor的after事件,放在Result的生成之前就行了。
看看XWork的拦截器接口注入的actionInvocation,其实就提供增加Result执行的前置监听事件-PreResultListener:
- * Register a {@link PreResultListener} to be notified after the Action is executed and
- * before the Result is executed.
- * <p/>
- * The ActionInvocation implementation must guarantee that listeners will be called in
- * the order in which they are registered.
- * <p/>
- * Listener registration and execution does not need to be thread-safe.
- *
- * @param listener the listener to add.
- */
- void addPreResultListener(PreResultListener listener);
因此,让拦截器实现这个接口,就可以自然实现Action执行after事件了。
方法二,实现自己的 ActionInvocation ,手动分离Action和Result的执行
本来前面的方法已经很好了,可是,可是啊,在addPreResultListener里的异常,不会被Struts的框架捕获,而且,addPreResultListener接口不能传递自己的上下文参数,难道动用ThreadLocal传参?
研究了一下XWork的ActionInvocation 接口默认实现类DefaultActionInvocation, 写了一个包装类,将Action的执行和Result的生成完全分开,或许有人用的着,放上来,见附件(ActionInvocationWrapper),如有不妥之处请告知。
exeucteAction是执行Action,executeResult是执行Result
Struts2拦截器原理
struts2版本:2.2.3
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; try { //设置编码 prepare.setEncodingAndLocale(request, response); //创建actionContext prepare.createActionContext(request, response); prepare.assignDispatcherToThread(); //如果不是struts的请求则继续由其它过滤器执行if ( excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) { chain.doFilter(request, response); } else { request = prepare.wrapRequest(request);//包装request,对有文件上传的特殊处理下 ActionMapping mapping = prepare.findActionMapping(request, response, true);//查找对应的ActionMapping if (mapping == null) {//如果找不到ActionMapping则当作静态资源来处理 boolean handled = execute.executeStaticResourceRequest(request, response); if (!handled) { chain.doFilter(request, response); } } else { execute.executeAction(request, response, mapping);//使用ActionMapping来执行action } } } finally { prepare.cleanupRequest(request); } } |
跟踪execute.executeAction(),则到了 org.apache.struts2.dispatcher.Dispatcher,如下:
public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context, ActionMapping mapping) throws ServletException { Map<String, Object> extraContext = createContextMap(request, response, mapping, context); // If there was a previous value stack, then create a new copy and pass it in to be used by the new Action ValueStack stack = (ValueStack) request.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY); boolean nullStack = stack == null; if (nullStack) { ActionContext ctx = ActionContext.getContext(); if (ctx != null) { stack = ctx.getValueStack(); } } if (stack != null) { extraContext.put(ActionContext.VALUE_STACK, valueStackFactory.createValueStack(stack)); } String timerKey = "Handling request from Dispatcher"; try { UtilTimerStack.push(timerKey); String namespace = mapping.getNamespace(); String name = mapping.getName(); String method = mapping.getMethod(); Configuration config = configurationManager.getConfiguration(); //使用StrutsActionProxyFactory(ActionProxyFactory的一个实现 )创建action代理对象ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy( namespace, name, method, extraContext, true, false); request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack()); // if the ActionMapping says to go straight to a result, do it! if (mapping.getResult() != null) { Result result = mapping.getResult(); result.execute(proxy.getInvocation()); } else { proxy.execute();//执行action } // If there was a previous value stack then set it back onto the request if (!nullStack) { request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack); } } catch (ConfigurationException e) { // WW-2874 Only log error if in devMode if(devMode) { String reqStr = request.getRequestURI(); if (request.getQueryString() != null) { reqStr = reqStr + "?" + request.getQueryString(); } LOG.error("Could not find action or result\n" + reqStr, e); } else { LOG.warn("Could not find action or result", e); } sendError(request, response, context, HttpServletResponse.SC_NOT_FOUND, e); } catch (Exception e) { sendError(request, response, context, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e); } finally { UtilTimerStack.pop(timerKey); } } |
DefaultActionProxyFactory创建ActionProxy,在com.opensymphony.xwork2.DefaultActionProxyFactory:
public ActionProxy createActionProxy(String namespace, String actionName, String methodName, Map<String, Object> extraContext, boolean executeResult, boolean cleanupContext) { ActionInvocation inv = new DefaultActionInvocation(extraContext, true); container.inject(inv); return createActionProxy(inv, namespace, actionName, methodName, executeResult, cleanupContext); } |
接下来看看 org.apache.struts2.impl.StrutsActionProxy的execute()方法,如下:
public String execute() throws Exception { ActionContext previous = ActionContext.getContext(); ActionContext.setContext(invocation.getInvocationContext()); try { //这里就是调用拦截器的入口了 return invocation.invoke(); } finally { if (cleanupContext) ActionContext.setContext(previous); } } |
最关键的,com.opensymphony.xwork2.DefaultActionInvocation.invoke()方法,这个DefaultActionInvocation是ActionInvocation的一个实现类,如下:
//保存了执行当前action方法时需要调用的拦截器栈,按照struts.xml中配制的拦截器顺序,从前到后,依次加入到了这个Iterator里面 protected Iterator<InterceptorMapping> interceptors; public String invoke() throws Exception { String profileKey = "invoke: "; try { UtilTimerStack.push(profileKey); if (executed) { throw new IllegalStateException("Action has already executed"); } //如果当前还有下一个,则继续执行拦截器 if (interceptors.hasNext()) { final InterceptorMapping interceptor = (InterceptorMapping) interceptors.next(); String interceptorMsg = "interceptor: " + interceptor.getName(); UtilTimerStack.push(interceptorMsg); try { resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this);//执行拦截器的intercept()方法,并将当前ActionInvocation对象传递给这个方法 } finally { UtilTimerStack.pop(interceptorMsg); } } else { //拦截器全部都执行了,那么最后来执行action,跳出递归了 resultCode = invokeActionOnly(); } // this is needed because the result will be executed, then control will return to the Interceptor, which will // return above and flow through again if (!executed) { if (preResultListeners != null) { for (Object preResultListener : preResultListeners) { PreResultListener listener = (PreResultListener) preResultListener; String _profileKey = "preResultListener: "; try { UtilTimerStack.push(_profileKey); listener.beforeResult(this, resultCode); } finally { UtilTimerStack.pop(_profileKey); } } } // now execute the result, if we're supposed to if (proxy.getExecuteResult()) { executeResult(); } executed = true; } return resultCode; } finally { UtilTimerStack.pop(profileKey); } } |
基本原理到此为止,下面弄个小例子再说明一下:
//拦截器,相当于struts2的拦截器 public interface Interceptor{ String intercept(InvocationContext context); } //很多拦截器的实现 public class ExceptionInterceptor implements Interceptor { public String intercept(InvocationContext context) { // 对异常的处理 System.out.println("\t\t\tExceptionInterceptor 处理异常"); return context.invoke(); } } public class FileUploadInterceptor implements Interceptor { public String intercept(InvocationContext context) { // 处理文件上传相关 System.out.println("\t\t\tFileUploadInterceptor 处理文件上传"); return context.invoke(); } } public class ParameterInterceptor implements Interceptor { public String intercept(InvocationContext context) { // 处理请求的参数 System.out.println("\t\t\tParameterInterceptor 处理请求参数"); return context.invoke(); } } //执行拦截器的invocation上下文,相当于struts2的ActionInvocation public class InvocationContext { // 这里存放当前执行当前action所需要执行的拦截器栈 private Iterator<Interceptor> interceptorIterator = null; private String prefix = ""; public InvocationContext() { // 模拟从配制文件中相应的规则取拦截器栈 ArrayList<Interceptor> list = new ArrayList<Interceptor>(); list.add(new ExceptionInterceptor()); list.add(new FileUploadInterceptor()); list.add(new ParameterInterceptor()); interceptorIterator = list.iterator(); } public String invoke() { // 是否还有拦截器需要执行 if (interceptorIterator.hasNext()) { // 获取下一个需要执行的拦截器 Interceptor interceptor = interceptorIterator.next(); String name = interceptor.getClass().getName(); name = prefix + name; System.out.println(name + " intercept start..."); prefix += "\t"; // Interceptor的所有intercept方法实现里面,最后都调用了InvocationContext.invoke()方法 // 其实就是一个递归,只不过invoke()的下一个递归是在Interceptor.intercept()里面调用的 // 所以说为什么Interceptor.intercept()方法要加个InvocationContext的参数呢,作用就在于此 String result = interceptor.intercept(this); System.out.println(name + " intercept end..."); return result; } else {// 所有的拦截器都执行完了,那就来执行action对应的方法 return executeAction(); } } private String executeAction() { System.out.println(prefix + "executeAction success."); return "success"; } } //模拟请求进行测试 public class Test { public static void main(String[] args) { InvocationContext context = new InvocationContext(); System.out.println("请求开始了..."); context.invoke(); System.out.println("请求处理完了..."); } } |
global-results定义全局的result不起作用
global-results定义全局的result不起作用,注意应extends配置全局result的package
(1)有很多时候一个<result>可供很多<action>使用,这时可以使用<global-results>标签来定义全局的<result>,代码见struts-user.xml。执行顺序:当一个Action返回的String没有相应的<result>与之对应,Struts2就会查找全局的<result>。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
<struts>
//注释:如果其他包中也需要用到这个全局结果集,需要用到package的extends
</struts>
web.xml 中的listener、 filter、servlet 加载顺序及其详解
在项目中总会遇到一些关于加载的优先级问题,近期也同样遇到过类似的,所以自己查找资料总结了下,下面有些是转载其他人的,毕竟人家写的不错,自己也就不重复造轮子了,只是略加点了自己的修饰。
首先可以肯定的是,加载顺序与它们在 web.xml 文件中的先后顺序无关。即不会因为 filter 写在 listener 的前面而会先加载 filter。最终得出的结论是:listener -> filter -> servlet
同时还存在着这样一种配置节:context-param,它用于向 ServletContext 提供键值对,即应用程序上下文信息。我们的 listener, filter 等在初始化时会用到这些上下文中的信息,那么 context-param 配置节是不是应该写在 listener 配置节前呢?实际上 context-param 配置节可写在任意位置,因此真正的加载顺序为:context-param -> listener -> filter -> servlet
对于某类配置节而言,与它们出现的顺序是有关的。以 filter 为例,web.xml 中当然可以定义多个 filter,与 filter 相关的一个配置节是 filter-mapping,这里一定要注意,对于拥有相同 filter-name 的 filter 和 filter-mapping 配置节而言,filter-mapping 必须出现在 filter 之后,否则当解析到 filter-mapping 时,它所对应的 filter-name 还未定义。web 容器启动时初始化每个 filter 时,是按照 filter 配置节出现的顺序来初始化的,当请求资源匹配多个 filter-mapping 时,filter 拦截资源是按照 filter-mapping 配置节出现的顺序来依次调用 doFilter() 方法的。
servlet 同 filter 类似 ,此处不再赘述。
web.xml文件详解
========================================================================
Web.xml常用元素
<web-app>
<display-name></display-name>定义了WEB应用的名字
<description></description> 声明WEB应用的描述信息
<context-param></context-param> context-param元素声明应用范围内的初始化参数。
<filter></filter> 过滤器元素将一个名字与一个实现javax.servlet.Filter接口的类相关联。
<filter-mapping></filter-mapping> 一旦命名了一个过滤器,就要利用filter-mapping元素把它与一个或多个servlet或JSP页面相关联。
<listener></listener>servlet API的版本2.3增加了对事件监听程序的支持,事件监听程序在建立、修改和删除会话或servlet环境时得到通知。
Listener元素指出事件监听程序类。
<servlet></servlet> 在向servlet或JSP页面制定初始化参数或定制URL时,必须首先命名servlet或JSP页面。Servlet元素就是用来完成此项任务的。
<servlet-mapping></servlet-mapping> 服务器一般为servlet提供一个缺省的URL: http://host/webAppPrefix/servlet/ServletName 。
但是,常常会更改这个URL,以便servlet可以访问初始化参数或更容易地处理相对URL。在更改缺省URL时,使用servlet-mapping元素。
<session-config></session-config> 如果某个会话在一定时间内未被访问,服务器可以抛弃它以节省内存。
可通过使用HttpSession的setMaxInactiveInterval方法明确设置单个会话对象的超时值,或者可利用session-config元素制定缺省超时值。
<mime-mapping></mime-mapping>如果Web应用具有想到特殊的文件,希望能保证给他们分配特定的MIME类型,则mime-mapping元素提供这种保证。
<welcome-file-list></welcome-file-list> 指示服务器在收到引用一个目录名而不是文件名的URL时,使用哪个文件。
<error-page></error-page> 在返回特定HTTP状态代码时,或者特定类型的异常被抛出时,能够制定将要显示的页面。
<taglib></taglib> 对标记库描述符文件(Tag Libraryu Descriptor file)指定别名。此功能使你能够更改TLD文件的位置,
而不用编辑使用这些文件的JSP页面。
<resource-env-ref></resource-env-ref>声明与资源相关的一个管理对象。
<resource-ref></resource-ref> 声明一个资源工厂使用的外部资源。
<security-constraint></security-constraint> 制定应该保护的URL。它与login-config元素联合使用
<login-config></login-config> 指定服务器应该怎样给试图访问受保护页面的用户授权。它与sercurity-constraint元素联合使用。
<security-role></security-role>给出安全角色的一个列表,这些角色将出现在servlet元素内的security-role-ref元素
的role-name子元素中。分别地声明角色可使高级IDE处理安全信息更为容易。
<env-entry></env-entry>声明Web应用的环境项。
<ejb-ref></ejb-ref>声明一个EJB的主目录的引用。
< ejb-local-ref></ ejb-local-ref>声明一个EJB的本地主目录的应用。
</web-app>
相应元素配置
1、Web应用图标:指出IDE和GUI工具用来表示Web应用的大图标和小图标
<icon>
<small-icon>/images/app_small.gif</small-icon>
<large-icon>/images/app_large.gif</large-icon>
</icon>
2、Web 应用名称:提供GUI工具可能会用来标记这个特定的Web应用的一个名称
<display-name>Tomcat Example</display-name>
3、Web 应用描述: 给出于此相关的说明性文本
<disciption>Tomcat Example servlets and JSP pages.</disciption>
4、上下文参数:声明应用范围内的初始化参数。
<context-param>
<param-name>ContextParameter</para-name>
<param-value>test</param-value>
<description>It is a test parameter.</description>
</context-param>
在servlet里面可以通过getServletContext().getInitParameter("context/param")得到
5、过滤器配置:将一个名字与一个实现javaxs.servlet.Filter接口的类相关联。
<filter>
<filter-name>setCharacterEncoding</filter-name>
<filter-class>com.myTest.setCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>setCharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
6、监听器配置
<listener>
<listerner-class>listener.SessionListener</listener-class>
</listener>
7、Servlet配置
基本配置
<servlet>
<servlet-name>snoop</servlet-name>
<servlet-class>SnoopServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>snoop</servlet-name>
<url-pattern>/snoop</url-pattern>
</servlet-mapping>
高级配置
<servlet>
<servlet-name>snoop</servlet-name>
<servlet-class>SnoopServlet</servlet-class>
<init-param>
<param-name>foo</param-name>
<param-value>bar</param-value>
</init-param>
<run-as>
<description>Security role for anonymous access</description>
<role-name>tomcat</role-name>
</run-as>
</servlet>
<servlet-mapping>
<servlet-name>snoop</servlet-name>
<url-pattern>/snoop</url-pattern>
</servlet-mapping>
元素说明
<servlet></servlet> 用来声明一个servlet的数据,主要有以下子元素:
<servlet-name></servlet-name> 指定servlet的名称
<servlet-class></servlet-class> 指定servlet的类名称
<jsp-file></jsp-file> 指定web站台中的某个JSP网页的完整路径
<init-param></init-param> 用来定义参数,可有多个init-param。在servlet类中通过getInitParamenter(String name)方法访问初始化参数
<load-on-startup></load-on-startup>指定当Web应用启动时,装载Servlet的次序。
当值为正数或零时:Servlet容器先加载数值小的servlet,再依次加载其他数值大的servlet.
当值为负或未定义:Servlet容器将在Web客户首次访问这个servlet时加载它
<servlet-mapping></servlet-mapping> 用来定义servlet所对应的URL,包含两个子元素
<servlet-name></servlet-name> 指定servlet的名称
<url-pattern></url-pattern> 指定servlet所对应的URL
8、会话超时配置(单位为分钟)
<session-config>
<session-timeout>120</session-timeout>
</session-config>
9、MIME类型配置
<mime-mapping>
<extension>htm</extension>
<mime-type>text/html</mime-type>
</mime-mapping>
10、指定欢迎文件页配置
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
11、配置错误页面
一、 通过错误码来配置error-page
<error-page>
<error-code>404</error-code>
<location>/NotFound.jsp</location>
</error-page>
上面配置了当系统发生404错误时,跳转到错误处理页面NotFound.jsp。
二、通过异常的类型配置error-page
<error-page>
<exception-type>java.lang.NullException</exception-type>
<location>/error.jsp</location>
</error-page>
上面配置了当系统发生java.lang.NullException(即空指针异常)时,跳转到错误处理页面error.jsp
12、TLD配置
<taglib>
<taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>
<taglib-location>/WEB-INF/jsp/debug-taglib.tld</taglib-location>
</taglib>
如果MyEclipse一直在报错,应该把<taglib> 放到 <jsp-config>中
<jsp-config>
<taglib>
<taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>
<taglib-location>/WEB-INF/pager-taglib.tld</taglib-location>
</taglib>
</jsp-config>
13、资源管理对象配置
<resource-env-ref>
<resource-env-ref-name>jms/StockQueue</resource-env-ref-name>
</resource-env-ref>
14、资源工厂配置
<resource-ref>
<res-ref-name>mail/Session</res-ref-name>
<res-type>javax.mail.Session</res-type>
<res-auth>Container</res-auth>
</resource-ref>
配置数据库连接池就可在此配置:
<resource-ref>
<description>JNDI JDBC DataSource of shop</description>
<res-ref-name>jdbc/sample_db</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
15、安全限制配置
<security-constraint>
<display-name>Example Security Constraint</display-name>
<web-resource-collection>
<web-resource-name>Protected Area</web-resource-name>
<url-pattern>/jsp/security/protected/*</url-pattern>
<http-method>DELETE</http-method>
<http-method>GET</http-method>
<http-method>POST</http-method>
<http-method>PUT</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>tomcat</role-name>
<role-name>role1</role-name>
</auth-constraint>
</security-constraint>
16、登陆验证配置
<login-config>
<auth-method>FORM</auth-method>
<realm-name>Example-Based Authentiation Area</realm-name>
<form-login-config>
<form-login-page>/jsp/security/protected/login.jsp</form-login-page>
<form-error-page>/jsp/security/protected/error.jsp</form-error-page>
</form-login-config>
</login-config>
17、安全角色:security-role元素给出安全角色的一个列表,这些角色将出现在servlet元素内的security-role-ref元素的role-name子元素中。
分别地声明角色可使高级IDE处理安全信息更为容易。
<security-role>
<role-name>tomcat</role-name>
</security-role>
18、Web环境参数:env-entry元素声明Web应用的环境项
<env-entry>
<env-entry-name>minExemptions</env-entry-name>
<env-entry-value>1</env-entry-value>
<env-entry-type>java.lang.Integer</env-entry-type>
</env-entry>
19、EJB 声明
<ejb-ref>
<description>Example EJB reference</decription>
<ejb-ref-name>ejb/Account</ejb-ref-name>
<ejb-ref-type>Entity</ejb-ref-type>
<home>com.mycompany.mypackage.AccountHome</home>
<remote>com.mycompany.mypackage.Account</remote>
</ejb-ref>
20、本地EJB声明
<ejb-local-ref>
<description>Example Loacal EJB reference</decription>
<ejb-ref-name>ejb/ProcessOrder</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local-home>com.mycompany.mypackage.ProcessOrderHome</local-home>
<local>com.mycompany.mypackage.ProcessOrder</local>
</ejb-local-ref>
21、配置DWR
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
22、配置Struts
<display-name>Struts Blank Application</display-name>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>application</param-name>
<param-value>ApplicationResources</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Struts Tag Library Descriptors -->
<taglib>
<taglib-uri>struts-bean</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>struts-html</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>struts-nested</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-nested.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>struts-logic</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>struts-tiles</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-tiles.tld</taglib-location>
</taglib>
23、配置Spring(基本上都是在Struts中配置的)
<!-- 指定spring配置文件位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
<!--加载多个spring配置文件 -->
/WEB-INF/applicationContext.xml, /WEB-INF/action-servlet.xml
</param-value>
</context-param>
<!-- 定义SPRING监听器,加载spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>