Struts2学习笔记(四)之——struts.xml配置文拦截器

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>
	<package name="manage" namespace="/" extends="struts-default">
		
		<!-- 拦截器 -->
		<interceptors>
			<interceptor name="myInteceptor" class="com.java1234.inteceptor.MyInteceptor"></interceptor>
			<interceptor name="loginInteceptor" class="com.java1234.inteceptor.LoginInteceptor"></interceptor>
			<!-- 定义拦截器栈 -->
			<interceptor-stack name="myStack">
				<interceptor-ref name="loginInteceptor"></interceptor-ref>
				<interceptor-ref name="defaultStack"></interceptor-ref><!-- struts默认的拦截器 -->
			</interceptor-stack>
		</interceptors>
		<!-- 此包下的所有Action默认使用拦截器栈 -->
		<default-interceptor-ref name="myStack"></default-interceptor-ref>
		<!-- 全局配置错误页面 -->
		<global-results>
			<result name="error">error.jsp</result>
		</global-results>
		
		
		<action name="hello" class="com.java1234.action.HelloWorldAction">
			<result name="success" >success.jsp</result>
			<interceptor-ref name="myInteceptor"></interceptor-ref>
			<interceptor-ref name="defaultStack"></interceptor-ref>
		</action>
		<action name="user" class="com.java1234.action.UserAction">
			<result name="success" >success.jsp</result>
			<!-- 此处一定要加上系统默认的拦截器,否则会默认使用拦截器栈myStack用户永远登不进了 -->
			<interceptor-ref name="defaultStack"></interceptor-ref>
		</action>
		<action name="girl" class="com.java1234.action.GirlAction">
			<result name="success" >success.jsp</result>
			<!-- <interceptor-ref name="loginInteceptor"></interceptor-ref>
			<interceptor-ref name="defaultStack"></interceptor-ref> -->
		</action>
	</package>
</struts>

 

拦截器类LoginInteceptor

package com.java1234.inteceptor;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

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

public class LoginInteceptor implements Interceptor{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Override
	public void destroy() {
		
		System.out.println("MyInteceptor销毁");
	}
	@Override
	public void init() {
		
		System.out.println("Inteceptor初始化");
	}
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		System.out.println("在Action执行之前");
		
		ActionContext actionContext = invocation.getInvocationContext();
		Map<String, Object> session = actionContext.getSession();
		Object currentUser = session.get("currentUser");
		String result = null;
		if(currentUser!=null) {
			result = invocation.invoke();
		} else {
			HttpServletRequest request = (HttpServletRequest)invocation.getInvocationContext().get(ServletActionContext.HTTP_REQUEST);
			request.setAttribute("error", "请先登录");
			result = "error";
			
		}
		System.out.println("result:"+result);
		System.out.println("在Action执行之后");
		return result;
	}

}

 

 

GirlAction

package com.java1234.action;

import com.opensymphony.xwork2.ActionSupport;

public class GirlAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public String execute() throws Exception {
		System.out.println("看美女");
		return SUCCESS;
	}
}

UserAction

package com.java1234.action;

import java.util.Map;

import com.java1234.model.User;
import com.java1234.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 2L;

	private UserService userService = new UserService();
	private User user;
	private String error;
	
	public String getError() {
		return error;
	}
	public void setError(String error) {
		this.error = error;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	@Override
	public String execute() throws Exception {
		boolean login2 = userService.login(user);
		if(login2) {
			ActionContext actionContext = ActionContext.getContext();
			Map<String, Object> session = actionContext.getSession();
			session.put("currentUser", user);
			return SUCCESS;
		}else {
			this.error = "用户名或密码错误";
			return ERROR;
		}
	}
}

login.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>
<form action="user" method="post">
	用户名:<input type="text" name="user.userName" />
	密码:<input type="text" name="user.password" />
	<input type="submit" value="登录">
</form>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值