一 视图
1 loginForm.jsp
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>登录页面</title>
</head>
<body>
<h3>用户登录</h3>
${tip}
<s:form action="login">
<s:textfield name="username" label="用户名"/>
<s:password name="password" label="密码"/>
<s:submit value="登录"/>
</s:form>
</body>
</html>
2 viewBook.jsp
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>作者李刚已经出版的图书:</title>
<meta name="website" content="http://www.crazyit.org"/>
</head>
<body>
<h2>作者李刚已经出版的图书:</h2>
轻量级Java EE企业应用实战<br/>
疯狂iOS讲义<br/>
疯狂Java讲义<br/>
</body>
</html>
3 welcome.jsp
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>成功页面</title>
</head>
<body>
您已经登录!
<a href="viewBook">查看图书</a>
</body>
</html>
4 error.jsp
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>错误页面</title>
</head>
<body>
您不能登录!
</body>
</html>
二 配置文件
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.i18n.encoding" value="GBK"/>
<package name="lee" extends="struts-default">
<!-- 用户拦截器定义在该元素下 -->
<interceptors>
<!-- 定义了一个名为authority的拦截器 -->
<interceptor name="authority"
class="org.crazyit.app.interceptor.AuthorityInterceptor"/>
</interceptors>
<!-- 定义全局Result -->
<global-results>
<!-- 当返回login视图名时,转入loginForm.jsp页面 -->
<result name="login">/WEB-INF/content/loginForm.jsp</result>
</global-results>
<action name="login" class="org.crazyit.app.action.LoginAction">
<result name="error">/WEB-INF/content//error.jsp</result>
<result>/WEB-INF/content/welcome.jsp</result>
</action>
<!-- 定义一个名为viewBook的Action,其实现类为ActionSupport -->
<action name="viewBook">
<!-- 返回success视图名时,转入viewBook.jsp页面 -->
<result>/WEB-INF/content/viewBook.jsp</result>
<interceptor-ref name="defaultStack"/>
<!-- 应用自定义拦截器 -->
<interceptor-ref name="authority"/>
</action>
<action name="*">
<result>/WEB-INF/content/{1}.jsp</result>
</action>
</package>
</struts>
三 action
package org.crazyit.app.action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionContext;
import java.util.*;
public class LoginAction extends ActionSupport
{
private String username;
private String password;
// username的setter和getter方法
public void setUsername(String username)
{
this.username = username;
}
public String getUsername()
{
return this.username;
}
// password的setter和getter方法
public void setPassword(String password)
{
this.password = password;
}
public String getPassword()
{
return this.password;
}
public String execute() throws Exception
{
System.out.println("进入execute方法执行体..........");
if (getUsername().equals("crazyit.org")
&& getPassword().equals("leegang") )
{
ActionContext ctx = ActionContext.getContext();
Map<String,Object> session = ctx.getSession();
session.put("user" , getUsername());
return SUCCESS;
}
return ERROR;
}
}
四 拦截器
package org.crazyit.app.interceptor;
import com.opensymphony.xwork2.*;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import java.util.*;
// 权限检查拦截器继承AbstractInterceptor类
public class AuthorityInterceptor
extends AbstractInterceptor
{
// 拦截Action处理的拦截方法
public String intercept(ActionInvocation invocation)
throws Exception
{
// 取得请求相关的ActionContext实例
ActionContext ctx = invocation.getInvocationContext();
Map session = ctx.getSession();
// 取出Session里的user属性
String user = (String)session.get("user");
//如果没有登录,或者登录所用的用户名不是crazyit.org,都返回重新登录
if (user != null && user.equals("crazyit.org") )
{
return invocation.invoke();
}
// 如果没有登录,将服务器提示放入ActionContext中
ctx.put("tip" ,"您还没有登录,请输入crazyit.org,leegang登录系统");
// 返回login的逻辑视图
return Action.LOGIN;
}
}
五 测试