Spring MVC is the way of JAVA architecture. Master spring MVC easily.

1. Text

1. What is MVC architecture?
2. What is the spring MVC framework?
3. Why use spring MVC architecture?
4. Quick start spring MVC?
5. The process of spring MVC.
6. How spring MVC accepts the requested parameters.
7. How does spring MVC forward the parameters of the control layer to the front-end page?

2. What is MVC architecture?


3. What is spring MVC?

Spring MVC is a branch of spring framework. Spring MVC is an MVC framework similar to struts 2. In actual development, = = receives the browser's request response, processes the data, and then returns to the page for display = =, but it is much easier to get started than struts 2. Moreover, due to the security problems exposed by struts 2, spring MVC has become the preferred framework for most enterprises.

4. Why spring MVC?



5. Quick start springMVC

(1) Create a web project


(2) introduce the dependency of spring MVC.

<!--maven All dependencies related to this dependency can be introduced.-->

(3) Create a control layer.

public class HelloController {
    @RequestMapping("/hello01") //The method of mapping the response according to the request path.
    public String hello01(){
        return "/views/hello01.jsp";

(4) Configure the configuration file of spring MVC.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

    <!--1.Package scanning scans the package or parent package of our own control layer class-->
    <context:component-scan base-package="com.ykq.controller"/>

(5) Introduce front-end controller web xml

<!--register servlet-->
    <!--load springmvc Configuration file for-->

<!--servlet Mapping path
      /: Represents all control layer paths and static resources
      /*: Indicates that all are included jsp Webpage

(6) Testing.


6. Spring MVC process

*     1. Request from client http://localhost:8080/145springmvc01/hello01
 *     2. tomcat server from.
 *     3. Spring MVC's front-end controller, the DipatcherServlet, accepts all requests.
 *     4. Check which @ requestmapping matches your request address.
 *     5. Execute the corresponding method. Method returns a string. Spring MVC parses the string into the web page to be forwarded.
 *     6. The string is spliced through the view parser.
 *     7. Get the spliced address and find the corresponding web page.

7. How does spring MVC accept request parameters

7.1 acceptance of small number of parameters


7.2 accepted parameters

For example: register and add when submitting a form.

Solution: all request parameters can be encapsulated into an entity class object.


7.3 accept date type parameters



8. Processing static resources

What are static resources: for example: css, js, img,html




9. How to render the data of the control layer to the page

Thinking: originally, when talking about servlet s, we saved the data in which objects.

Request, valid within the same request. - --- > The El expression obtains the object data of the corresponding range.

Session: the same session is valid.

public class HelloController03 {

    public String index03(Model model){ //The model object can be understood as a request object. All data saved in this object has the same scope and is valid for the same request.
        User user=new User("Zhang San",1,"120",17,new Date());
        return "index03";//Spring MVC adopts the jump of request forwarding.

    public String index04(HttpServletRequest request){
        User user=new User("Zhang San 2",0,"120",18,new Date());
        return "index03";//Spring MVC adopts the jump of request forwarding.
    ///The functions of the above two methods are the same. They are saved in the request range.
    public String  index05(HttpSession session){
        User user=new User("Li Si",0,"130",19,new Date());
        return "index03";


10. Return json data

(1) It has its own transformation - introducing its dependency. jackson dependency

<!-- Jackson dependency can integrate spring MVC to convert java objects into json objects -- >

(2) The control layer directly returns the corresponding object type. (3) @ ResponseBody --- > Convert java objects into json objects.  



terms of settlement:



11. Define global exception handling

Once an exception occurs in the background, the web page will display ugly exception information. In order to show good-looking, we define a unified exception handling class.

Abnormal structure:

Throwable: the root class of the exception.

Error: error. This error cannot be solved by the programmer.

Exception: exception.

Compilation exception:

Runtime exception: RuntimeException: occurs only during runtime.

(1) Customize a global exception handling class

@ControllerAdvice //This class is an exception handling class.
public class MyHandler {

    //ExceptionHandler the method to execute when an exception occurs.
    @ExceptionHandler(value = ArithmeticException.class) //If value is not used, Throwable exceptions are handled by default.
    public String handlerException(){
        return "error";


Note: make sure spring MVC scans the exception handling class.


12. Interceptor

Review: filters: interceptors all resources [jsp,servlet,css,js,img]

Interceptor: only the control layer interface path of spring MVC will be intercepted.

technological process:



How to define interceptors:

(1) Customize an interceptor class.

public class MyInterceptor implements HandlerInterceptor {

    //Interceptor method. If the method returns true, it is released. If it returns false, it is intercepted.
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return true;

(2) Declare the custom interceptor in the spring MVC container--- springmvc.xml

 <!--Declaration interceptor-->
<!-- <mvc:interceptors>
         <mvc:mapping path="Interceptor path"/>
         <mvc:exclude-mapping path="Non intercepted path"/>
         <bean class="Full path of custom interceptor"/>

         <!--/*: Layer 1 path /hello
             /**: Multilayer path
         <mvc:mapping path="/**"/>
         <mvc:exclude-mapping path="/hello04/json01"/>
         <bean class="com.ykq.interceptor.MyInterceptor"/>

13. Spring MVC process


Step 1: the user sends a request to the front-end controller (dispatcher servlet).

Step 2: the front-end controller requests HandlerMapping to find the Handler, which can be found according to the xml configuration and annotation.

Step 3: processor mapper HandlerMapping returns the Handler to the front controller

Step 4: the front-end controller calls the processor adapter to execute the Handler

Step 5: the processor adapter executes the Handler

Step 6: after the Handler completes execution, return ModelAndView to the adapter

Step 7: the processor adapter returns ModelAndView to the front controller

ModelAndView is an underlying object of spring MVC framework, including Model and View

Step 8: the front-end controller requests the parser to parse the view

Resolve the real view according to the logical view name.

Step 9: try to return the view of the parser to the front controller

Step 10: the front controller renders the view

Is to fill the request field with the model data (in the ModelAndView object)

Step 11: the front-end controller responds the result to the user

Keywords: Java architecture mvc

Added by MikeTyler on Sat, 26 Feb 2022 09:27:00 +0200