手写实现MVC框架之前我们先来回顾一下SpringMVC框架的原理。后续我们再模仿SpringMVC实现自定义的MVC框架。
具体流程如下:
首先我们构建一个maven工程,引入相关的坐标,自定义一个Servlet为前端控制器,并且再web.xml中对前端控制器的Servlet进行配置并且创建配置文件mvc.properties,并加入到web.xml中进行配置,详细代码如下
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.lchgroupId>
<artifactId>mvcartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
<name>mvc Maven Webappname>
<url>http://www.example.comurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.11maven.compiler.source>
<maven.compiler.target>1.11maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-lang3artifactId>
<version>3.12.0version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.1version>
<configuration>
<source>11source>
<target>11target>
<encoding>utf-8encoding>
<compilerArgs>
<arg>-parametersarg>
compilerArgs>
configuration>
plugin>
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<version>2.2version>
<configuration>
<port>8080port>
<path>/path>
configuration>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<configuration>
<source>11source>
<target>11target>
configuration>
plugin>
plugins>
build>
project>
编写前端控制器类,这里我们先创建,后续再完成这个Servlet里边的代码
public class LchDispatcherServlet extends HttpServlet {
...
}
springmvc.properties文件,配置包扫描scanPackage=com.lch.demo
scanPackage=com.lch.demo
然后再web.xml中对Servlet以及配置文件进行配置:
DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Applicationdisplay-name>
<servlet>
<servlet-name>mvcservlet-name>
<servlet-class>com.lch.mvcframework.servlet.LchDispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>springmvc.propertiesparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>mvcservlet-name>
<url-pattern>/*url-pattern>
servlet-mapping>
web-app>
到这里我们流程图片中的第一步就完成啦,下面我们再去完成第二部,自定义注解的开发
@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LchAutowired {
String value() default "";
}
@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface LchController {
String value() default "";
}
@Documented
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface LchRequestMapping {
String value() default "";
}
@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface LchServive {
String value() default "";
}
编写Controller,按照我们SpringMVC中的使用添加相应的注解
@LchController
@LchRequestMapping("/demo")
public class DemoController {
@LchAutowired
private IDemoService demoService;
@LchRequestMapping("/query")
public String query(String name, HttpServletRequest request, HttpServletResponse response){
String result = demoService.get(name);
return result;
}
}
编写Service接口以及实现类ServiceImpl,把我们自定义的service注解添加上去
public interface IDemoService {
String get(String name);
}
@LchServive
public class DemoServiceImpl implements IDemoService {
@Override
public String get(String name) {
System.out.println("service中的name参数:"+name);
return name;
}
}
下面就是自定义MVC框架最关键的步骤就是完成我们之前创建好的前端控制器,中代码的编写,在这里我们需要实现注解的扫描,包扫描,Bean的实例化,依赖注入,首先我们要重写HttpServlet的如下几个方法:
@Override
public void init(ServletConfig config) throws ServletException {
...
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
...
}
然后我们先来完成初始化中我们需要的代码逻辑,首先我们分析得出,再初始化的过程中我们需要完成的内容如下:
处理请求
package com.lch.mvcframework.servlet;
import com.lch.mvcframework.annotations.LchAutowired;
import com.lch.mvcframework.annotations.LchController;
import com.lch.mvcframework.annotations.LchRequestMapping;
import com.lch.mvcframework.annotations.LchServive;
import com.lch.mvcframework.pojo.Handler;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LchDispatcherServlet extends HttpServlet {
//配置文件
private Properties properties = new Properties();
//缓存扫描到类的全限定类名
private List<String> classNames = new ArrayList<>();
//ioc容器
private Map<String,Object> ioc = new HashMap<>();
//handlerMapping
private List<Handler> handlerMapping = new ArrayList<>();
@Override
public void init(ServletConfig config) throws ServletException {
String contextConfigLocation = config.getInitParameter("contextConfigLocation");
//处理请求
//1.加载配置文件springmvc.properties
doLoadConfig(contextConfigLocation);
//2.扫描相关的类,扫描注解
doScan(properties.getProperty("scanPackage"));
//3.初始化bean对象(实现IOC容器基于注解)
doInstance();
//4.实现依赖注入
doAutowired();
//5.构造一个HandlerMapping处理器映射器将配置好的url和Method建立映射关系
initHandlerMapping();
System.out.println("自定义mvc初始化完成");
//6.等待请求进入处理请求
}
/**构造一个HandlerMapping处理器映射器
*
*/
private void initHandlerMapping() {
if(ioc.isEmpty()) return;
for (Map.Entry<String, Object> entry : ioc.entrySet()) {
Class<?> aClass = entry.getValue().getClass();
if(!aClass.isAnnotationPresent(LchController.class)){
continue;
}
String baseUrl = "";
if(aClass.isAnnotationPresent(LchRequestMapping.class)){
LchRequestMapping annotations = aClass.getAnnotation(LchRequestMapping.class);
baseUrl = annotations.value(); //demo
}
//获取方法
Method[] methods = aClass.getMethods();
for (Method method : methods) {
//方法没有标识requestMapping不处理
if(!method.isAnnotationPresent(LchRequestMapping.class)){
continue;
}
LchRequestMapping annotation = method.getAnnotation(LchRequestMapping.class);
String value = annotation.value();
String url = baseUrl + value;
//把method的所有信息封装成一个Handler对象
Handler handler = new Handler(entry.getValue(),method, Pattern.compile(url));
//处理计算参数位置信息
Parameter[] parameters = method.getParameters();
for (int j = 0; j < parameters.length; j++) {
Parameter parameter = parameters[j];
if(parameter.getType() == HttpServletRequest.class || parameter.getType() == HttpServletResponse.class){
//如果是request和response对象,那么参数名称写HttpServletRequest和HttpServletResponse
handler.getParamIndexMapping().put(parameter.getType().getSimpleName(),j);
}else{
handler.getParamIndexMapping().put(parameter.getName(),j);
}
}
handlerMapping.add(handler);
}
}
}
/**依赖注入
*
*/
private void doAutowired() {
if(ioc.isEmpty()) return;
//有对象判断依赖注入
//遍历ioc中的所有对象,查看对象中是否有@LchAutowired注解,如果有需要维护依赖关系
for (Map.Entry<String, Object> stringObjectEntry : ioc.entrySet()) {
Field[] declaredFields = stringObjectEntry.getValue().getClass().getDeclaredFields();
for (int i = 0; i < declaredFields.length; i++) {
Field declaredField = declaredFields[i];
if(!declaredField.isAnnotationPresent(LchAutowired.class)){
continue;
}
LchAutowired annotation = declaredFields[i].getAnnotation(LchAutowired.class);
String beanName = annotation.value();
if("".equals(beanName.trim())){
//没有配置具体的bean id,根据当前字段类型注入(接口注入) IDemoService
beanName = declaredField.getType().getName();
}
//开启赋值
declaredField.setAccessible(true);
try {
declaredField.set(stringObjectEntry.getValue(),ioc.get(beanName));
}catch(Exception e){
e.printStackTrace();
}
}
}
}
/**ioc容器
*
*/
private void doInstance(){
if(classNames.size()==0) return;
try{
for (int i = 0; i < classNames.size(); i++) {
String className = classNames.get(i);
//反射
Class<?> aClass = Class.forName(className);
//区分Controller和Service
if(aClass.isAnnotationPresent(LchController.class)){
String simpleName = aClass.getSimpleName();
String lowerFirstSimpleName = lowerFirst(simpleName);
Object o = aClass.newInstance();
ioc.put(lowerFirstSimpleName,o);
}else if(aClass.isAnnotationPresent(LchServive.class)){
LchServive annotation = aClass.getAnnotation(LchServive.class);
String beanName = annotation.value();
//如果制定了id,就以指定的为准
if(!"".equals(beanName.trim())){
ioc.put(beanName,aClass.newInstance());
}else{
//如果没有指定value,就以类名首字母小写
beanName = lowerFirst(aClass.getSimpleName());
ioc.put(beanName,aClass.newInstance());
}
//service层往往是有接口的,面向接口开发,此时再以接口名为id,放入一份对象到ioc容器中
//便于后期根据接口注入
Class<?>[] interfaces = aClass.getInterfaces();
for (int j = 0; j < interfaces.length; j++) {
Class<?> anInterface = interfaces[j];
ioc.put(anInterface.getName(),aClass.newInstance());
}
}else{
continue;
}
}
}catch (Exception e){
e.printStackTrace();
}
}
//首字母小写转化
public String lowerFirst(String str){
char[] chars = str.toCharArray();
if(chars[0]>= 'A'&& chars[0] <= 'Z'){
chars[0]+=32;
}
return String.valueOf(chars);
}
/**扫描类
* 找到磁盘上的文件夹通过File判断子类
*/
private void doScan(String scanPackage) {
String scanPackagePath = Thread.currentThread().getContextClassLoader().getResource("").getPath() + scanPackage.replaceAll("\\.","/");
File pack = new File(scanPackagePath);
File[] files = pack.listFiles();
for (File file : files) {
if(file.isDirectory()){
//递归进来
doScan(scanPackage+"."+file.getName());
}else if(file.getName().endsWith(".class")){
String className = scanPackage + "." + file.getName().replaceAll(".class", "");
classNames.add(className);
}
}
}
/**加载配置文件
*
* @param contextConfigLocation
*/
private void doLoadConfig(String contextConfigLocation) {
InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(contextConfigLocation);
try {
properties.load(resourceAsStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//接收处理请求
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//等待请求进入处理请求:根据url找到对应的method方法调用
//获取url
//String requestURI = req.getRequestURI();
//获取到一个反射方法
//Method method = handlerMapping.get(requestURI);
//反射调用,需要传入对象,需要传入参数,需要把对象缓存起来
//需要改造initHandlerMapping方法
//method.invoke()
//根据uri获取到能够处理当前请求的Handler,从handlermapping中
Handler handler = getHandler(req);
//如果没找到
if(handler == null ){
resp.getWriter().write("404 not found");
return;
}
//参数绑定
//获取所有的参数类型数组,这个数组的长度就是我们要传入的args数组的长度
Class<?>[] parameterTypes = handler.getMethod().getParameterTypes();
//根据上述数组长度创建新的参数数组
Object[] paraValues = new Object[parameterTypes.length];
//向参数数组中塞值
Map<String, String[]> parameterMap = req.getParameterMap();
for (Map.Entry<String, String[]> param : parameterMap.entrySet()) {
String value = StringUtils.join(param.getValue(), ",");
//填充数据
if(!handler.getParamIndexMapping().containsKey(param.getKey())){
continue;
}
//方法形参确实有该参数,找到它的索引位置,对应的把参数值放入
Integer index = handler.getParamIndexMapping().get(param.getKey());
paraValues[index] = value;
}
int requestIndex = handler.getParamIndexMapping().get(HttpServletRequest.class.getSimpleName());
paraValues[requestIndex] = req;
int responseIndex = handler.getParamIndexMapping().get(HttpServletResponse.class.getSimpleName());
paraValues[responseIndex] = resp;
//最终调用Handler的method属性
try {
handler.getMethod().invoke(handler.getController(),paraValues);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
private Handler getHandler(HttpServletRequest req) {
String url = req.getRequestURI();
if(handlerMapping.size() == 0) return null;
for (Handler handler : handlerMapping) {
Matcher matcher = handler.getPattern().matcher(url);
if(!matcher.matches()) continue;
return handler;
}
return null;
}
}
首先我们从初始化开始对之前提到的步骤进行实现,每一个步骤我们单独封装了一个方法,如下
@Override
public void init(ServletConfig config) throws ServletException {
String contextConfigLocation = config.getInitParameter("contextConfigLocation");
//处理请求
//1.加载配置文件springmvc.properties
doLoadConfig(contextConfigLocation);
//2.扫描相关的类,扫描注解
doScan(properties.getProperty("scanPackage"));
//3.初始化bean对象(实现IOC容器基于注解)
doInstance();
//4.实现依赖注入
doAutowired();
//5.构造一个HandlerMapping处理器映射器将配置好的url和Method建立映射关系
initHandlerMapping();
System.out.println("自定义mvc初始化完成");
//6.等待请求进入处理请求
}
加载配置文件springmvc.properties,把配置文件读取到流中,添加配置文件的全局变量用来存储配置信息并把解析的流设置到全局变量properties中
//配置文件
private Properties properties = new Properties();
/**加载配置文件
*
* @param contextConfigLocation
*/
private void doLoadConfig(String contextConfigLocation) {
InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(contextConfigLocation);
try {
properties.load(resourceAsStream);
} catch (IOException e) {
e.printStackTrace();
}
}
扫描相关的类,扫描注解,首先我们传递的参数就是我们在配置文件中配置的包扫描的路径即:com.lch.demo,在这里需要判断子文件夹的情况所以我们使用了File通过判断进行递归处理,解析并且拼接出当前文件类的全限定类名,我们添加一个全局变量classNames用于缓存扫描到的类的全限定类名。如下:
//缓存扫描到类的全限定类名
private List<String> classNames = new ArrayList<>();
/**扫描类
* 找到磁盘上的文件夹通过File判断子类
*/
private void doScan(String scanPackage) {
String scanPackagePath = Thread.currentThread().getContextClassLoader().getResource("").getPath() + scanPackage.replaceAll("\\.","/");
File pack = new File(scanPackagePath);
File[] files = pack.listFiles();
for (File file : files) {
if(file.isDirectory()){
//递归进来
doScan(scanPackage+"."+file.getName());
}else if(file.getName().endsWith(".class")){
String className = scanPackage + "." + file.getName().replaceAll(".class", "");
classNames.add(className);
}
}
}
初始化bean对象(实现IOC容器基于注解),在这里我们需要对Controller和Service自定义注解标记的类进行初始化bean对象,首先我们需要识别标记的注解类,然后再去完成实例化的创建,最后我们在把得到的对象存放到ioc集合中,方便后续使用;这里我们也需要添加一个全局的ioc容器,即Map
//ioc容器
private Map<String,Object> ioc = new HashMap<>();
/**ioc容器
*
*/
private void doInstance(){
if(classNames.size()==0) return;
try{
for (int i = 0; i < classNames.size(); i++) {
String className = classNames.get(i);
//反射
Class<?> aClass = Class.forName(className);
//区分Controller和Service
if(aClass.isAnnotationPresent(LchController.class)){
String simpleName = aClass.getSimpleName();
String lowerFirstSimpleName = lowerFirst(simpleName);
Object o = aClass.newInstance();
ioc.put(lowerFirstSimpleName,o);
}else if(aClass.isAnnotationPresent(LchServive.class)){
LchServive annotation = aClass.getAnnotation(LchServive.class);
String beanName = annotation.value();
//如果制定了id,就以指定的为准
if(!"".equals(beanName.trim())){
ioc.put(beanName,aClass.newInstance());
}else{
//如果没有指定value,就以类名首字母小写
beanName = lowerFirst(aClass.getSimpleName());
ioc.put(beanName,aClass.newInstance());
}
//service层往往是有接口的,面向接口开发,此时再以接口名为id,放入一份对象到ioc容器中
//便于后期根据接口注入
Class<?>[] interfaces = aClass.getInterfaces();
for (int j = 0; j < interfaces.length; j++) {
Class<?> anInterface = interfaces[j];
ioc.put(anInterface.getName(),aClass.newInstance());
}
}else{
continue;
}
}
}catch (Exception e){
e.printStackTrace();
}
}
//首字母小写转化
public String lowerFirst(String str){
char[] chars = str.toCharArray();
if(chars[0]>= 'A'&& chars[0] <= 'Z'){
chars[0]+=32;
}
return String.valueOf(chars);
}
然后实现依赖注入,这里就是对Autowired自定义注解的解析和处理,完成对象之间的依赖关系,遍历ioc中的所有对象,查看对象中是否有@LchAutowired注解,如果有需要维护依赖关系
/**依赖注入
*
*/
private void doAutowired() {
if(ioc.isEmpty()) return;
//有对象判断依赖注入
//遍历ioc中的所有对象,查看对象中是否有@LchAutowired注解,如果有需要维护依赖关系
for (Map.Entry<String, Object> stringObjectEntry : ioc.entrySet()) {
Field[] declaredFields = stringObjectEntry.getValue().getClass().getDeclaredFields();
for (int i = 0; i < declaredFields.length; i++) {
Field declaredField = declaredFields[i];
if(!declaredField.isAnnotationPresent(LchAutowired.class)){
continue;
}
LchAutowired annotation = declaredFields[i].getAnnotation(LchAutowired.class);
String beanName = annotation.value();
if("".equals(beanName.trim())){
//没有配置具体的bean id,根据当前字段类型注入(接口注入) IDemoService
beanName = declaredField.getType().getName();
}
//开启赋值
declaredField.setAccessible(true);
try {
declaredField.set(stringObjectEntry.getValue(),ioc.get(beanName));
}catch(Exception e){
e.printStackTrace();
}
}
}
}
构造一个HandlerMapping处理器映射器将配置好的url和Method建立映射关系,这里我们定义了一个Handler类,去存放封装Handler方法相关的信息
Handler.java,在构造其中我们对paramIndexMapping就行初始化
/**封装Handler方法相关的信息
*
*/
public class Handler {
//method.invoke(obj,...)
private Object controller;
private Method method;
private Pattern pattern;
//参数顺序,为了参数绑定 key参数名 value第几个参数
private Map<String,Integer> paramIndexMapping;
public Object getController() {
return controller;
}
public void setController(Object controller) {
this.controller = controller;
}
public Method getMethod() {
return method;
}
public void setMethod(Method method) {
this.method = method;
}
public Pattern getPattern() {
return pattern;
}
public void setPattern(Pattern pattern) {
this.pattern = pattern;
}
public Map<String, Integer> getParamIndexMapping() {
return paramIndexMapping;
}
public void setParamIndexMapping(Map<String, Integer> paramIndexMapping) {
this.paramIndexMapping = paramIndexMapping;
}
public Handler(Object controller, Method method, Pattern pattern) {
this.controller = controller;
this.method = method;
this.pattern = pattern;
this.paramIndexMapping = new HashMap<>();
}
}
然后我们添加一个全局变量handlerMapping 用来存放处理器信息
//handlerMapping
private List<Handler> handlerMapping = new ArrayList<>();
/**构造一个HandlerMapping处理器映射器
*
*/
private void initHandlerMapping() {
if(ioc.isEmpty()) return;
for (Map.Entry<String, Object> entry : ioc.entrySet()) {
Class<?> aClass = entry.getValue().getClass();
if(!aClass.isAnnotationPresent(LchController.class)){
continue;
}
String baseUrl = "";
if(aClass.isAnnotationPresent(LchRequestMapping.class)){
LchRequestMapping annotations = aClass.getAnnotation(LchRequestMapping.class);
baseUrl = annotations.value(); //demo
}
//获取方法
Method[] methods = aClass.getMethods();
for (Method method : methods) {
//方法没有标识requestMapping不处理
if(!method.isAnnotationPresent(LchRequestMapping.class)){
continue;
}
LchRequestMapping annotation = method.getAnnotation(LchRequestMapping.class);
String value = annotation.value();
String url = baseUrl + value;
//把method的所有信息封装成一个Handler对象
Handler handler = new Handler(entry.getValue(),method, Pattern.compile(url));
//处理计算参数位置信息
Parameter[] parameters = method.getParameters();
for (int j = 0; j < parameters.length; j++) {
Parameter parameter = parameters[j];
if(parameter.getType() == HttpServletRequest.class || parameter.getType() == HttpServletResponse.class){
//如果是request和response对象,那么参数名称写HttpServletRequest和HttpServletResponse
handler.getParamIndexMapping().put(parameter.getType().getSimpleName(),j);
}else{
handler.getParamIndexMapping().put(parameter.getName(),j);
}
}
handlerMapping.add(handler);
}
}
}
以上就是我们Servlet在初始化过程中完成的任务,下面我们再去完成doGet和doPost方法。这里我们doGet调用的是doPost。doPost代码如下:
在这我们添加了一个getHandler方法,从handlerMapping中拿到处理器Handler对象。
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//等待请求进入处理请求:根据url找到对应的method方法调用
//获取url
//String requestURI = req.getRequestURI();
//获取到一个反射方法
//Method method = handlerMapping.get(requestURI);
//反射调用,需要传入对象,需要传入参数,需要把对象缓存起来
//需要改造initHandlerMapping方法
//method.invoke()
//根据uri获取到能够处理当前请求的Handler,从handlermapping中
Handler handler = getHandler(req);
//如果没找到
if(handler == null ){
resp.getWriter().write("404 not found");
return;
}
//参数绑定
//获取所有的参数类型数组,这个数组的长度就是我们要传入的args数组的长度
Class<?>[] parameterTypes = handler.getMethod().getParameterTypes();
//根据上述数组长度创建新的参数数组
Object[] paraValues = new Object[parameterTypes.length];
//向参数数组中塞值
Map<String, String[]> parameterMap = req.getParameterMap();
for (Map.Entry<String, String[]> param : parameterMap.entrySet()) {
String value = StringUtils.join(param.getValue(), ",");
//填充数据
if(!handler.getParamIndexMapping().containsKey(param.getKey())){
continue;
}
//方法形参确实有该参数,找到它的索引位置,对应的把参数值放入
Integer index = handler.getParamIndexMapping().get(param.getKey());
paraValues[index] = value;
}
int requestIndex = handler.getParamIndexMapping().get(HttpServletRequest.class.getSimpleName());
paraValues[requestIndex] = req;
int responseIndex = handler.getParamIndexMapping().get(HttpServletResponse.class.getSimpleName());
paraValues[responseIndex] = resp;
//最终调用Handler的method属性
try {
handler.getMethod().invoke(handler.getController(),paraValues);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
private Handler getHandler(HttpServletRequest req) {
String url = req.getRequestURI();
if(handlerMapping.size() == 0) return null;
for (Handler handler : handlerMapping) {
Matcher matcher = handler.getPattern().matcher(url);
if(!matcher.matches()) continue;
return handler;
}
return null;
}
到这我们就完成了手写实现MVC框架,下面我们测试一下。
启动服务,发送http://localhost:8080/demo/query?name=lisi