1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
package
csg.struts2.action;
/**
*
* @author 小夜的传说
* @2014-4-18
* @struts
* @csg.struts2.action
* @StrutsAction
* @2014-4-18上午1:03:37
*/
public
class
StrutsAction {
private
String message;
public
String getMessage() {
return
message;
}
public
void
setMessage(String message) {
this
.message = message;
}
public
String execute() {
message =
"execute方法"
;
return
"success"
;
}
public
String addUI() {
message =
"addUI方法"
;
return
"success"
;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<
html
>
<
head
>
<
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"
>
</
head
>
<
body
>
<%
request.getSession().setAttribute("user", "login");
%>
用户已登录
</
body
>
</
html
>
|
1
2
3
4
5
6
7
8
9
10
11
12
|
<?
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
=
"struts2"
namespace
=
"/test"
extends
=
"struts-default"
>
<!-- 通配符形式访问action -->
<
action
name
=
"list_*"
class
=
"csg.struts2.action.StrutsAction"
method
=
"{1}"
>
<
result
name
=
"success"
>/WEB-INF/page/success.jsp</
result
>
</
action
>
</
package
>
</
struts
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
package
csg.struts2.interceptor;
import
com.opensymphony.xwork2.ActionContext;
import
com.opensymphony.xwork2.ActionInvocation;
import
com.opensymphony.xwork2.interceptor.Interceptor;
/**
*
* @author 小夜的传说
* @2014-4-18
* @struts
* @csg.struts2.interceptor
* @LoginInterceptor
* @2014-4-18上午1:03:29
*/
public
class
LoginInterceptor
implements
Interceptor {
// 方法在拦截器被垃圾回收之前调用,用来回收init方法初始化的资源
public
void
destroy() {
}
// 方法在拦截器被创建之后,在对Action镜像拦截之前调用,使用这个方法主要是给拦截器做类似初始化的操作
public
void
init() {
}
public
String intercept(ActionInvocation invocation)
throws
Exception {
Object user = ActionContext.getContext().getSession().get(
"user"
);
if
(user !=
null
) {
// 如果user不为null,代表用户已经登录,允许执行action中的方法 这个结果就代表返回验证通过
return
invocation.invoke();
}
else
{
//提示用户登录,我这里直接提示,在实际项目中,肯定会返回到登录页面
ActionContext.getContext().put(
"message"
,
"你没有登录"
);
return
"success"
;
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<?
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
=
"struts2"
namespace
=
"/test"
extends
=
"struts-default"
>
<
interceptors
>
<
interceptor
name
=
"logininterceptor"
class
=
"csg.struts2.interceptor.LoginInterceptor"
/>
<!-- 自定义一个拦截器栈,下面你拦截只需要用这个栈名字就行了 -->
<
interceptor-stack
name
=
"loginstack"
>
<!-- 这个是系统拦截器栈,不这样写,就是当你单独一个action引用拦截器的时候就无法使用拦截器的中的核心功能 -->
<
interceptor-ref
name
=
"defaultStack"
/>
<!-- 这个引用你自己写的拦截器 -->
<
interceptor-ref
name
=
"logininterceptor"
/>
</
interceptor-stack
>
</
interceptors
>
<!-- 这样写的意思是 在这个package包下面的所有action方法都需要拦截器 -->
<
default-interceptor-ref
name
=
"loginstack"
></
default-interceptor-ref
>
<!-- 全局视图,这个就是说拦截到你没登录的时候提示你登录 ,你做的话可以提示返回登录界面-->
<
global-results
>
<
result
name
=
"success"
>/WEB-INF/page/message.jsp</
result
>
</
global-results
>
<!-- 通配符形式访问action -->
<
action
name
=
"list_*"
class
=
"csg.struts2.action.StrutsAction"
method
=
"{1}"
>
<
result
name
=
"success"
>/WEB-INF/page/success.jsp</
result
>
<!-- 也可以这样写 ,但是这样写就比较麻烦了,假如你下面还有action,这样你就每个action都需要引用,所以我注掉了-->
<!-- <interceptor-ref name="loginstack"></interceptor-ref> -->
</
action
>
</
package
>
</
struts
>
|
1
2
3
4
|
<!-- 这个是系统拦截器栈,不这样写,就是当你单独一个action引用拦截器的时候就无法使用拦截器的中的核心功能 -->
<
interceptor-ref
name
=
"defaultStack"
/>
<!-- 这个引用你自己写的拦截器 -->
<
interceptor-ref
name
=
"logininterceptor"
/>
|