Strust2优点之全局异常处理器+拦截器

来源:互联网 发布:日本推理电影 知乎 编辑:程序博客网 时间:2024/06/05 11:19

这次记录一下strust2的异常处理器和拦截器,我觉得很好用
一.异常处理器(用于自定义捕捉异常)
实现功能,当发生异常时转到自定义的异常页面(404页面)
1.在strust2的配置文件添加一个引入(引入写404那个文件的xml文件)

<include file="index404.xml"></include>

2.index404.xml

<package name="global" extends="struts-default">`<global-results>    

<result name=”error”type=”redirect”>/default404jsp.jsp</global-results>
//发生异常时捕捉,找到转向的页面“result”

    <global-exception-mappings>            <exception-mapping exception="java.lang.Exception" result="error"/>         </global-exception-mappings></package>

3.任意一个有异常的action页面(例如,算数的异常,当被除数为0的时候报异常,捕捉,并找向配置文件进行处理)
public class DealMathAction extends ActionSupport{

private int i;private int j;public int getI() {    return i;}public void setI(int i) {    this.i = i;}public int getJ() {    return j;}public void setJ(int j) {    this.j = j;}public String execute()throws Exception{    i=8;    j=0;    try{    int result=i/j;    }catch(Exception e){        return "error";     }    return "success";}

}

完成了

二.拦截器(iterceptor)
1.在xml中的配置

<interceptors>    <interceptor name="authority" class="com.xwl.action.AuthorityInterceptor"></interceptor>    <interceptor-stack name="mydefault">        <interceptor-ref name="defaultStack"></interceptor-ref>        <interceptor-ref name="authority"></interceptor-ref>    </interceptor-stack></interceptors><default-interceptor-ref name="mydefault"/>

<!– 设置全局的results,拦截器拦截之后转向login.jsp页面 –>

<global-results>``<result name="login">``/login.jsp`</result>`        <result name="input">/login.jsp</result></global-results>

2.拦截器的action
package com.xwl.action;
import java.util.HashMap;
import java.util.Map;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class AuthorityInterceptor implements Interceptor{

@Overridepublic void destroy() {    // TODO Auto-generated method stub    System.out.println("destory");}@Overridepublic void init() {    // TODO Auto-generated method stub    System.out.println("init");}@Overridepublic String intercept(ActionInvocation invocation) throws Exception {    // TODO Auto-generated method stub    //HashMap<Object, String> map=new HashMap<Object, String>();    //获取这个拦截器    ActionContext actioncontext=invocation.getInvocationContext();    Map session=actioncontext.getSession();    String user=(String) session.get("user");    //假如当前用户已经登录的话(利用session),并且登录的人为xwl的话    if(user!=null&&user.equals("xwl")){        System.out.println("use interceptor");    //调用拦截器的invoke方法,来通过这个拦截器        String result=invocation.invoke();        return result;    }else{        没有登录的话,进行拦截        session.put("msg", "please login");        System.out.println("0.0");        return Action.INPUT;    }   }

}
完成了
另外发表一些自己的感悟,今天使用了Servlet的过滤器和监听器共同实现了对网站权限的设定,使未登陆的用户不能访问主页面等页面,同时使用了监听器对session进行了跟踪,确保系统安全。(觉得过滤器和拦截器基本差不多,但是若是对多个包中的内容进行拦截的话,拦截器反而产生的代码量有点多,因此采用了过滤器实现了基本的功能,也算小小的成功啦)

0 0