写出一个你自己的MVC框架-基于对springMVC源码实现和理解(3):数据初始化(二)

前文已经对SpringMVC中DispatcherServlet数据初始化过程有了一定的认识,下面开始编码:

1.设计自定义注解@MyController:

package com.wbh.mymvc.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface MyController {
}
2.设计自定义注解@MyInterceptor:

package com.wbh.mymvc.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface MyInterceptor {
	
	
	/**
	 * 默认全匹配,匹配规则如下
	 * "/*"项目根目录下匹配,不包含子目录
	 * "/**"项目下全匹配,包括所有子目录下的请求
	 * "/Example/*"即Example下的请求匹配,不包含子目录,以此类推
	 * "!:"后跟的目录为要排除的目录
	 * @return
	 */
	String[] mappingPath() default {"/**"};
	
	String interceptionMethod() default "both";//默认拦截GET,POST方法
	
	int index() ;
	
}
3.设计自定义注解@MyRequestMapping:

package com.wbh.mymvc.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface MyRequestMapping {
	
	String value() default "/";
	String method() default "GET"; //默认请求方式为GET
	
}
4.设计拦截器接口BaseInterceptor:

package com.wbh.mymvc.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wbh.mymvc.ui.MyModelAndView;

/**
 * 基本的拦截器接口
 * @author wbh
 * 
 */
public interface BaseInterceptor {
	
	/**
	 * 先于获得handler
	 * @param request
	 * @param response
	 * @param handler
	 * @return
	 * @throws Exception
	 */
	boolean beforeHandler(HttpServletRequest request, HttpServletResponse response)
		    throws Exception;
	
	/**
	 * handler执行后执行,先于view加载
	 * @param request
	 * @param response
	 * @param handler
	 * @throws Exception
	 */
	void afterHandler(HttpServletRequest request, HttpServletResponse response, MyModelAndView modelAndeView)
		    throws Exception;
	
	/**
	 * view加载后执行
	 * @param request
	 * @param response
	 * @param handler
	 * @throws Exception
	 */
	void afterViewLoad(HttpServletRequest request, HttpServletResponse response)
		    throws Exception;
	
}
5.设计拦截器适配器 :

package com.wbh.mymvc.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wbh.mymvc.ui.MyModelAndView;

/**
 * 拦截器适配器
 * @author wbh
 *
 */
public abstract class InterceptorAdapt implements BaseInterceptor {
	
	@Override
	public boolean beforeHandler(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return true;  //默认允许
	}
	
	@Override
	public void afterHandler(HttpServletRequest request,
			HttpServletResponse response, MyModelAndView modelAndView) throws Exception {
	}
	
	@Override
	public void afterViewLoad(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
	}

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值