??????????????????д???Java Web???????????д????飬????????????????????????????????????????????????MVC???д????????????????С??????????????????springMVC???????????????????????????????????????ЩMVC????????????????????д???MVC??????????????????????????????????????
????1.???????????????????MVC???
??????1?????????????????????????????????????????????????????
??????2???????????????????jar??????????servlet??????????????????????????????д?????????????????????
????@Controller
????public class Test {
????@MapURL(value = "/as.do")
????public String main(HttpServletRequest req??HttpServletResponse resp??ModelMap model??String name) throws IOException?? ServletException {
????System.out.println("as1");
????model.put("time"??new Date(System.currentTimeMillis()));
????return "test";
????}
????}
??????3?????????????????????????????????????????д??????·????????????????????
??????4????????springMVC?????????????????????д?????????÷????????????
????????3????????????????????ò?????????????????????????????????ò??????????????????????????????????????????????????????????????????????????????????????
????2.??annotation???
????annotation???jdk1.5?????????????annotation????????????????????????????????????????????????????????????????????????????????????????@Controller????????????????????????????????????????????????????
????????????????????????????????@interface?????????????????????????????????????????@Controller??
????@Target(ElementType.TYPE)
????@Retention(RetentionPolicy.RUNTIME)
????public @interface Controller {}
????@Target(ElementType.METHOD)
????@Retention(RetentionPolicy.RUNTIME)
????public @interface MapURL {
????String value();
????String method() default "GET";
????}
????????????????????new??????class??????????Class.forName??????class.isAnnotationPresent(Controller.class)?????????????????ж???????????????Controller??????????????????????@MapURL?????????2??????value??method?????????url??http??????????????get??????????????????á???????????У?Controller??????????к???????????????????????Щ???????????ó?????????????У?????????class????????ó????е?????????????url???key?????????value?浽map?У?http???????δ????????????????????map??????????????
???????ó???????????У??????????config.ini??????????????????????????????????????????е?class?????????????????????????????Controller????
public static void  scanClassSetByPackage(String packageName)
{
methodMap=new HashMap<String?? MethodPro>();
classMap=new HashMap<String?? Class<?>>();
classSet=new HashSet<Class<?>>();
String filePath=Config.getProPath()+ StringUtils.modifyPackagePath(packageName);
FileUtils.getClassSet(filePath??classSet??packageName);
for(Class<?> clazz:classSet)
{
if(clazz.isAnnotationPresent(Controller.class))
{
Method[] methods=clazz.getDeclaredMethods();
for(Method method:methods)
{
if(method.isAnnotationPresent(MapURL.class))
{
MapURL mapURL=method.getAnnotation(MapURL.class);
MethodPro mp=new MethodPro(method??mapURL.value()??mapURL.method());
methodMap.put(mapURL.value()??mp);
classMap.put(mapURL.value()??clazz);
}
}
}
}
}
????3.???????servlet
????????????????servlet??????????????????????????????????????????????????servlet???Щ????????????????????????????????????????????????????servlet?????????????servlet??????????????????????????????Щ??????@Controller?е???????@MapURL????????????????????????????map??map???????????????????????????????????????????servlet??init?????г???????ɡ???servlet??service?У??????request??getPathInfo()??????getServletPath()???????????????·???????map??????·??url?????method????????request??response??????????????κ?@Controller???κη????????????MVC????????????????????????ü??????????servlet?????????????????????????????д????servlet????????@Controller?м???????????????????????????????дservlet?????????????д??????servlet?????????????????д?????jsp????滹??request.getDispatcher("test.jsp").forward(request.response)????????????????????request??????????????????request?С?
????4.??????springMVC?????????
???????????springMVC???????????????????????????????????????????????????????????????????form????????????????????????????????????д??????????????????е????????????ü???springMVC???????????????????????????????????????????????顣
????public Object invoke(Object obj?? Object... args)????method?????????в???????????????????????????????????????????ú?????????invoke(obj??req??resp??model??name);???????漸??????????invoke(obj??obj[]);?????@Controller????????????????????????????????method??????????????????????????????????????鷯???á????????2??????
??????1????????method ???????????????????????????滻?????????????????????????????????????method??????????????method???????????????????????????method.getParameterTypes()????method?и????????????????ò?????????????????????????????????????????????asm???????й?????????о????????????????Demo?????????ú????в????????????asm??????????У??????method??2??classNames??paraNames???????????????????????
List<String> paraNames= MethodResolver.getMethodNames(clazz.getName()??methodPro.getName());
List<String> classNames= CollectionUtils.classArrToStringList(method.getParameterTypes());
Object[] args=MethodResolver.makeArgs(paraNames??classNames??req??resp??model);
public static List<String> getMethodNames(String className??String methodName) throws IOException {
List<String> list=new ArrayList<String>();
String cn=Config.getProPath()+className.replace("."?? "/")+".class";
InputStream is=new FileInputStream(new File(cn));
ClassReader cr = new ClassReader(is);
ReadMethodArgNameClassVisitor classVisitor = new ReadMethodArgNameClassVisitor();
cr.accept(classVisitor?? 0);
for(Entry<String?? List<String>> entry : classVisitor.nameArgMap.entrySet()) {
if(entry.getKey().equals(methodName)) {
for (String s : entry.getValue()) {
list.add(s);
}
}
}
return list;
}
??????2??????????????method???????????е?request??response???????Object[] args??????????á?????????????????????С?????classNames??paraNames?????????? ????????[??????]??????????????javax.servlet.http.HttpServletRequest????javax.servlet.http.HttpServletResponse????ModelMap????????args?У?????????ж??????????????String?????????request??ParameterNames??????????????????args?????????????ж????????request??ParameterNames???????????????е????????????String????????????????????????????????????????????????ж??????????????????????д?????????????????????????????????????÷???????????????????е????????????request??ParameterNames????????????????????????????args?????????????????????????args????null???????????е?classNames??paraNames????method???????????????顣
????5.???·??????
??????@Controller??????л????????????????????????????????????·??????????????????????????
????Object result=method.invoke(clazz.newInstance()??args);
????Map<String??Object> map=model.getMap();
????for(String key:map.keySet())
????{
????req.setAttribute(key??map.get(key));
????}
????if(result instanceof String)
????{
????req.getRequestDispatcher(Config.getConfig("pagePath")+File.separator+result.toString()+Config.getConfig("suffix")).forward(req?? resp);
????}
????6.????
??????????????????IDE??????????jar?????????? mvn install??????????????????web?????????jar????servlet???????д????????*.do??????????????web.xml???????????????????????????????á?
????<%@ page contentType="text/html;charset=UTF-8" language="java" %>
????<html>
????<head>
????<title>11</title>
????</head>
????<body>
??????????:${time}
????</body>
????</html>
??????????????????????δ???????jsp????????tomcat????Ч???

??????
???????????????д??3??4????????annotation??asm???????????????????????????MVC??????????????????????