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

一、自定义拦截器

1.架构

6f9274a81a47cb8cd4eddcdac9feb922.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.标签体系

d02dbf29093beaca00e59898e45540d7.png

1b32f51d6f0156ada8d6ee83bb45e67d.png

2.struts2标签结构

4eda995968ccc7f6acf6020f653a6ed1.png

3.控制标签

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

public class Demo2Action extends ActionSupport {public String execute() throws Exception {

List list = new ArrayList<>();

list.add("tom");

list.add("jerry");

list.add("jack");

list.add("rose");

list.add("hqy");

ActionContext.getContext().put("list", list);return SUCCESS;

}

}

开始练习控制标签:



|
list长度为4!list长度为3!list不3不4!

4.数据标签


5.表单标签

6.非表单标签

在action中添加错误信息

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

取出错误信息

三、练习:登陆功能

9f624047f0ce5471b467b9519171d803.png

核心代码:

Action代码:

public class UserAction extends ActionSupport implements ModelDriven {private User user = new User();private UserService us = new UserServiceImpl(); public String login() throws Exception {//1 调用Service 执行登陆操作User u = us.login(user);//2 将返回的User对象放入session域作为登陆标识ActionContext.getContext().getSession().put("user", u);//3 重定向到项目的首页return "toHome";

}

@Overridepublic User getModel() {return user;

}

}

Service层代码:

public class UserServiceImpl implements UserService {private UserDao ud = new UserDaoImpl();

@Overridepublic User 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返回return existU;

}

}

Dao层代码:

public class UserDaoImpl implements UserDao {

@Overridepublic User getByUserCode(String user_code) {//HQL查询//1.获得SessionSession session = HibernateUtils.getCurrentSession();//2 书写HQLString hql = "from User where user_code = ? ";//3 创建查询对象Query query = session.createQuery(hql);//4 设置参数query.setParameter(0, user_code);//5 执行查询User u = (User) query.uniqueResult();return u;

}

}

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

核心代码:

struts.xml配置文件代码:

<?xml version="1.0" encoding="UTF-8"?>/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.jspCustomerAction_list / /index.htm/login.jsp

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

}

};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值