VereMVC 之 拦截器Interceptor 拦截 Action 和 方法

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 

http://www.verejava.com/?id=1699128811725

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值