struts2拦截器

关于struts 全局拦截器的配置

 

1.首先写好action 用户从页面获取相应的数据

 

UserAction代码

package com.yihaodian.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	private String userName;
	private String password;

	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 execute(){
		return SUCCESS;
	}
	
	public String listAllUser(){
//将页面上的数据存放到session中
		ActionContext.getContext().getSession().put("userName", userName);
		return SUCCESS;
	}
}

 

2.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.devMode" value="true"></constant>
	<package name="struts" extends="struts-default" namespace="/">

		<interceptors>
			<!-- 定义权限控制的拦截器  -->
			<interceptor name="authrity"
				class="com.yihaodian.authorzation.AuthorizationInterceptor">
			</interceptor>
			<!-- 定义一个包含权限控制的拦截器栈 -->
			<interceptor-stack name="mydefault">
				<interceptor-ref name="defaultStack"></interceptor-ref>
				<interceptor-ref name="authrity"></interceptor-ref>
			</interceptor-stack>
		</interceptors>

		<!-- 定义默认的拦截器  则对该包下所有的action都有拦截作用-->
		<default-action-ref name="mydefault"></default-action-ref>

		<!-- 定义全局处理结果 -->
		<global-results>
			<!-- 逻辑名为login的结果,映射到/login.jsp页面 -->
			<result name="login">/login.jsp</result>
		</global-results>

       <action name="listall" class="com.yihaodian.action.UserAction" method="listAllUser">
          <result name="success">/listall.jsp</result>
       </action>

	</package>
	
	<!-- 重新定义的package中的action 对上一个包中的默认拦截器不起作用 -->
	<package name="font" extends="struts-default">
	   <action name="user0pt" class="com.yihaodian.action.UserAction">
	      <result name="success">/success.jsp</result>
	   </action>
	</package>
</struts>

 

3.拦截器实现类

 

package com.yihaodian.authorzation;

import java.util.Map;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class AuthorizationInterceptor extends AbstractInterceptor {

	private static final long serialVersionUID = 1L;

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		Map session = invocation.getInvocationContext().getSession();
		String userName = (String)session.get("userName");
		if(userName!=null&& userName.equals("test")){
			return invocation.invoke();
		}else{
			return Action.LOGIN;
		}
	}

}

 4.jsp页面

<%@ 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">

	</head>

	<body>
		<form action="listall">
			<table align="center">
               <tr>
                  <td>用户名</td><td><input type="text" name="userName"/></td>
               </tr>
               <tr>
                  <td>密码</td><td><input type="password" name="password"/></td>
               </tr>
               <tr>
                  <td colspan="2" align="center"><input type="submit" value="登陆"/><input type="reset" value="重置"/></td>
               </tr>
			</table>
		</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>SUCCESS</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">

	</head>

	<body>
		SUCCESS
	</body>
</html>
 

你可能感兴趣的:(STRUTS2 拦截器,全局拦截)