java struts2拦截器_JAVAEE——struts2_04:自定义拦截器、struts2标签、登陆功能和校验登陆拦截器的实现...

一、自定义拦截器

1.架构

884c581e4d3965a3b96b111256dec271.png

2.拦截器创建

//拦截器:第一种创建方式//拦截器生命周期:随项目的启动而创建,随项目关闭而销毁

public class MyInterceptor implements Interceptor{}

//创建方式2: 继承AbstractInterceptor -> struts2的体贴//帮我们空实现了init 和 destory方法. 我们如果不需要实现这两个方法,就可以只实现intercept方法

public class MyInterceptor2 extends AbstractInterceptor{}

//创建方式3:继承MethodFilterInterceptor 方法过滤拦截器//功能: 定制拦截器拦截的方法.//定制哪些方法需要拦截.//定制哪些方法不需要拦截

public class MyInterceptor3 extends MethodFilterInterceptor{}

3.拦截器api

//放行

String result = invocation.invoke();

//前处理

System.out.println("MyInterceptor3 的前处理!");//放行

String result =invocation.invoke();//后处理

System.out.println("MyInterceptor3 的后处理!");

//不放行,直接跳转到一个结果页面//不执行后续的拦截器以及Action,直接交给Result处理结果.进行页面跳转

return "success";

4.拦截器配置

add,delete

/index.jsp

/login.jsp

二、struts2标签

1.标签体系

47d1487283e03b21b7567e8fa3066c31.png

08a649eb74fc780d2bdd804dce33c057.png

2.struts2标签结构

86fe2a7c16b85cae08351d0b5b1fe24a.png

3.控制标签

准备Action然后再到jsp练习struts2标签

public class Demo2Action extendsActionSupport {public String execute() throwsException {

List list = new ArrayList<>();

list.add("tom");

list.add("jerry");

list.add("jack");

list.add("rose");

list.add("hqy");

ActionContext.getContext().put("list", list);returnSUCCESS;

}

}

开始练习控制标签:



|


list长度为4!

list长度为3!

list不3不4!

4.数据标签


5.表单标签

6.非表单标签

在action中添加错误信息

this.addActionError("我是错误信息 哈哈哈");

取出错误信息

三、练习:登陆功能

57d4abd1756ecb05ab95834d079a18b5.png

核心代码:

Action代码:

public class UserAction extends ActionSupport implements ModelDriven{private User user = newUser();private UserService us = newUserServiceImpl();public String login() throwsException {//1 调用Service 执行登陆操作

User u =us.login(user);//2 将返回的User对象放入session域作为登陆标识

ActionContext.getContext().getSession().put("user", u);//3 重定向到项目的首页

return "toHome";

}

@OverridepublicUser getModel() {returnuser;

}

}

Service层代码:

public class UserServiceImpl implementsUserService {private UserDao ud = newUserDaoImpl();

@OverridepublicUser login(User user) {//打开事务

HibernateUtils.getCurrentSession().beginTransaction();//1.调用Dao根据登陆名称查询User对象

User existU =ud .getByUserCode(user.getUser_code());//提交事务

HibernateUtils.getCurrentSession().getTransaction().commit();if(existU==null){//获得不到=>抛出异常提示用户名不存在

throw new RuntimeException("用户名不存在!");

}//2 比对密码是否一致

if(!existU.getUser_password().equals(user.getUser_password())){//不一致=>抛出异常提示密码错误

throw new RuntimeException("密码错误!");

}//3 将数据库查询的User返回

returnexistU;

}

}

Dao层代码:

public class UserDaoImpl implementsUserDao {

@OverridepublicUser getByUserCode(String user_code) {//HQL查询//1.获得Session

Session session =HibernateUtils.getCurrentSession();//2 书写HQL

String hql = "from User where user_code = ? ";//3 创建查询对象

Query query =session.createQuery(hql);//4 设置参数

query.setParameter(0, user_code);//5 执行查询

User u =(User) query.uniqueResult();returnu;

}

}

四、练习:校验登陆拦截器

核心代码:

拦截器代码:

public class LoginInterceptor extendsMethodFilterInterceptor {//指定不拦截登陆方法. 其他方法都拦截

@Overrideprotected String doIntercept(ActionInvocation invocation) throwsException {//1.获得session

Map session =ActionContext.getContext().getSession();//2.获得登陆标识

Object object = session.get("user");//3.判断登陆标识是否存在

if(object == null){//不存在=>没登录=>重定向到登录页面

return "toLogin";

}else{//存在=>已经登陆=>放行

returninvocation.invoke();

}

}

}

struts.xml配置文件代码:

/p>

"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

"http://struts.apache.org/dtds/struts-2.3.dtd">

login

/login.jsp

/jsp/customer/list.jsp

CustomerAction_list

/

/index.htm

/login.jsp

补充知识:检查当前页面的父页面是否是自己,不是的话进行跳转,解决页面嵌套问题。

//就让框架页面跳转到登陆页面

window.parent.location.href = "${pageContext.request.contextPath}/login.jsp";

}

};

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值