图示:

Struts2 配置登陆拦截器_java

 

User 实体类

package entity;

public class User {
	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 say(){
		return "这是一个方法";
	}
}

 

MyTimerAction Action类

package action;

import java.util.Map;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

import entity.User;

public class MyTimerAction implements Action {
	private User user;
	private String message;
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public String execute() throws Exception {
		//判断账号密码是否正确
		if("admin".equals(user.getUsername())&&"admin".equals(user.getPassword())){
			//session
			Map<String, Object> session = ActionContext.getContext().getSession();
			session.put("USER", user);
			//返回成功
			return SUCCESS;
		}else{
			this.message = "用户名或者密码错误";
			//返回重新输入
			return INPUT;
		}
	}
}

 

MyTimerInterceptor 拦截器类

package interceptor;

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

import entity.User;

public class MyTimerInterceptor extends AbstractInterceptor {

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		//1、执行 Action 之前的工作:获取开始执行时间
		long startTime = System.currentTimeMillis();
		System.out.println("执行 Action 之前的工作,开始时间"+startTime);
		String result =null;
		User user=(User) invocation.getInvocationContext().getSession().get("USER");
//		if(null!=user){
//			return "input"; //问题来了,如果登陆成功后,如果刷新一次之后就跳到登陆页面,再次登陆就会永远是登陆页面
//		}
		
//		if(null==user){
//			return "input";//由于执行拦截器,所以无论如何都无法跳转首页,一直都是登陆页面。因为user一直为空
//		}
		
//		if(user == null){
//			return Action.INPUT;
//		}
		/*
		 * 想问一下:
		 * 如果user为空就放行
		 * 如果user为不空就判断它应该跳转到哪个页面,不知道这样做是否合理
		 * */
		if(null!=user){
			if("admin".equals(user.getUsername())&&"admin".equals(user.getPassword())){
				return "success";
			}else{
				return "input";
			}
		}else{
			//2、执行后续拦截器或 Action
			result = invocation.invoke();
			//3、执行 Action 之后的工作:计算并输出执行时间
			long endTime = System.currentTimeMillis();
			long execTime = endTime - startTime;
			System.out.println("执行 Action 后的,结束时间"+endTime);
			System.out.println("总共用时"+execTime);
			//返回结果字符串
			return result;
		}
	}
	/*
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		//1、执行 Action 之前的工作:获取开始执行时间
		long startTime = System.currentTimeMillis();
		System.out.println("执行 Action 之前的工作,开始时间"+startTime);
		//2、执行后续拦截器或 Action
		String result = invocation.invoke();
		//3、执行 Action 之后的工作:计算并输出执行时间
		long endTime = System.currentTimeMillis();
		long execTime = endTime - startTime;
		System.out.println("执行 Action 后的,结束时间"+endTime);
		System.out.println("总共用时"+execTime);
		//返回结果字符串
		return result;
	}
	 */
}

 

配置 struts.xml

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

<struts>
	<package name="default" namespace="/" extends="struts-default">
		<!-- 所有拦截器都放这里 -->
		<interceptors>
			<!-- 定义拦截器 -->
			<interceptor name="myTimer" class="interceptor.MyTimerInterceptor"></interceptor>
		</interceptors>
		<!-- 定义全局的返回 -->
		<global-results>
			<result name="input">/login.jsp</result>
		</global-results>
		<action name="login" class="action.MyTimerAction">
			<!-- 为 Action 指定拦截器引用 -->
			<interceptor-ref name="myTimer"></interceptor-ref>
			<interceptor-ref name="defaultStack"></interceptor-ref>
			<result name="success">/index.jsp</result>
		</action>
	</package>
</struts>

 

login.jsp 页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%
	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>
</head>

<body>
	<s:form action="login">
		<s:textfield label="用户名" name="user.username"></s:textfield>
		<s:password label="密码" name="user.password"></s:password>
		<s:submit value="提交"></s:submit>
	</s:form>
	<h4>
		<s:property value="message" />
	</h4>
</body>
</html>

 

index.jsp 页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%
	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>
</head>

<body>
	<h4>登陆后才能访问</h4>
	<h4>${sessionScope.USER.username },欢迎光临本网站</h4>
	<h4><s:property value="#session.USER.username" />,欢迎光临本网站</h4>
</body>
</html>

 

效果图:

Struts2 配置登陆拦截器_struts_02


 

Struts2 配置登陆拦截器_System_03

Struts2 配置登陆拦截器_User_04