简单例子分析struts的自定义拦截器
博客专区 > 丶KKdo 的博客 > 博客详情
简单例子分析struts的自定义拦截器
丶KKdo 发表于2年前
简单例子分析struts的自定义拦截器
  • 发表于 2年前
  • 阅读 2
  • 收藏 0
  • 点赞 0
  • 评论 0

【腾讯云】买域名送云解析+SSL证书+建站!>>>   

首先,创建web项目,配置好web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>struts-auth</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>

然后,在webRoot目录下编写一个简单的前台页面:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<h2>用户登录</h2>
<form action="admin.html" method="post">
用户名:
<input type="text" name="username">${loginError}
<input type="submit" value="登录">
</form>
</body>
</html>

在WEB-INF下新建webPage目录,建立一个简单的admin.jsp页面

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
后台管理页面!
</body>
</html>

建立Action类:

package com.auth.action;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public class AdminAction extends ActionSupport implements SessionAware {
	private String username;
	private Map<String, Object> session;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	public String login() {
		if(username.equals("admin")) {
			session.put("username", username);
			return SUCCESS;
		}else {
			session.put("loginError", "用户名错误!");
			return ERROR;
		}
	}
}

建立Inteceptor类:

package com.auth.interceptor;

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

public class UserInterceptor extends AbstractInterceptor {

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		String name = (String) ActionContext.getContext().
				getSession().get("username");
		if(name != null) {
			String result = invocation.invoke();
			return result;
		}
		return "error";
	}

}

配置struts.xml文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
	"http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>

    <constant name="struts.devMode" value="true" />
    <!-- action的后缀名为html -->
    <constant name="struts.action.extension" value="html"></constant>

	<package name="auth" extends="struts-default" namespace="/">
		<interceptors>
			<interceptor name="auth" class="com.auth.interceptor.UserInterceptor"></interceptor>
			<interceptor-stack name="myStack">
				<interceptor-ref name="auth"></interceptor-ref>
				<interceptor-ref name="defaultStack"></interceptor-ref>
			</interceptor-stack>
		</interceptors>
		
		<action name="admin" class="com.auth.action.AdminAction" method="login">
			<result>/WEB-INF/webPage/admin.jsp</result>
			<result name="error">/login.jsp</result>
		</action>
		
		<action name="auth">
			<result>/WEB-INF/webPage/admin.jsp</result>
			<interceptor-ref name="myStack"></interceptor-ref>
			<result name="error">/login.jsp</result>
		</action>
	</package>
</struts>

然后,直接访问auth.html会跳转到登录页面,然后输入admin点击登录,正确跳转到后台页面,接着由于username已经存放在session中,所以再次访问auth.html能够正常跳转到admin.html的后台页面.

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 1
博文 22
码字总数 13683
×
丶KKdo
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: