【Struts2】Struts2中拦截器

【拦截器】

拦截器是Struts2中的核心。其实就是一个类对Action的访问进行拦截。


【拦截器与过滤器的区别】

过滤器过滤的是从客户端向服务器发送的请求。过滤器是可以过滤到Html,JSP…

拦截器拦截的是从客户端向Action发送的请求。拦截器只能拦截Action,拦截器更细粒度的实施拦截。拦截到Action中具体的方法。


【Struts2的执行流程】

请求访问服务器Action,请求会首先到达核心过滤器,过滤器调用Dispatcher中的serviceAction的方法,在这个方法内部,Struts2框架创建一个Action的代理对象,执行代理对象中的execute方法,在execute方法内部调用ActionInvocation中的invoke方法。在invoke方法内部,递归调用拦截器的拦截的方法,如果没有下一个拦截器,执行目标Action,根据Action返回Result进行页面跳转,由Response作出响应。


【拦截器使用】

jsp中引入action中的登录

<form id=form1 name=form1 action="/struts2_crm/user_login.action" method=post>


编写Action

public class UserAction extends ActionSupport implements ModelDriven<User>{
	// 模型驱动使用的对象
	private User user = new User();
	@Override
	public User getModel() {
		return user;
	}
	/**
	 * 用户登录的方法:login
	 */
	public String login(){
		// 调用业务层:
		UserService userService = new UserServiceImpl();
		User existUser = userService.login(user);
		// 判断进行页面跳转:
		if(existUser == null){
			// 登录失败:
			// 错误信息的提示:
			this.addActionError("登录失败:用户名或密码错误!");
			// 页面跳转回login页面:
			return LOGIN;
		}else{
			// 登录成功:
			// 将用户存入到Session
			ActionContext.getContext().getSession().put("existUser", existUser);
			// 页面跳转
			return SUCCESS;
		}
	}

}

编写Service

public class UserServiceImpl implements UserService {

	@Override
	public User login(User user) {
		UserDao userDao = new UserDaoImpl();
		return userDao.login(user);
	}

}

编写DAO

public class UserDaoImpl implements UserDao {

	@Override
	public User login(User user) {
		Session session = HibernateUtils.getCurrentSession();
		Transaction transaction = session.beginTransaction();
		
		Query query = session.createQuery("from User where user_code =? and user_password=?");
		query.setParameter(0, user.getUser_code());
		query.setParameter(1, user.getUser_password());
		User existUser = (User) query.uniqueResult();
		
		transaction.commit();
		return existUser;
	}
}

XML配置文件中配置Action

name="user_*" class="com.itheima.crm.web.action.UserAction" method="{1}">
			<result name="success" type="redirect">/index.jsp</result>
			<result name="login">/login.jsp</result>
		</action>

编写拦截器

/**
 * 登录的权限拦截器
 * @author zjl
 *
 */
public class PrivilegeInterceptor extends MethodFilterInterceptor{

	@Override
	protected String doIntercept(ActionInvocation invocation) throws Exception {
		// 从session中获取用户的信息
		User existUser = (User) ServletActionContext.getRequest().getSession().getAttribute("existUser");
		if(existUser!=null){
			return invocation.invoke();
		}else{
			// 存入错误信息
			ActionSupport actionSupport=(ActionSupport) invocation.getAction();
			actionSupport.addActionError("您还没有登录!您没有权限访问!");
			// 跳转到其他页面
			return actionSupport.LOGIN;
		}
	}

}



配置拦截器

<package name="crm" extends="struts-default" namespace="/">
	
		
		<!-- 第一种拦截器的配置:定义拦截器 -->
		<interceptors>
			<interceptor name="privilegeInterceptor" class="com.itheima.crm.web.interceptor.PrivilegeInterceptor"></interceptor>
		</interceptors>
		
		<global-results>
			<result name="login">/login.jsp</result>
		</global-results>
	
		<action name="customer_*" class="com.itheima.crm.web.action.CustomerAction" method="{1}">
			<result name="success">/jsp/customer/list.jsp</result>
			<result name="saveUI">/jsp/customer/add.jsp</result>
			<result name="saveSuccess" type="redirectAction">customer_findAll.action</result>
			
			<interceptor-ref name="privilegeInterceptor"></interceptor-ref>
			<interceptor-ref name="defaultStack"></interceptor-ref>
		</action>

		<action name="user_*" class="com.itheima.crm.web.action.UserAction" method="{1}">
			<result name="success" type="redirect">/index.jsp</result>
			
			<interceptor-ref name="privilegeInterceptor">
				<param name="excludeMethods">login</param>
			</interceptor-ref>
			<interceptor-ref name="defaultStack"></interceptor-ref>
		</action>
	</package>


【小结】

登录拦截器的使用就是在未登录状态下不能访问某些特定的网页,一访问就会弹出登录界面,登录成功之后才能够进行网站内部的操作。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值