??????????????????д???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??????