struts自定义拦截器
第01步:配置web.xml,启动struts框架
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
第02步:编写action类
package com.self.action;
public class InterceptorAction {
private String message ;
public String login(){
this.message="登录成功!";
return "success";
}
public String addUser(){
this.message="添加用户";
return "success";
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
第03步:编写拦截器类
package com.self.action; import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;//导入包,包里含有Interceptor类 /**第01 步:编写拦截器**/
public class Permission implements Interceptor { public void destroy() { } public void init() { } public String intercept(ActionInvocation invocation) throws Exception {//属于aop编程的环绕通知
String reString="";
//01:判断用户是否登录,登录就有权限执行action方法,没登陆就没权限
Object user=ActionContext.getContext().getSession().get("user");
if(user!=null){
reString=invocation.invoke();//执行被拦截的action方法,被拦截的action方法有返回的字符串,原样返回
ActionContext.getContext().put("message", "允许执行该操作");
System.out.println("执行拦截的方法,并返回字符串:"+reString);
//return invocation.invoke();可以直接这样
}else{
reString="error";
ActionContext.getContext().put("message", "不允许执行该操作!");
System.out.println("执行拦截的方法,并返回字符串:"+reString);
}
return reString;//原样返回action方法的字符串
} }
第04步:配置struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>
<constant name="struts.action.extension" value="do,action"/>
<!-- 第02步:配置action -->
<package name="intercept" namespace="/" extends="struts-default"> <!-- 第03步:注册拦截器 -->
<interceptors>
<!-- 03.1:自定义拦截器配置 -->
<interceptor name="permission" class="com.self.action.Permission" />
<!-- 03.2 :定义拦截器栈,不定义会失去很多struts自身的拦截器-->
<interceptor-stack name="permissionStackss">
<!-- 03.4:引入系统拦截器栈:defaultStack,系统拦截器栈放前面,先执行 -->
<interceptor-ref name="defaultStack"/>
<!-- 03.5:引入自定义拦截器 -->
<interceptor-ref name="permission" />
</interceptor-stack>
</interceptors> <global-results>
<result name="error">
/error.jsp
</result>
</global-results>
<action name="list_*" class="com.self.action.InterceptorAction" method="{1}">
<!-- 03.6:自定义拦截器先执行:"permissionStackss"还是"permissionStacks" -->
<interceptor-ref name="permissionStackss" />
<result name="success">
/show.jsp
</result>
</action>
</package>
</struts>
第05步:编写界面,session有数据,拦截器通过
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
</head>
<body>
<%
request.getSession().setAttribute("user", "zouli");
%>
<center>
<a href="list_login.action">用户登录action!</a><BR>
<a href="list_addUser.action">添加用户action!</a><BR>
</center>
<br>
</body>
</html>
第06步:编写界面,session没数据,拦截器不通过
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
</head>
<body>
<%
request.getSession().removeAttribute("user");
%>
<center>
没有用户<BR>
<a href="list_login.action">用户登录action!</a><BR>
<a href="list_addUser.action">添加用户action!</a><BR>
</center>
<br>
</body>
</html>
第07步:编写通过显示界面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
</head>
<body>
<center>
登录信息:message:${message }
</center>
<br>
</body>
</html>
第08步:编写不通过错误界面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
</head>
<body>
<center>
拦截信息:message:${message }<BR>
拦截器不能使用错误!
</center>
</body>
</html>
注意:需要导入包

struts自定义拦截器的更多相关文章
- Struts自定义拦截器&拦截器工作原理
0.拦截器的调用原理: 拦截器是一个继承了序列化接口的普通接口.其工作原理是讲需要被拦截的对象作为参数传到intercept()方法内,在方法内部对此对象进行处理之后再执行原方法.intercept( ...
- JavaWeb -- Struts 自定义拦截器, 登录权限拦截
1. 自定义拦截器, 登录权限拦截 login.jsp 登录JSP <%@ page language="java" contentType="text/html; ...
- struts自定义拦截器实现
示例 添加新功能:只有是登录的状态访问hello_loginSuccess才会显示登录成功. index.jsp登录成功页面 test.jsp登录页面 一.修改原代码实现 1.登录后将登录信息添加到S ...
- struts自定义拦截器配置
配置自己的拦截器可以先参照下系统的拦截器是怎么配置的,首先打开struts-default.xml搜索下interceptor:系统里的拦截器有很多,拦截器都是放在堆栈里的,系统引用的是默认堆栈, & ...
- [原创]java WEB学习笔记74:Struts2 学习之路--自定义拦截器,struts内建的拦截器
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Struts学习之自定义拦截器
* 所有的拦截器都需要实现Interceptor接口或者继承Interceptor接口的扩展实现类 * 要重写init().intercept().destroy()方法 * in ...
- 2018.12.17 struts.xml 配置自定义拦截器配置
自定义拦截器有三个步骤哦 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PU ...
- java:struts框架3(自定义拦截器,token令牌,文件上传和下载(单/多))
1.自定义拦截器: struts.xml: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ...
- Struts的拦截器
Struts的拦截器 1.什么是拦截器 Struts的拦截器和Servlet过滤器类似,在执行Action的execute方法之前,Struts会首先执行Struts.xml中引用的拦截器,在执行完所 ...
随机推荐
- 在使用EFCodeFirst中出现类型“System.Data.Objects.ObjectContext”在未被引用的程序集中定义的解决方案
我安装了EF4.1版本,并在一个项目中映射一个数据库并生成了EF的MODEL实体层 测试:在Default.aspx页面上加了个GridView控件,后台进行绑定 using System; usin ...
- su terminal get around---docker root
su : must be run from a terminal After some googling, I found the solution from Tero's glob. If you ...
- 【C】用我所学去讲C语言指针
很多人不敢讲C的指针,有些人讲不清,有些人怕讲错.初生牛犊不怕虎,就让我讲讲. 下面开始. 一.指针的定义 指针是内存单元的编号.内存单元是以字节为单位的.所以指针就是字节的编号. 比如我们的个人电 ...
- 关于Bitcode的探索
Bitcode概述 Bitcode is an intermediate representation of a compiled program. Apps you upload t ...
- HAL层Camera模块Dump图片--工作积累
Camera的raw data一般都是YUV420的格式,数据的特点是: YUV 4:2:0采样,每四个Y共用一组UV分量 YUV420格式: 先Y,后V,中间是U.其中的Y是w * h,U和V是w/ ...
- transform: translateY(-50%) 实现元素垂直居中效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- .SQL Server中 image类型数据的比较
原文:.SQL Server中 image类型数据的比较 在SQL Server中如果你对text.ntext或者image数据类型的数据进行比较.将会提示:不能比较或排序 text.ntext 和 ...
- 视频播放器开发中遇到的一些小问题MPMoviePlayerController
1 开发环境是 xcode6 ipad3真机 ios8.1.1越狱 需要添加以下代码 ,否则真机测试没有外音,只有耳机 NSError *setCategoryError = nil; ...
- [LeetCode]题解(python):050-Pow(x, n)
题目来源 https://leetcode.com/problems/powx-n/ Implement pow(x, n). 题意分析 Input: x,n Output:pow(x,n) Cond ...
- Definition Questions
What is the relationship and differences between processes and threads? A process usually represent ...