首页 > 代码库 > struts2 在拦截器进行注入(根据Action是否实现自定义接口)

struts2 在拦截器进行注入(根据Action是否实现自定义接口)

例如:常常在Action中都需要获取当前登录的User,就需要获取Session,然后从Session获取当前登录的User,因为这些步骤都是重复操作,可以想办法在拦截器中进行实现,可以自定义一个接口,只要你的Action实现了这个接口,就在自定义拦截器中进行注入。即从拦截器中获取Session,然后设置进行注入。


简单的例子:

一个自定义接口,只要Action实现这个接口,就在拦截器中进行注入

package com.atguigu.surveypark.struts2;

import com.atguigu.surveypark.model.User;

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


一个自定义拦截器(登录拦截器):在拦截器中获取action的实例,如果实现上面的接口就进行注入。即调用接口的方法。
package com.atguigu.surveypark.struts2.interceptor;

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

/**
 * 登陆拦截器
 */
public class LoginInterceptor implements Interceptor {

	private static final long serialVersionUID = 4230211839075439660L;

	public void destroy() {
	}

	public void init() {
	}

	@SuppressWarnings("rawtypes")
	public String intercept(ActionInvocation arg0) throws Exception {
		BaseAction action = (BaseAction) arg0.getAction();
		if(action instanceof LoginAction
				|| action instanceof RegAction){
			return arg0.invoke();
		}
		else{
			User user = (User) arg0.getInvocationContext().getSession().get("user");
			if(user == null){
				//去登陆
				return "login" ;
			}
			else{
				//放行
				if(action instanceof UserAware){
					//注入user给action
					((UserAware)action).setUser(user);
				}
				return arg0.invoke();
			}
		}
	}
}

Action:一个实现接口的Action

package com.atguigu.surveypark.struts2.action;

import java.util.List;

import javax.annotation.Resource;

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

import com.atguigu.surveypark.model.Survey;
import com.atguigu.surveypark.model.User;
import com.atguigu.surveypark.service.SurveyService;
import com.atguigu.surveypark.struts2.UserAware;

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

	private static final long serialVersionUID = 2438909978838628762L;
	
	//注入SurveyService
	@Resource
	private SurveyService surveyService ;

	//调查集合
	private List<Survey> mySurveys ;

	//接受user对象
	private User user;
	
	public List<Survey> getMySurveys() {
		return mySurveys;
	}

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

	/**
	 * 查询我的调查列表
	 */
	public String mySurveys(){
		this.mySurveys = surveyService.findMySurveys(user);
		return "mySurveyListPage" ;
	}
	
	/**
	 * 新建调查
	 */
	public String newSurvey(){
		this.model = surveyService.newSurvey(user);
		return "designSurveyPage" ;
	}

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

struts.xml:拦截器注册

<?xml version="1.0"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
	<!-- 主题 -->
	<constant name="struts.ui.theme" value=http://www.mamicode.com/"simple" />>