首页 > 其他 > 详细

struts 拦截器

时间:2015-12-24 02:16:33      阅读:244      评论:0      收藏:0      [点我收藏+]

?

struts-action-trace.xml

?

 
 <package name="tracePackage" namespace="/trace" extends="exceptionPackage">
         
         <interceptors>
        	<interceptor name="traceAuthInterceptor" class="com.fusionability.web.interceptor.TraceAuthInterceptor"></interceptor>
            <interceptor-stack name="traceAuthInterceptorStack">
	            <interceptor-ref name="defaultStack"></interceptor-ref>
	            <interceptor-ref name="traceAuthInterceptor"></interceptor-ref>
            </interceptor-stack>
        </interceptors>
        <default-interceptor-ref name="traceAuthInterceptorStack"></default-interceptor-ref>
         
         
        <action name="showAppList" class="wKJSAppAction" method="showAppList">
            <result name="success">/application/application_list.jsp</result>
        </action>
</package>

?

?

package com.fusionability.web.interceptor;
 

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext; 

import com.fusionability.user.service.UserService;
import com.fusionability.web.bean.User;
import com.fusionability.web.bean.UserAppRelation;
import com.fusionability.web.utils.Constants; 
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
/**
 * @author baoyou E-mail:curiousby@163.com
 * @version 2015年12月23日 下午2:47:10
 *
 * desc: ...
 */
@SuppressWarnings("serial")
public class TraceAuthInterceptor extends AbstractInterceptor {
	private static Logger log = Logger.getLogger(TraceAuthInterceptor.class);

	private UserService userService;

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
        
		User user = (User)ServletActionContext.getRequest().getSession().getAttribute(Constants.SESSION_USER);
		
		if (user == null) {
			return Action.LOGIN;
		}else{
			if(  !"/FusionAbility/trace/showAppList.action".equals(ServletActionContext.getRequest().getRequestURI())){
				try { 
					String appid = ServletActionContext.getRequest().getParameter("appid");
					UserAppRelation userAppRelation = userService.getUserAppRelation(user.getUserid(), appid);
		
					if (userAppRelation != null) {
						return invocation.invoke();
					}
					return "error";
				} catch (Exception e) {
					//e.printStackTrace();
					return "error";
				}
			}
			return invocation.invoke();
		}
		
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}

?

?

?

?

?

?

?

?

?

?

struts 拦截器

原文:http://knight-black-bob.iteye.com/blog/2266046

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!