mvc框架图书馆管理系统jdbc mvc架构图用什么画( 三 )


/*** 我们需要一个Set,将所有能够响应的Controller存起来*/private Set<Class<?>> classSet = new HashSet<>();写工具性方法/*** 扫描包,获取所有带MyController的类* @param packageName*/private void scanPackage(String packageName){Reflections reflections = new Reflections(packageName);classSet = reflections.getTypesAnnotatedWith(MyController.class);}在init()中调用
@Overridepublic void init(ServletConfig config) throws ServletException {//1. 加载配置文件//这里我们在web.xml中配置的初始化参数contextConfigLocation就起到效果了String initParameter = config.getInitParameter("contextConfigLocation");try {loadConfigfile(initParameter);} catch (IOException e) {e.printStackTrace();}//2. 扫描controller包,存储所有能够响应的ControllerscanPackage(properties.getProperty("package"));}3.初始化controller定义所需属性
/*** 类spring-mvc容器,存储Controller对象*/private Map<String,Object> mySpringMVCContext = new HashMap<>();写工具性方法/*** 初始化Controller*/private void initController() throws IllegalAccessException, InstantiationException {if(classSet.isEmpty()){return;}for (Class<?> controller : classSet) {mySpringMVCContext.put(firstWordToLowCase(controller.getSimpleName()),controller.newInstance());}}/*** 首字母转小写* @param string* @return 首字母为小写的String*/private String firstWordToLowCase(String string){char[] chars = string.toCharArray();//将大写转成小写chars[0]+=32;return String.valueOf(chars);}在init()中调用@Overridepublic void init(ServletConfig config) throws ServletException {//1. 加载配置文件String initParameter = config.getInitParameter("contextConfigLocation");try {loadConfigfile(initParameter);} catch (IOException e) {e.printStackTrace();}//2. 扫描controller包,存储所有能够响应的ControllerscanPackage(properties.getProperty("package"));//3. 初始化controllertry {initController();} catch (IllegalAccessException e) {e.printStackTrace();} catch (InstantiationException e) {e.printStackTrace();}}4.初始化Handler映射器(Handler = controller + method)
定义所需属性
/*** 存储所有方法的Map<url:method>*/private Map<String,Method> methodMap = new HashMap<>();/*** 存储所有Controller的Map*/private Map<String,Object> controllerMap = new HashMap<>();
具体实现方法
private void initHandlerMapping() {if (mySpringMVCContext.isEmpty()){return;}for (Map.Entry<String, Object> entry : mySpringMVCContext.entrySet()) {Class<?> entryClass = entry.getValue().getClass();if (!entryClass.isAnnotationPresent(MyController.class)){continue;}//Controller类上的requestMapping值,如果有则获取String baseUrl = "";if (entryClass.isAnnotationPresent(MyRequestMapping.class)){MyRequestMapping annotation = entryClass.getAnnotation(MyRequestMapping.class);baseUrl = annotation.value();}//获取所有方法Method[] methods = entryClass.getMethods();for (Method method : methods) {if (method.isAnnotationPresent(MyRequestMapping.class)){MyRequestMapping annotation = method.getAnnotation(MyRequestMapping.class);String url = annotation.value();url = baseUrl + url;//将该方法放入方法集methodMap.put(url,method);//将该controller方法处理器集controllerMap.put(url,entry.getValue());//至此,初始化完成,后端整装待发}}}}在init()中调用
@Overridepublic void init(ServletConfig config) throws ServletException {//1. 加载配置文件String initParameter = config.getInitParameter("contextConfigLocation");try {loadConfigfile(initParameter);} catch (IOException e) {e.printStackTrace();}//2. 扫描controller包,存储所有能够响应的ControllerscanPackage(properties.getProperty("package"));//3. 初始化controllertry {initController();} catch (IllegalAccessException e) {e.printStackTrace();} catch (InstantiationException e) {e.printStackTrace();}//4. 初始化Handler映射器initHandlerMapping();}好了至此,我们的领导MyDispatcherServlet 的初始化部分就写完了,现在他已经对自己的团队成员:众多业务员们(Controller)已经了如指掌了(有同学可能会问:陈老师,你还没定义Controller呢!这个先不急)下面,我们就重写他的service()方法,让他能够到外面接活


以上关于本文的内容,仅作参考!温馨提示:如遇健康、疾病相关的问题,请您及时就医或请专业人士给予相关指导!

「四川龙网」www.sichuanlong.com小编还为您精选了以下内容,希望对您有所帮助: