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>