架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 7663|回复: 5

[JavaEE] struts2自定义拦截器

[复制链接]
发表于 2014-12-28 21:32:45 | 显示全部楼层 |阅读模式
1.先写一个action

package action;public class InterceptorAction {                public String fun1() {                return "ok";        }}


2.配置action

        <package name="base" extends="struts-default">                <global-results>                <result name="message">/index.jsp</result>                <result name="message">/WEB-INF/page/message.jsp</result>        </global-results>        </package>        <package name="delversi" namespace="/test" extends="base">                <interceptors>                <interceptor name="permission" class="interceptor.InterceptorDemo"></interceptor>                <interceptor-stack name="permissionStack">                        <interceptor-ref name="defaultStack"></interceptor-ref>                        <interceptor-ref name="permission"></interceptor-ref>                </interceptor-stack>        </interceptors>        <action name="interceptor_*" class="action.InterceptorAction" method="{1}">                <interceptor-ref name="permission"></interceptor-ref>                <result name="ok">/ddd.jsp</result>        </action>
定义拦截器栈,是为了能使用系统自带的拦截器

3.写一个权限拦截器

package interceptor;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.Interceptor;public class InterceptorDemo implements Interceptor {        public void destroy() {                // TODO Auto-generated method stub                        }        public void init() {                // TODO Auto-generated method stub                        }        public String intercept(ActionInvocation invocation) throws Exception {                System.out.println("进入拦截器");                if (ActionContext.getContext().getSession().get("user") !=null ) {                        return invocation.invoke();                }else {                        ActionContext.getContext().put("message", "无权限");                        return "message";                }                        }}登录了,返回ok,未登录返回message

4.写一个jsp,向session中注入user

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><% request.getSession().setAttribute("user","111");%>



转载请注明出处http://blog.csdn.net/z1104222568/article/details/42218125

点评

以去除图片  发表于 2014-12-29 10:16




上一篇:印尼官员:失联亚航航班并未发出求救信号
下一篇:广西河池 吸粪车爆炸,国民素质提高了
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2014-12-28 21:39:26 | 显示全部楼层
图片是错误的,加载不出来
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
 楼主| 发表于 2014-12-29 00:25:29 | 显示全部楼层
admin 发表于 2014-12-28 21:39
图片是错误的,加载不出来

从我的博客复制过来的
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
 楼主| 发表于 2014-12-29 00:25:30 | 显示全部楼层
admin 发表于 2014-12-28 21:39
图片是错误的,加载不出来

从我的博客复制过来的
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
 楼主| 发表于 2014-12-29 00:25:32 | 显示全部楼层
admin 发表于 2014-12-28 21:39
图片是错误的,加载不出来

从我的博客复制过来的
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|手机版|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2024-4-29 16:49

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表