从0开始写JavaWeb框架系列(5)从0开始写SamrtFrameWork:加载控制器

1、加载带有@Controller注解的控制器java

    1.一、首先咱们须要获取带有@Controller注解的全部类,所以能够经过咱们事先写好的ClassHelper.java来获取。apache

    1.二、获取到全部带有@Controller注解的Class对象后,进而能够经过反射来获取带有@Action注解的方法,获取@Action的值(也就是请求表达式),进而获取请求方法和请求路径。数组

    1.三、因此咱们须要把请求方法和请求路径封装到一个Request对象中。app

package org.smart4j.framework.bean;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

public class Request {

	/**
	 * 请求方法
	 */
	private String requestMethod;
	
	/**
	 * 请求路径
	 */
	private String requestPath;
	
	
	public Request() {

	}


	public Request(String requestMethod, String requestPath) {
		this.requestMethod = requestMethod;
		this.requestPath = requestPath;
	}


	public String getRequestMethod() {
		return requestMethod;
	}


	public String getRequestPath() {
		return requestPath;
	}
	
	
	//TODO
	@Override
	public int hashCode() {
		return HashCodeBuilder.reflectionHashCode(this);
	}
	
	
	@Override
	public boolean equals(Object obj) {
		
		return EqualsBuilder.reflectionEquals(this,obj);
	}	
}

    1.四、如何根据Request对象的属性找到须要调用的方法呢?这时候咱们须要编写一个处理对象Handler类。来封装须要调用的Class对象的方法。框架

package org.smart4j.framework.bean;

import java.lang.reflect.Method;

/**
 * 封装Action信息
 * @author Admin
 *
 */
public class Handler {
	
	
	/**
	 * controller类
	 */
	private Class<?> controllerClass;
	
	/**
	 * method方法
	 */
	private Method actionMethod;

	public Handler() {
		
	}

	public Handler(Class<?> controllerClass, Method actionMethod) {
		this.controllerClass = controllerClass;
		this.actionMethod = actionMethod;
	}
	
	
	

	public Class<?> getControllerClass() {
		return controllerClass;
	}

	public Method getActionMethod() {
		return actionMethod;
	}
	
	
}

    1.五、咱们能够把Request和Handler进行一个关联映射。放入一个Map中。当客户端一个请求(HttpServletRequest)过来的时候,获取请求的url和ActionMap里的key( Request对象 )作比较,若是相等的话, 就找到ActionMap的key对应的value(也就是Handler处理器)来调用相应的方法。ide

    1.六、最后,咱们创建一个ControllerHelper.java类实现一个Map映射来初始化咱们封装好的Request对象和Handler对象,保存处理器和映射关系。ui

package org.smart4j.framework.helper;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections4.CollectionUtils;
import org.smart4j.framework.annotation.Action;
import org.smart4j.framework.annotation.Service;
import org.smart4j.framework.bean.Handler;
import org.smart4j.framework.bean.Request;
import org.smart4j.framework.util.ArrayUtil;

/**
 * 控制器助手类
 * @author Admin
 *
 */
public final class ControllerHelper {

	/**
	 *用于存放请求和处理器的映射关系 
	 */
	private static final Map<Request,Handler> ACTION_MAP = new HashMap<Request,Handler>();

	//初始化映射关系
	static{
		//获取全部@Controller注解下的全部Java类的Class对象
		Set<Class<?>> controllerClassSet = ClassHelper.getClassController();

		if( CollectionUtils.isNotEmpty( controllerClassSet ) ){

			for (Class<?> controllerClass : controllerClassSet) {			//遍历Controller类

				Method[] methods = controllerClass.getDeclaredMethods();    //获取单个Class对象下的全部Method对象。

				if( ArrayUtil.isNotEmpty(methods) ){		//若是methods数组不为空

					for (Method method : methods) {		//遍历全部的method
						//判断当前方法是否包含Action注解
						if( method.isAnnotationPresent( Action.class ) ){

							Action action =	method.getAnnotation( Action.class );		//从Action中获取url映射规则

							String mapping = action.value();			//获取当前方法的@Action value的url地址和方法

							//验证URL映射规则
							if( mapping.matches("\\w+:/\\w*") ){		//regex
								String[] array = mapping.split(":");			//拆分
								if( ArrayUtil.isNotEmpty(array) && array.length == 2 ) {
									//获取请求路径和请求方法
									String requestMethod = array[0];
									String requestPath = array[1];

									//封装到Request对象中
									Request request = new Request(requestMethod, requestPath);

									//封装Handler对象
									Handler handler = new Handler(controllerClass, method);

									ACTION_MAP.put( request , handler );
								}
							}
						}
					}
				}
			}
		}
	}
	
	/**
	 * TODO
	 * 获取Handler
	 * @param requestMethod
	 * @param requestPath
	 * @return
	 */
	public static Handler getHandler(String requestMethod,String requestPath){
		Request request = new Request(requestMethod, requestPath);
		return ACTION_MAP.get(request);
	}
	
}

---------------------------------------summarize(总结):基于@Action注解的Request和Handler对象已经映射完毕了。解析来就能够初始化框架了---------------------------------------this

相关文章
相关标签/搜索