Home> Java> javaTutorial> body text

JAVAEE - Implementation of custom interceptors, struts2 tags, login functions and verification login interceptors

巴扎黑
Release: 2017-06-26 11:46:23
Original
2069 people have browsed it

1. Custom interceptor

1. Architecture

2. Interceptor creation

//拦截器:第一种创建方式//拦截器生命周期:随项目的启动而创建,随项目关闭而销毁public class MyInterceptor implements Interceptor{}
Copy after login

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

//创建方式3:继承MethodFilterInterceptor 方法过滤拦截器//功能: 定制拦截器拦截的方法.// 定制哪些方法需要拦截.// 定制哪些方法不需要拦截public class MyInterceptor3 extends MethodFilterInterceptor{}
Copy after login

3. Interceptor api

//放行String result = invocation.invoke();
Copy after login

//前处理System.out.println("MyInterceptor3 的前处理!");//放行String result = invocation.invoke();//后处理System.out.println("MyInterceptor3 的后处理!");
Copy after login

//不放行,直接跳转到一个结果页面//不执行后续的拦截器以及Action,直接交给Result处理结果.进行页面跳转return "success";
Copy after login
4. Interceptor configuration

  add,delete /index.jsp
Copy after login

/login.jsp
Copy after login

2. struts2 tags

1. Tag system

## 2.struts2 tag structure

3. Control tags

Prepare Action and then go to jsp to practice struts2 tags

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; } }
Copy after login

Start practicing control tags:

<%@ taglib prefix="s" uri="/struts-tags" %>



|
list长度为4!list长度为3!list不3不4!
Copy after login

4. Data label


Copy after login

5. Form label

Copy after login

6. Non-form tags

Add error message to action
this.addActionError("我是错误信息 哈哈哈");
Copy after login

Remove error message

Copy after login

3. Exercise: Login function

## Core code:

Action code:

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; } }
Copy after login

Service layer code:

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; } }
Copy after login

Dao layer code:

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; } }
Copy after login

IV. Exercise: Verify login interception Device

Core code:

Struts.xml configuration file code:

login/login.jsp /jsp/customer/list.jsp CustomerAction_list / /index.htm/login.jsp
Copy after login

Supplementary knowledge: Check whether the parent page of the current page is itself, not If so, jump to solve the page nesting problem.

Copy after login

The above is the detailed content of JAVAEE - Implementation of custom interceptors, struts2 tags, login functions and verification login interceptors. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!