第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:自定义拦截器先执行 --> <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>
注意:需要导入包
时间: 2024-10-20 04:20:20