strust2的一个拦截器

自定义拦截器
import com.cjy.entity.BgUser;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class LoginInter extends AbstractInterceptor {

	public void init() {
		super.init();
	}

	public void destroy() {
		super.destroy();
	}

	public String intercept(ActionInvocation actionInvocation) {
		try {
			if (actionInvocation.getProxy().getNamespace().equals("/backstage")) {
				return actionInvocation.invoke();
			}
			BgUser user = (BgUser) ActionContext.getContext().getSession().get("bguser");
			if (user == null) {
				return "login";
			}
			else {
				return actionInvocation.invoke();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}

}

配置文件  继承json-default
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
    "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<package name="backstage/module" namespace="/backstage/bgmodule" extends="json-default">
		<action name="Cjy_*" method="{1}" class="com.cjy.backstage.bgmodule.action.ModuleAction">
			<result name="allmodel">../left.jsp</result>
			<result name="modulemanage">modulellist.jsp</result>
			<result name="searchModels">modellist.jsp</result>
			<result name="modulelist">../modulemanage.jsp</result>
			<interceptor-ref name="urlStack"></interceptor-ref>
		</action>
	</package>
</struts>

strust-default
<package name="json-default" extends="struts-default">
<!-- 增加result类型,支持json格式数据返回 -->
   <result-types>
        <result-type name="json" class="com.googlecode.jsonplugin.JSONResult"/>
   </result-types>
   <interceptors>
        	<!-- json拦截器 -->
	<interceptor name="json" class="com.googlecode.jsonplugin.JSONInterceptor" />
	<!--URL 拦截器-->
    <interceptor name="url" class="com.cjy.backstage.login.interceptor.LoginInter" />
    <interceptor-stack name="urlStack">
    <interceptor-ref name="url"></interceptor-ref>
    <interceptor-ref name="defaultStack"></interceptor-ref>
    </interceptor-stack>
  </interceptors>
 	<global-results>
		<result name="login">/backstage/error.jsp</result>
		<result name="error">/error.jsp</result>
	</global-results>
</package>

你可能感兴趣的:(strust2)