http://www.verejava.com/?id=1699128811725
1.打开 Eclipse 新建一个 test 项目 Eclipse -> File -> New -> Dynamic Web Project
2.在 test/WebContent 下面新建一个 test.jsp 文件
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
test
</body>
</html>
3.在 test/src 下面新建一个要拦截的 com.test.action.TestAction 类
package com.test.action;
import javax.servlet.http.HttpServletRequest;
import com.vere.mvc.ServletActionContext;
import com.vere.mvc.util.CParam;
public class TestAction {
public String execute() {
return "success";
}
}
4.在 test/src/VereMVC.xml 里面配置Action
<?xml version="1.0" encoding="UTF-8"?>
<VereMVC>
<constant name="charset" value="utf-8" />
<constant name="developMode" value="true" />
<constant name="language" value="zh_CN" />
<action name="test" class="com.test.action.TestAction">
<result name="success">/test.jsp</result>
</action>
</VereMVC>
5.在 test/src 新建一个拦截器 MyInterceptor implements Intercepter
package com.test.interceptor;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.vere.mvc.ServletActionContext;
import com.vere.mvc.interceptor.Interceptor;
public class MyInterceptor implements Interceptor {
@Override
public boolean execute() {
String actionName=ServletActionContext.getContextParam().getActionName();
if("test".equals(actionName))
{
//处理拦截 action 后的动作
System.out.println("拦截到了test action");
}
String methodName=ServletActionContext.getContextParam().getMethodName();
if("execute".equals(methodName))
{
//处理拦截 action的方法 后的动作
System.out.println("拦截到了test execute 方法");
}
return true;
}
}
6.在 test/src/WereMVC.xml 配置拦截器
<?xml version="1.0" encoding="UTF-8"?>
<VereMVC>
<constant name="charset" value="utf-8" />
<constant name="developMode" value="true" />
<constant name="language" value="zh_CN" />
<interceptor name="myInterceptor" class="com.test.interceptor.MyInterceptor" />
<action name="test" class="com.test.action.TestAction">
<result name="success">/test.jsp</result>
</action>
</VereMVC>
7.在 浏览器地址栏输入 http://localhost/test/test