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="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>
struts2 在拦截器进行注入(依据Action是否实现自己定义接口)的更多相关文章
- Struts2默认拦截器栈及内建拦截器使用具体解释
Struts2内建拦截器介绍: alias (别名拦截器):同意參数在跨越多个请求时使用不同别名,该拦截器可将多个Action採用不同名字链接起来,然后用于处理同一信息. autowiring ...
- 简单理解Struts2中拦截器与过滤器的区别及执行顺序
简单理解Struts2中拦截器与过滤器的区别及执行顺序 当接收到一个httprequest , a) 当外部的httpservletrequest到来时 b) 初始到了servlet容器 传递给一个标 ...
- struts2之拦截器
1. 为什么需要拦截器 早期MVC框架将一些通用操作写死在核心控制器中,致使框架灵活性不足.可扩展性降低, Struts 2将核心功能放到多个拦截器中实现,拦截器可自由选择和组合,增强了灵活性,有利于 ...
- 第九篇——Struts2的拦截器
拦截器: Struts2大多数核心功能都是通过拦截器实现的,每个拦截器完成某项功能: 拦截器方法在Action执行之前或之后执行. 工作原理: 拦截器的执行过程是一个递归的过程 action请求--& ...
- Struts2的拦截器是如何使用AOP工作的
拦截器(interceptor)是Struts2最强大的特性之一,也可以说是struts2的核心,拦截器可以让你在Action和result被执行之前或之后进行一些处理.同时,拦截器也可以让你将通用的 ...
- Struts2 利用拦截器 interceptor 控制登陆和访问权限
最近学习了Struts2的登录和权限控制用到的是拦截器,需要在struts.xml中配置,每个action都默认的继承defaultStack,如果你用了别的拦截器,还需要手动引入defaultSta ...
- 谈谈 Struts2 的拦截器
套话 相信非常多人都用过 Struts2 了,当然,对 Struts2 的原理也都比較了解.之前在一个项目中就已经用到了,当初的理解也不过局限在应用的层面上,对于更深层次的原理.机制,了解的并非非常多 ...
- java之struts2之拦截器
1.struts2能完成数据的设置,数据的封装,数据的类型转换,数据的校验等等.struts2是如何来完成这些功能的?struts2的所有功能都是由拦截器来完成的. 2.拦截器是struts2的核心. ...
- 12.Struts2自定义拦截器
12.自定义拦截器 拦截器是Struts2的一个重要特性.因为Struts2的大多数核心功能都是通过拦截器实现的. 拦截器之所以称之为“拦截器”,是因为它可以拦截Action方法的执行, ...
随机推荐
- appium+python自动化46-安装app三种方式
前言 adb安装 1.在app自动化之前,首先手机上有要被测试的app,如何把电脑本地上的app安装到手机上呢?可以在运行自动化代码前,在cmd输入adb指令,把电脑app安装到手机上 adb ins ...
- python3 str和bytes之间转换
a bytes-like object is required, not 'str' 碰到 这个错误 ,是因为需要是的bytes,不是str bytes -> str: 1 str-> ...
- 使用Apache POI导出Excel小结--导出XLS格式文档
使用Apache POI导出Excel小结 关于使用Apache POI导出Excel我大概会分三篇文章去写 使用Apache POI导出Excel小结--导出XLS格式文档 使用Apache POI ...
- Neural Networks for Machine Learning by Geoffrey Hinton (4)
一种能够学习家谱关系的简单神经网络 血缘一共同拥有12种关系: son, daughter, nephew, niece, father, mother, uncle, aunt, brother, ...
- IP编址
IP地址 /include/linux/inetdevice.h,定义IPV4专用的网络设备相关的结构.宏等 /net/ipv4/devinet.c.支持IPV4特性的设备操作接口 数据组织 net_ ...
- 天台人满为患,不如来看下这个Ramnit蠕虫DesktopLayer.exe分析
今年的世界杯越来越看不懂,想去天台吹吹风都不一定有位置,心凉了,事儿还得做,先从网上抓个可疑样本压压惊!上手分析才发现并没有我想得那么简单…… 一.基本信息 MD5 ff5e1f27193ce51ee ...
- windows下 memcached 和 redis 服务器安装
memcached 安装: 1.下载memcached 文件: 2.拷贝到运行目录: 3.命令行进入到程序目录: 运行命令: memcached -d install 如果没有报错说明安装成功 4.打 ...
- Linux C高级编程——文件操作之系统调用
Linux C高级编程文件操作之系统调用 宗旨:技术的学习是有限的,分享的精神是无限的. 库函数是一些完毕特定功能的函数.一般由某个标准组织制作公布,并形成一定的标准.使用库函数编 ...
- django中根据模型生成页面的脚手架app-groundwork
相信做过Asp.net MVC的朋友对在此框架下,根据模型自动生成浏览,编辑,查看,删除的四个页面的脚手架功能记忆尤新,那么我们在用python中的django框架时,有没有此脚手架功能呢,很显然,默 ...
- jsp页面数据回显(select下拉选择框)
1.静态变量方式: <!-- 实现select标签回显 --> 1.<select name="curStatus" value="${curStatu ...