您的位置:首页 > 编程语言 > Java开发

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

2015-03-10 19:23 609 查看
前文已经对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 {
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  springmvc 源代码 mvc
相关文章推荐