自定义拦截器

源码位置

struts2_day02_web

自定义拦截器

1 在struts2里面有很多的拦截器,这些拦截器是struts2封装的功能,但是在实际开发中,struts2里面的拦截器中可以没有要使用的功能,这个时候需要自己写拦截器实现功能
2 拦截器结构
(1)查看源代码看拦截器结构
- 继承类

随便看一个默认拦击诶器

在接口里面有三个方法

初始化操作

销毁

拦截逻辑的操作

(2)开发中,建议使用另外一种方式
- 写类,继承 MethodFilterInterceptor类实现
-- 让action里面某个的方法不进行拦截
(3)让拦截器和action有关系
- 不是在action调用拦截器的方法,而是通过配置文件方式让建立关系

自定义登录拦截器

1 需求:在项目中,有很多的action的超链接,实现只有是登录的状态,才可以点击action的超链接实现功能,如果不是登录状态,点击action超链接返回到登录页面
2 登录的状态:使用session域对象实现
(1)登录成功之后,把数据放到session里面
(2)判断session是否有值,可以知道是否是登录状态
3 实现登录的基本功能
(1)查询数据库判断用户名和密码(作业)

public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{

	//使用模型驱动获取表单数据
	private Customer customer = new Customer();
	public Customer getModel() {
		return customer;
	}
	
	//登录的方法
	public String login() {
		//1 得到request对象
		HttpServletRequest request = ServletActionContext.getRequest();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		//2 作业:查询数据库判断用户名和密码是否正确
		// 用户名 admin  密码 250
		if("admin".equals(username) && "250".equals(password)) {
			//成功
			//向session里面放值
			request.getSession().setAttribute("username", username);
			return "loginsuccess";
			
		} else {
			//失败
			return "login";
		}
	}
	
<!-- 登录操作 -->
<result name="loginsuccess">/index.htm</result>
<result name="login">/login.jsp</result>

 

 

或者使用一种更加完整的写法

4 添加登录拦截器功能
(1)判断是否登录:判断session里面是否有名称是username的值
(2)拦截器实现过程
第一步 创建类,继承MethodFilterInterceptor类
第二步 重写MethodFilterInterceptor类里面的方法写拦截器逻辑


/**
 * 拦截器类
 * @author asus
 *
 */
public class LoginInterceptor extends MethodFilterInterceptor {

	//这个方法里面写拦截器逻辑
	protected String doIntercept(ActionInvocation invocation) throws Exception {
		// 判断session里面是否有名称是username的值
		// 得到session
		HttpServletRequest request = ServletActionContext.getRequest();
		Object obj = request.getSession().getAttribute("username");
		//判断
		if(obj != null) {
			//登录状态
			//做类似于放行操作,执行action的方法
			return invocation.invoke();
		} else {
			//不是登录状态
			//不到登录,不执行action的方法,返回登录页面
			//到result标签里面找到名称是login的值,到配置路径里面
			return "login";
		}
	}

}

 

第三步 配置action和拦截器关系(注册拦截器)
(1)在要拦截的action标签所在的package标签里面声明拦截器

由于是对CustomerAction 进行拦截,所以拦截器要写在里面

<package name="demo" extends="struts-default" namespace="/">
		<!-- 1 声明拦截器 -->
		<interceptors>
			<interceptor name="loginintercept" class="cn.itcast.interceptor.LoginInterceptor"></interceptor>
		</interceptors>
		
		<action name="customer_*" class="cn.itcast.action.CustomerAction" method="{1}">
		
			<!-- 2 使用自定义拦截器 -->
			<interceptor-ref name="loginintercept">
				<!-- 配置action里面某些方法不进行拦截
					name属性值: excludeMethods
					值:action不拦截的方法名称
				 -->
				<param name="excludeMethods">login</param>
			</interceptor-ref>
			
			<!-- 3 把默认拦截器手动使用一次 -->
			<interceptor-ref name="defaultStack"></interceptor-ref>
			
			<result name="list">/jsp/customer/list.jsp</result>
			<!-- 到添加页面 -->
			<result name="toAddPage">/jsp/customer/add.jsp</result>
			<!-- 添加之后操作 到列表页面
				请求一次列表的action
				type属性:chain不用缓存问题,一般使用redirectAction重定向到action
			-->
			<result name="addCustomer" type="redirectAction">customer_list</result>
			<!-- 登录操作 -->
			<result name="loginsuccess">/index.htm</result>
			<result name="login">/login.jsp</result>
		</action>
	</package>

 

(3)struts2里面执行很多的默认拦截器,但是如果在action里面配置自定义拦截器,
问题:默认的拦截器不会执行了
解决:把默认拦截器手动使用一次

5配置拦截器,对action里面所有的方法都进行拦截
(1)在action里面有login的登录的方法,这个方法不需要拦截,如果这个方法都拦截,问题是,永远登录不进去了
(2)解决:让login方法不进行拦截
- 直接通过配置方式让action里面某些方法不进行拦截

6 如果登录状态,直接到功能页面,如果不是登录显示登陆页面
登录之后出现小问题:

(1)设置打开位置,在form标签里面

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值