Struts2学习笔记——自定义拦截器技术

来源:互联网 发布:js身份证算法验证 编辑:程序博客网 时间:2024/06/05 06:41

以一个故事来分析自定义拦截器技术(权限管理):


登陆页面(寻找辟邪剑谱)------>主页面(进入林家老宅【所有用户都可以进入】)------>成功页面(辟邪剑谱秘诀)【只有岳不群才能看见此页面,其他自动调整到寻找辟邪剑谱页】)。


登陆页面(寻找辟邪剑谱):

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@page import="com.opensymphony.xwork2.util.ValueStack"%><%@ taglib uri="/struts-tags" prefix="s" %><%String path = request.getContextPath();String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="<%=basePath%>"><title>寻找辟邪剑谱</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><form method="post" action="login.action" name="loginForm"><p> username:<input type="text" name="username"></p><p> password:<input type="password" name="password"><s:fielderror fieldName="password"></s:fielderror></p><p> <input type="submit" value="login" name="loginBtn"><br></p></form></body></html>

主页面(进入林家老宅)

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>林家老宅</title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>     <h1><a href="toContent.action">辟邪剑谱</a></h1><br>  </body></html>

成功页面(辟邪剑谱秘诀)

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>林家老宅</title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>     <h1><a href="toContent.action">辟邪剑谱</a></h1><br>  </body></html>


登陆页面对应的Action:

import java.util.Map;import org.apache.struts2.interceptor.RequestAware;import org.apache.struts2.interceptor.SessionAware;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupportimplements SessionAware,RequestAware{private String username;private String password;private Map session;private Map request;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}//public String tianxia(){if(this.username.equals("yuebuqun")&& this.password.equals("junzijian")){session.put("role", "vip");}else{session.put("role", "normal");}return this.SUCCESS;}@Overridepublic void validate() {// TODO Auto-generated method stubif(this.username == null || this.username.equals("")){this.addFieldError("username", "用户名不能为空!");}if(this.password == null || this.password.equals("")){this.addFieldError("password", "密码不能为空!");}}public void validateWorld(){System.out.println("World validate");}public void setSession(Map<String, Object> arg0) {// TODO Auto-generated method stubthis.session = arg0;}public void setRequest(Map<String, Object> arg0) {// TODO Auto-generated method stubthis.request = arg0;}}

自定义的拦截器:


import java.util.Map;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;public class RoleInterceptor extends AbstractInterceptor{@Overridepublic String intercept(ActionInvocation invocation) throws Exception {// TODO Auto-generated method stub//1、先得到session当中的roleMap session = invocation.getInvocationContext().getSession();//2、判断是否具有VIPString role = (String)session.get("role");if(role.equals("vip")){return invocation.invoke();}return "login";}}


struts.xml配置;


<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "struts-2.3.dtd" ><struts><package name="interceptorStruts2" namespace="/"extends="struts-default"><interceptors><interceptor name="roleInter" class="com.lovo.struts2.interceptor.RoleInterceptor"></interceptor><interceptor-stack name="role-stack"><interceptor-ref name="defaultStack"></interceptor-ref><interceptor-ref name="roleInter"></interceptor-ref></interceptor-stack></interceptors><action name="login" method="tianxia"class="com.lovo.struts2.action.LoginAction"><result>main.jsp</result><result name="login">login.jsp</result><result name="input">login.jsp</result></action><action name="toContent"class="com.opensymphony.xwork2.ActionSupport"> <interceptor-ref name="role-stack"></interceptor-ref><result>content.jsp</result><result name="login">login.jsp</result></action></package></struts>

当然还有web.xml的配置:

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  <display-name></display-name>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>    <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></web-app>


效果:

当输入yuebuqun  junzijian时:


然后进入林家老宅:







然后看见剑谱口诀:





原创粉丝点击