1.建空项目

2.建包

Struts2学习-横切关注点-LMLPHP

3.建类

Struts2学习-横切关注点-LMLPHP

4.编写

package com.nf.action;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor; public class MeetIntercepter extends MethodFilterInterceptor {
protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
System.out.println("化妆...");
actionInvocation.getStack().setValue("face","第2个女朋友");
String s = actionInvocation.invoke();
System.out.println("卸妆");
return s;
}
}
package com.nf.action;

import com.opensymphony.xwork2.ActionSupport;

public class MyAction1 extends ActionSupport{
private String face = "火星人";
public String getFace() {
return face;
}
public void setFace(String face) {
this.face = face;
}
@Override
public String execute() throws Exception{
System.out.println("一起吃饭,展现:"+face);
return this.NONE;
}
}
其他几个基本是一样的,就不发了

5.编写struts.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd"> <struts>
<package name="mypackage" extends="struts-default"> <interceptors>
<interceptor name="meet" class="com.nf.action.MeetIntercepter"></interceptor>
<interceptor-stack name="myStack">
<interceptor-ref name="meet"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors> <default-interceptor-ref name="myStack"></default-interceptor-ref> <action name="a1" class="com.nf.action.MyAction1"></action>
<action name="a2" class="com.nf.action.MyAction2"></action>
<action name="a3" class="com.nf.action.MyAction3"></action>
</package>
</struts>

运行项目时

Struts2学习-横切关注点-LMLPHP

Struts2学习-横切关注点-LMLPHP

http://localhost:8080/ss/a1

ps:网上有关于横切关注点的内容,请自行学习更多的有关内容。(难)

地址:https://gitee.com/MuNianShi/user6.git

05-11 18:21