1.springmvc is essentially the encapsulation of servlet s. When a user sends a request, the working principle of springmvc is as shown in the figure:
The process is as follows:
1. The user sends the request to the front-end controller dispatcher servlet.
2. The dispatcher servlet receives the request to call the HandlerMapping processor mapper.
3. The processor mapper finds the specific processor (which can be found according to the xml configuration and annotation), generates the processor object and the processor interceptor (if any) and returns them to the dispatcher servlet.
4. Dispatcher servlet calls HandlerAdapter processor adapter.
5. The HandlerAdapter calls the specific processor (Controller, also called back-end Controller) through adaptation.
6. The Controller returns to ModelAndView after execution.
7. The HandlerAdapter returns the controller execution result ModelAndView to the dispatcher servlet.
8. DispatcherServlet transfers the ModelAndView to the viewreslower view parser.
9. After the viewrelover is parsed, it returns to the specific View.
10. The dispatcher servlet renders the View according to the View (that is, the model data is filled into the View).
11. Dispatcher servlet responds to users.
2. Next, write a simple demo case of spring MVC according to fluency:
package com.xyf.mvc.servlet; import java.io.File; import java.io.IOException; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import com.xyf.mvc.annotation.Autowired; import com.xyf.mvc.annotation.RequestMapping; import com.xyf.mvc.annotation.RequestParam; import com.xyf.mvc.annotation.Service; import com.xyf.mvc.annotation.xyfController; import com.xyf.mvc.controller.testController; /** * Servlet implementation class dispatcherServlet */ public class dispatcherServlet extends HttpServlet { List<String> classNames = new ArrayList<String>(); Map<String, Object> map = new HashMap<String, Object>(); Map<String, Object> handerMap = new HashMap<String, Object>(); private String scan_packageName=""; private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public dispatcherServlet() { super(); // TODO Auto-generated constructor stub } public void init(ServletConfig config) { doScanXml(); doScan(scan_packageName);// Scan all classes below this package doInstance();// Create instance and save doAutowired();// Shoot in doMapping();// Find method from map } private void doScanXml() { //Get the package name to be scanned through xml file URL url=this.getClass().getClassLoader().getResource("config.xml"); File file =new File(url.getFile()); SAXReader reader=new SAXReader(); Document document = null; try { document = reader.read(file); } catch (DocumentException e) { e.printStackTrace(); } Element root=document.getRootElement(); List<?> list=root.elements(); for(Object object:list) { Element element = (Element)object; scan_packageName=element.getText(); } } private void doScan(String basePackage) { URL url = this.getClass().getClassLoader().getResource(("/" + basePackage.replaceAll("\\.", "/")));// Convert to file type String str = url.getFile(); File file = new File(str); String[] fileStr = file.list(); // Get all the. class files under the current package for (String path : fileStr) { File filePath = new File(str + path); // if (filePath.isDirectory()) { doScan(basePackage + "." + path); } else // Class java { classNames.add(basePackage + "." + filePath.getName()); } } } private void doInstance() { for (String className : classNames) { // Remove the class suffix String cn = className.replace(".class", ""); try { Class<?> clazz = Class.forName(cn); if (clazz.isAnnotationPresent(xyfController.class)) { Object instace = clazz.newInstance(); // map.put instace RequestMapping reqMap = clazz.getAnnotation(RequestMapping.class); String key = reqMap.value(); map.put(key, instace); // Put in map } else if (clazz.isAnnotationPresent(Service.class)) { Object instace = clazz.newInstance(); // map.put instace Service reqMap = clazz.getAnnotation(Service.class); // Get service as a key String key = reqMap.value(); map.put(key, instace); // Put in map } else { continue; } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } } private void doAutowired() { for (Map.Entry<String, Object> entry : map.entrySet()) { Object instance = entry.getValue();// Get the target Class<?> clazz = instance.getClass(); if (clazz.isAnnotationPresent(xyfController.class)) { Field[] fields = clazz.getDeclaredFields(); for (Field fd : fields) { if (fd.isAnnotationPresent(Autowired.class)) { // If there is autowired, inject Autowired auto = fd.getAnnotation(Autowired.class); String key = auto.value(); Object value = map.get(key); fd.setAccessible(true);// Open the private place try { fd.set(instance, value);// Shoot in } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } } } } } private void doMapping() { for (Map.Entry<String, Object> entry : map.entrySet()) { Object instance = entry.getValue();// Get the target Class<?> clazz = instance.getClass(); if (clazz.isAnnotationPresent(xyfController.class)) { RequestMapping reqMapping = clazz.getAnnotation(RequestMapping.class); String classPath = reqMapping.value(); // Method[] methods = clazz.getMethods(); for (Method method : methods) { System.out.println(method); if (method.isAnnotationPresent(RequestMapping.class)) { RequestMapping requestMapping = method.getAnnotation(RequestMapping.class); String methodPath = requestMapping.value(); handerMap.put(classPath + methodPath, method); } else { continue; } } } } } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Get request path String uri = request.getRequestURI(); // projectName String context = request.getContextPath(); String path = uri.replace(context, ""); // key Method method = (Method) handerMap.get(path); // method -- if (!path.equals("/")&&method!=null) { testController instance = (testController) map.get("/" + path.split("/")[1]); Object args[] = han(request, response, method); try { method.invoke(instance, args); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } private Object[] han(HttpServletRequest request, HttpServletResponse response, Method method) { Class<?>[] paramClazzs = method.getParameterTypes(); Object[] args = new Object[paramClazzs.length]; int args_i = 0; int index = 0; for (Class<?> paramClazz : paramClazzs) { if (ServletRequest.class.isAssignableFrom(paramClazz)) { args[args_i++] = request; } if (ServletResponse.class.isAssignableFrom(paramClazz)) { args[args_i++] = response; } Annotation[] paramAns = method.getParameterAnnotations()[index]; if (paramAns.length > 0) { for (Annotation paramAn : paramAns) { if (RequestParam.class.isAssignableFrom(paramAn.getClass())) { RequestParam rp = (RequestParam) paramAn; args[args_i++] = request.getParameter(rp.value()); } } } index++; } return args; } } © 2019 GitHub, Inc.
Full code address: https://github.com/RAOE/Iframe