例如:常常在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);
}
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="simple" />
<!-- 开发模式 -->
<constant name="struts.devMode" value="true" />
<package name="surveyparkPkg" extends="struts-default" namespace="/">
<interceptors>
<!-- 注册登陆拦截器 -->
<interceptor name="loginInterceptor" class="com.atguigu.surveypark.struts2.interceptor.LoginInterceptor" />
<!-- 定义拦截器栈 -->
<interceptor-stack name="surveyparkStack">
<interceptor-ref name="loginInterceptor" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<!-- 定义默认栈 -->
<default-interceptor-ref name="surveyparkStack" />
<!-- 定义全局结果 -->
<global-results>
<result name="login">/index.jsp</result>
</global-results>
<!-- regAction -->
<action name="RegAction_*" class="regAction" method="{1}">
<result name="regPage">/reg.jsp</result>
<result name="input">/reg.jsp</result>
<result name="success">/index.jsp</result>
</action>
<!-- loginAction -->
<action name="LoginAction_*" class="loginAction" method="{1}">
<result name="loginPage">/index.jsp</result>
<result name="input">/index.jsp</result>
<result name="success">/index.jsp</result>
</action>
<!-- SurveyAction -->
<action name="SurveyAction_*" class="surveyAction" method="{1}">
<result name="mySurveyListPage">/mySurveyList.jsp</result>
<result name="designSurveyPage">/designSurvey.jsp</result>
</action>
</package>
</struts>