myeclipse5.5 + tomcat5.5 + jdk1.5 需要的朋友可以直接下载看看 ^_^ ...
首先是 actions 包里的类 (2个 action ,一个拦截器)
1.CheckLoginInterceptor
/*************************************************
@Copyright (C), 2008, lzpeng
@File name: CheckLoginInterceptor.java
@Author: lzpeng
@CreateDate: 2008-6-17
@Description: CheckLoginInterceptor拦截器
@Extends: AbstractInterceptor
@Function List:
1. public String intercept()
*************************************************/
package cn.com.lzpeng.actions;
import java.util.Map;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class CheckLoginInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = 1L;
public static final String USER_SESSION_KEY="user";
@Override
public String intercept(ActionInvocation actionInvocation) throws Exception {
System.out.println("begin check login interceptor");
// 对LoginAction不做该项拦截
Object action = actionInvocation.getAction();
if (action instanceof LoginAction) {
System.out
.println("exit check login, because this is login action.");
return actionInvocation.invoke();
}
// 验证 session
Map session = actionInvocation.getInvocationContext().getSession();
String username = (String) session.get(USER_SESSION_KEY);
if (username != null) {
// 存在的情况下进行后续操作。
System.out.println(username+" already login!");
return actionInvocation.invoke();
} else {
// 否则终止后续操作,返回LOGIN
System.out.println("no login, forward login page!");
return Action.LOGIN;
}
}
}
2.LoginAction
/*************************************************
@Copyright (C), 2008, lzpeng
@File name: LoginAction.java
@Author: lzpeng
@CreateDate: 2008-6-17
@Description: LoginAction控制类
@Extends: ActionSupport
@Function List:
1. public UserBean getUserBean()
返回一个UserBean实体
2. public void setUserBean(UserBean userBean)
设置一个UserBean实体
3. public String execute()
执行Action的方法
4.public void setSession()
设置 session 值
*************************************************/
package cn.com.lzpeng.actions;
import com.opensymphony.xwork2.ActionSupport;
import cn.com.lzpeng.entity.UserBean;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
public class LoginAction extends ActionSupport implements SessionAware {
private static final long serialVersionUID = 1L;
private UserBean userBean;
private Map<String, String> session;
public UserBean getUserBean() {
return userBean;
}
public void setUserBean(UserBean userBean) {
this.userBean = userBean;
}
@Override
public String execute() {
if (userBean == null) {
return LOGIN;
}
String name = userBean.getName();
String pwd = userBean.getPassword();
session.put(CheckLoginInterceptor.USER_SESSION_KEY, name);
if (name.equals("java") && pwd.equals("struts")) {
return SUCCESS;
} else {
return ERROR;
}
}
public void setSession(Map session) {
this.session = session;
}
}
3.TestInterceptorAction
/*************************************************
@Copyright (C), 2008, lzpeng
@File name: LoginAction.java
@Author: lzpeng
@CreateDate: 2008-6-17
@Description: TestInterceptorAction控制类
@Extends: ActionSupport
@Function List:
1.public String execute()
执行 Action 的方法
*************************************************/
package cn.com.lzpeng.actions;
import com.opensymphony.xwork2.ActionSupport;
public class TestInterceptorAction extends ActionSupport {
private static final long serialVersionUID = 1L;
@Override
public String execute() {
return SUCCESS;
}
}
这个是实体类 UserBean.java
/*************************************************
@Copyright (C), 2008, lzpeng
@File name: LoginAction.java
@Author: lzpeng
@CreateDate: 2008-6-17
@Description: UserBean实体类
*************************************************/
package cn.com.lzpeng.entity;
public class UserBean {
private String name;
private String password;
public void setName(String name) {
this.name = name;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public String getPassword() {
return password;
}
}
struts.xml
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 加载默认的 struts2 配置文件 -->
<include file="struts-default.xml" />
<!-- 继承默认的 struts2 配置文件 -->
<package name="default" extends="struts-default">
<!-- 定义一个名为 checkLogin 的拦截器 -->
<interceptors>
<!-- 定义权限检查拦截器 -->
<interceptor name="checkLogin"
class="cn.com.lzpeng.actions.CheckLoginInterceptor" />
<!-- 定义一个包含权限检查的拦截器栈 -->
<interceptor-stack name="myDefaultStack">
<!-- 定义拦截器栈包含checkLogin拦截器 -->
<interceptor-ref name="checkLogin"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 设置全局 全局默认的拦截器栈-->
<default-interceptor-ref name="myDefaultStack"></default-interceptor-ref>
<!-- 定义全局Result -->
<global-results>
<!-- 当返回login视图名时,转入/login.jsp页面 -->
<result name="login">/login.jsp</result>
</global-results>
<!--
action 标签里 name属性代表我们要处理的.action的前面部分
action 标签里 class属性代表我们需要哪个类来处理
result 标签的 name属性代表action类的执行方法的返回值,
action类的默认执行方法是public String execute()
-->
<action name="login"
class="cn.com.lzpeng.actions.LoginAction">
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
<result name="login">login.jsp</result>
<!--拦截器一般配置在 result 元素之后 -->
<interceptor-ref name="myDefaultStack" />
</action>
<action name="testInterceptor"
class="cn.com.lzpeng.actions.TestInterceptorAction">
<result name="success">content.jsp</result>
<result name="login">login.jsp</result>
</action>
</package>
</struts>
最后是 4个 jsp界面
login.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>struts2</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<!—-
注意这里action="login.action"
因为要与Struts.xml里你注册的那个别名叫"login"
-->
<form action="login.action" method="post" >
<fieldset>
<legend>
用户登录
</legend>
<p align="center">
帐 号:
<input type="text" name="userBean.name" />
<!—-
注意这里一定要写name="bean.name",
因为要与LoginAction.java里面定义的bean对应
name是它的helloWorld的属性
-->
</p>
<p align="center">
密 码:
<input type="password" name="userBean.password" />
</p>
<p align="center">
<input type="submit" value="登 录" />
</p>
</fieldset>
</form>
</body>
</html>
success.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="testInterceptor.action">
<div align="center">
恭喜${userBean.name}登录成功了 !
<br>
只有通过这才能访问 content.jsp!
<br>
<br>
<input type="submit" value=" content.jsp">
<br>
<br>
主要演示拦截器 CheckLoginInterceptor 对 TestInterceptorAction 的拦截情况(可以试直接访问
TestInterceptorAction.action)
</div>
</form>
</body>
</html>
error.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>error</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<div align="center">
这是一个错误界面 ! 用以下帐号试试:<br>
id : java<br>
password: struts
</div>
<div>
<a href="login.jsp">返回登录界面</a>
</div>
</body>
</html>
content.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>content</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body><div align="center">
这里是 content.jsp!
<br>
成功通过拦截器 CheckLoginInterceptor......
<br>
复制上边地址 , 在新打开的窗口里粘贴 , 回车试试......
</div>
</body>
</html>