Struts2登陆拦截器

阅读更多
package com.nantian.surveypark.struts2.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.nantian.surveypark.domain.Survey;
import com.nantian.surveypark.domain.User;
import com.nantian.surveypark.service.SurveyService;

/**
*
*/
@Controller
@Scope("prototype")
public class SurveyAction extends BaseAction implements UserAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Resource
	private SurveyService surveyService;
	// 接受session的user对象
	private User user;
	private List mySurveys;

	public List getMySurveys() {
		return mySurveys;
	}

	public void setMySurveys(List mySurveys) {
		this.mySurveys = mySurveys;
	}

	/*
	 * 新建调查
	 */
	public String newSurvey() {
		this.model = surveyService.newSurvey(user);
		return "designSurveyPage";
	}

	/**
	 * 查询我的调查
	 */
	public String mySurveys() {
		this.mySurveys = surveyService.findMySurveys(user);
		return "mySurveyListPage";
	}

	// 注入user对象
	public void setUser(User user) {
		this.user = user;
	}

}

 

package com.nantian.surveypark.struts2.action;

import com.nantian.surveypark.domain.User;

/**
 * 用户关注接口
 */
public interface UserAware {
	public void setUser(User user);
}

 

package com.nantian.surveypark.struts2.interceptor;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.nantian.surveypark.domain.User;
import com.nantian.surveypark.struts2.action.BaseAction;
import com.nantian.surveypark.struts2.action.LoginAction;
import com.nantian.surveypark.struts2.action.RegAction;
import com.nantian.surveypark.struts2.action.UserAware;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

/**
 * LoginInterceptor
 */
public class LoginInterceptor implements Interceptor {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2780249243216901184L;

	public void destroy() {

	}

	public void init() {

	}

	@SuppressWarnings("rawtypes")
	public String intercept(ActionInvocation invocation) throws Exception {
		BaseAction action = (BaseAction) invocation.getAction();
		if (action instanceof LoginAction || action instanceof RegAction) {
			return invocation.invoke();
		}
		// 登录判断
		else {
			HttpSession s = ServletActionContext.getRequest().getSession();
			User user = (User) s.getAttribute("user");
			if (user == null) {
				return "login";
			} else {
				// 处理action的user注入问题
				if (action instanceof UserAware) {
					((UserAware) action).setUser(user);
				}
				return invocation.invoke();
			}
		}
	}

}

 




	
	
	
	
	
		
			
			
			
			
				
				
					true
				
			
		
		
		
		
		
			/index.jsp
		
		
		
			/reg.jsp
			/index.jsp
			/reg.jsp
		
		
		
			/index.jsp
			/index.jsp
			/index.jsp
		
		
		
			/designSurvey.jsp
			/mySurveyList.jsp
		
	

 

你可能感兴趣的:(struts2,登陆,拦截器)