Atitit mvc框架的实现 目录 1.1. class MvcCtrol { 1 1.2. MvcFilter 1 1.3. MvcUtil 2 /springboothelloword/sr
Atitit mvc框架的实现
目录
/springboothelloword/src/springboothtml/MvcUtil.java
@MyComponent
public class MvcCtrol {
// http://localhost:8080/mvctest
@MyMapping("/mvctest")
public String mvctestMeth(HttpServletRequest req) {
return "data";
}
}
@WebFilter(urlPatterns = "/*", filterName = "mvcFilter")
public class MvcFilter implements Filter {
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) {
try {
// get url out mapper
Map<String, MethodObj> url_method_maps = MvcUtil.get_url_out_mapper();
HttpServletRequest httpServletRequest = (HttpServletRequest) arg0;
HttpServletResponse httpServletResponse = (HttpServletResponse) arg1;
String uri = httpServletRequest.getRequestURI();
System.out.println(uri);
if(url_method_maps.get(uri)==null)
{
arg2.doFilter(arg0, arg1);
return;
}
MethodObj MethodObj1= url_method_maps.get(uri);
Object classObj = ConstructorUtils.invokeConstructor(MethodObj1.classProp, null);
Method meth = MethodObj1.methodProp;
Object[] args =MvcUtil.getArgs(meth,arg0,arg1); //new Object[] {};
String r = (String) meth.invoke(classObj, args);
outputHtml(httpServletResponse, r);
return; //not to next ,beirs springmvc show err,cause cant this mappering
package springboothtml;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
public class MvcUtil {
public static Object[] getArgs(Method meth, ServletRequest arg0, ServletResponse arg1) {
List li=Lists.newArrayList();
Parameter[] Parameters_arr= meth.getParameters();
for (Parameter parameter : Parameters_arr) {
Class<?> type1 = parameter.getType();
if ( type1 ==HttpServletRequest.class)
{
li.add(arg0);
}
}
return li.toArray();
}
public static Map<String, MethodObj> get_url_out_mapper() {
Map<String, MethodObj> url_method_maps = Maps.newLinkedHashMap();
ClassListForeach(new Consumer<Class<?>>() {
@Override
public void accept(Class<?> cls) {
MyComponent MyComponentAnno = (MyComponent) cls.getAnnotation(MyComponent.class);
if (MyComponentAnno == null)
return;
// 获取一个类的注解,根据注解名
Method[] methds = cls.getMethods();
for (Method method : methds) {
MyMapping anno1 = (MyMapping) method.getAnnotation(MyMapping.class);
if (anno1 == null)
continue;
String uri_mapping = anno1.value();
MethodObj MethodObj1 = new MethodObj();
MethodObj1.classProp = cls;
MethodObj1.methodProp = method;
url_method_maps.put(uri_mapping, MethodObj1);
}
}
});
return url_method_maps;
}
private static void ClassListForeach(Consumer<Class<?>> consumer) {
List<Class<?>> li_allclass = Lists.newArrayList();
li_allclass.add(MvcCtrol.class);
for (Class class1 : li_allclass) {
consumer.accept(class1);
}
}
}