Spring-MVC System Learning

1.Spring MVC Overview 1.1. Enterprise Application Basic Architecture The enterprise-level application infrastructure (C/S) and the role of SPRING in the architecture.

这里写图片描述 此架构图中spring要解决什么问题? 1)对象的构建,对象的依赖管理。(重点是从资源使用角度进行分析) 2)扩展业务的动态切入。(基于OCP更好实现功能扩展,同时进行解耦) 3)简化传统web mvc架构中的一些细节处理问题(参数获取,校验,值的注入,响应方式及数据格式的转换)。

问题: 1) Is there a decoupling that we generally say is not coupled? No, between all objects in the software system There will be coupling, just to reduce the coupling. 2) What is the main purpose of reducing coupling? It is to improve the maintainability of the system and facilitate the system to carry out better upgrades. 3) Reduce the main implementation of coupling? 3.1) Coupling between objects as much as possible coupled with interfaces and factories 3.2) Reuse of code Try to use combination instead of inheritance. (has a replace is a)

1.2.Web MVC architecture and analysis MVC architecture based on servlet, jsp, javabean technology 这里写图片描述

问题: 1) If we are not using spring mvc, how do you layer the system using mvc ideas? (Refer to jsp+servlet+javabean technology)

2) What are the advantages and disadvantages of the traditional web mvc programming architecture? Advantages: The structure level is clearer, more readable and maintainable. Disadvantages: parameter acquisition, type conversion, process calls are relatively complex

3) relatively mature mvc framework on the market: spring mvc, struts2, .... 1.3.Spring MVC architecture and analysis 这里写图片描述 Spring MVC是MVC架构模式的一种完美实现,它简化了Java WEB 中基于MVC架构的编程过程,是Spring中的WEB应用模块。其官方MVC概要架构图如下:

Spring MVC The underlying core architecture diagram and workflow (first understand, write the project case and then focus on strengthening) 这里写图片描述

Spring MVC core components:

1)DispatcherServlet (front controller, processing request entry) 2) HandlerMapping (mapper object, used to manage the mapping relationship between url and corresponding controller) 3) Controller (back-end controller, the control logic responsible for processing the request) 4) ModelAndView (model, encapsulate business processing results and views) 5) ViewResolver (view resolver, parsing the corresponding view relationship: prefix + view + suffix)

Remarks: If you want to understand the detailed processing flow of Spring MVC can be tracked based on breakpoint debugging. 2. Spring MVC programming foundation 2.1. Basic programming steps

Step01: Create a maven web project and solve the problem in the project Step02: Add project dependencies (spring-webmvc) and spring core configuration files Step03: Configure the front controller DispatcherServlet (web.xml) Step04: Create a backend controller (Controller) and page Step05: Configure the core application components in the spring configuration file. Step06: Deploy and test the springmvc application.

2.2. Based on xml programming implementation 2.2.1. Creating a Maven WEB project Web project packaging is war mode The target runtimes of the web project is tomcat The compiled version of the Web project is a problem that JDK1.8

may encounter: 1) The war project does not create web.xml by default (you need to generate it yourself) 2) Project creation needs to set the runtime environment tomcat (when multiple tomcat is selected) 3) Unified compilation version (the version is not uniform, there is a possibility that the project will not be compiled) 4) Unified coding (UTF-8) 5) If there is still a problem with the created project, you can clean the project

2.2.2. Add Spring MVC project dependencies and configuration files Open the project's pom.xml file, and then add dependencies (select group groupId for org.springframework)

org.springframework spring-webmvc 4.3.9.RELEASE

If you add dependencies, you will not find the corresponding jar dependencies, first check if the network is connected. If the network is through However, you have not downloaded the specific dependencies. In this case, you need to right-click the project and select maven/upate maven project/fore update... to perform the maven mandatory update operation.

Add the core configuration file applicationContext.xml in the project's resource directory. ,E.g: