1
2
3
4
5
|
<
form
action
=
"login.do"
method
=
"post"
>
username:<
input
type
=
"text"
name
=
"username"
/><
br
>
password:<
input
type
=
"password"
name
=
"password"
><
br
>
<
input
type
=
"submit"
value
=
"login"
>
</
form
>
|
1
2
3
4
5
6
7
8
9
10
11
12
|
/**
*
* <p>[描述信息:登陆表单类]</p>
*
* @author bruce.yang
* @version 1.0 Created on 2013-8-13 下午2:04:09
*/
public
class
LoginActionForm
extends
ActionForm {
private
String username;
private
String password;
//setter,getter方法
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
/**
*
* <p>[描述信息:登陆Action]</p>
*
* @author bruce.yang
* @version 1.0 Created on 2013-8-13 下午2:04:38
*/
public
class
LoginAction
extends
Action {
private
ILoginService loginService;
@Override
public
ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LoginActionForm laf = (LoginActionForm) form;
String
username = laf.getUsername();
String
password = laf.getPassword();
boolean ret = loginService.login(username, password);
if
(ret) {
return
mapping.findForward(
"success"
);
}
return
mapping.findForward(
"fail"
);
}
//loginService的setter,getter方法
}
|
1
2
3
4
5
6
7
8
|
public
interface
ILoginService {
public
boolean login(
String
userName,
String
password);
}
public
interface
ILogService {
public
void
log();
public
void
logArg(JoinPoint point);
public
void
logArgAndReturn(JoinPoint point,
Object
returnObj);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
/**
*
* <p>[描述信息:登录Service类]</p>
*
* @author bruce.yang
* @version 1.0 Created on 2013-8-13 下午1:45:32
*/
public
class
LoginServiceImpl
implements
ILoginService {
/**
*
* <p>功能实现描述:登录处理逻辑(这里是假实现)</p>
*
* @see com.bruceyang.login.service.ILoginService#login(java.lang.String, java.lang.String)
* @author: bruce.yang
* @date: Created on 2013-8-13 下午1:45:32
*/
public
boolean
login(String userName, String password) {
StringBuffer sb=
new
StringBuffer();
sb.append(
"Target:"
).append(
"login:"
).append(userName).append(
","
).append(password);
System.out.println(sb.toString());
return
true
;
}
}
|
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
/**
*
* <p>[描述信息:操作日志记录Service]</p>
*
* @author bruce.yang
* @version 1.0 Created on 2013-8-13 下午1:45:32
*/
public
class
LogServiceImpl
implements
ILogService {
/**
*
* <p>功能实现描述:最简单的情况</p>
*
* @see com.bruceyang.login.service.ILogService#log()
* @author: bruce.yang
* @date: Created on 2013-8-13 下午1:46:17
*/
public
void
log() {
System.out.println(
new
SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss"
).format(
new
Date())+
" *******Log*********"
);
}
/**
*
* <p>功能实现描述:有参无返回值的方法</p>
*
* @see com.bruceyang.login.service.ILogService#logArg(org.aspectj.lang.JoinPoint)
* @author: bruce.yang
* @date: Created on 2013-8-13 下午1:43:43
*/
public
void
logArg(JoinPoint point) {
StringBuffer sb=
new
StringBuffer();
//获取连接点所在的目标对象
Object obj=point.getTarget();
//获取连接点的方法签名对象
String method=point.getSignature().getName();
//获取连接点方法运行时的入参列表
Object[] args = point.getArgs();
sb.append(
new
SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss"
).format(
new
Date())).append(
" "
);
sb.append(obj.toString().substring(
0
,obj.toString().indexOf(
'@'
)));
sb.append(
"."
).append(method).append(
" "
);
sb.append(
"Args:["
);
if
(args !=
null
) {
for
(
int
i=
0
;i<args.length;i++){
Object o=args[i];
sb.append(o);
if
(i<args.length-
1
){
sb.append(
","
);
}
}
}
sb.append(
"]"
);
System.out.println(sb.toString());
}
/**
*
* <p>功能实现描述:有参并有返回值的方法</p>
*
* @see com.bruceyang.login.service.ILogService#logArgAndReturn(org.aspectj.lang.JoinPoint, java.lang.Object)
* @author: bruce.yang
* @date: Created on 2013-8-13 下午1:43:17
*/
public
void
logArgAndReturn(JoinPoint point, Object returnObj) {
StringBuffer sb=
new
StringBuffer();
//获取连接点所在的目标对象
Object obj=point.getTarget();
//获取连接点的方法签名对象
String method=point.getSignature().getName();
//获取连接点方法运行时的入参列表
Object[] args = point.getArgs();
sb.append(
new
SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss"
).format(
new
Date())).append(
" "
);
sb.append(obj.toString().substring(
0
,obj.toString().indexOf(
'@'
)));
sb.append(
"."
).append(method).append(
" "
);
sb.append(
"Args:["
);
if
(args !=
null
) {
for
(
int
i=
0
;i<args.length;i++){
Object o=args[i];
sb.append(o);
if
(i<args.length-
1
){
sb.append(
","
);
}
}
}
sb.append(
"]"
).append(
" "
);
sb.append(
"Ret:["
).append(returnObj).append(
"]"
);
System.out.println(sb.toString());
}
}
|
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
|
<!-- spring config -->
<
context-param
>
<
param-name
>contextConfigLocation</
param-name
>
<
param-value
>classpath:config/spring/app*.xml</
param-value
>
</
context-param
>
<
listener
>
<
listener-class
>org.springframework.web.context.ContextLoaderListener</
listener-class
>
</
listener
>
<!-- struts config -->
<
servlet
>
<
servlet-name
>action</
servlet-name
>
<
servlet-class
>org.apache.struts.action.ActionServlet</
servlet-class
>
<
init-param
>
<
param-name
>config</
param-name
>
<
param-value
>/config/struts/struts-config.xml</
param-value
>
</
init-param
>
<
init-param
>
<
param-name
>debug</
param-name
>
<
param-value
>2</
param-value
>
</
init-param
>
<
init-param
>
<
param-name
>detail</
param-name
>
<
param-value
>2</
param-value
>
</
init-param
>
<
load-on-startup
>2</
load-on-startup
>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>action</
servlet-name
>
<
url-pattern
>*.do</
url-pattern
>
</
servlet-mapping
>
|
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
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
beans
xmlns
=
"http://www.springframework.org/schema/beans"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
xmlns:aop
=
"http://www.springframework.org/schema/aop"
xmlns:tx
=
"http://www.springframework.org/schema/tx"
>
<
bean
id
=
"logService"
class
=
"com.bruceyang.login.service.impl.LogServiceImpl"
></
bean
>
<
bean
id
=
"loginService"
class
=
"com.bruceyang.login.service.impl.LoginServiceImpl"
></
bean
>
<
aop:config
>
<!-- 切入点 -->
<
aop:pointcut
expression
=
"execution(* com.bruceyang.login.service.impl.Login*.*(..))"
id
=
"myPointcut"
/>
<!-- 切面: 将哪个对象中的哪个方法,织入到哪个切入点 -->
<
aop:aspect
id
=
"dd"
ref
=
"logService"
>
<!-- 前置通知
<aop:before method="log" pointcut-ref="myPointcut" />
<aop:after method="logArg" pointcut-ref="myPointcut"/>
<aop:after-returning method="logArgAndReturn" returning="returnObj" pointcut-ref="myPointcut"/>
-->
<
aop:before
method
=
"log"
pointcut-ref
=
"myPointcut"
/>
<
aop:after
method
=
"logArg"
pointcut-ref
=
"myPointcut"
/>
<
aop:after-returning
method
=
"logArgAndReturn"
returning
=
"returnObj"
pointcut-ref
=
"myPointcut"
/>
</
aop:aspect
>
</
aop:config
>
</
beans
>
|
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
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<
struts-config
>
<
form-beans
>
<
form-bean
name
=
"loginActionForm"
type
=
"com.bruceyang.login.web.form.LoginActionForm"
/>
</
form-beans
>
<
action-mappings
>
<
action
path
=
"/tologin"
forward
=
"/login.jsp"
/>
<
action
path
=
"/login"
type
=
"org.springframework.web.struts.DelegatingActionProxy"
name
=
"loginActionForm"
scope
=
"request"
input
=
"/login.jsp"
>
<
forward
name
=
"success"
path
=
"/success.jsp"
/>
<
forward
name
=
"fail"
path
=
"/login.jsp"
/>
</
action
>
</
action-mappings
>
<
plug-in
className
=
"org.springframework.web.struts.ContextLoaderPlugIn"
>
<
set-property
property
=
"contextConfigLocation"
value
=
"/WEB-INF/classes/config/struts/action-servlet.xml"
/>
</
plug-in
>
</
struts-config
>
|
1
2
3
4
5
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<
beans
>
<
import
resource
=
"login-action.xml"
/>
</
beans
>
|
1
2
3
4
5
6
7
8
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<
beans
>
<
bean
name
=
"/login"
class
=
"com.bruceyang.login.web.action.LoginAction"
>
<
property
name
=
"loginService"
ref
=
"loginService"
/>
</
bean
>
</
beans
>
|