2021-11 (java-springmvc Learning Notes 1)

Keywords: Java server

1. Advantages of springmvc

  1. Light weight, easy to learn
  2. Efficient
  3. Compatible with spring
  4. Contract greater than configuration
  5. Powerful: restful, data validation, formatting, localization, theme
  6. Simple and flexible

2. springmvc Execution Principle

  1. The Dispatcher Servlet represents the front controller and is the control center for the entire springmvc. The user makes requests, and the Dispatcher Servlet accepts and intercepts them. Let's say: http://localhost:8080/Springmvc/hello
    1. http://localhost:8080 Server Domain Name
    2. springmvc server site
    3. hello representation controller
  2. HandlerMapping is a processor mapping. Dispatcher Servlet Call
  3. HandlerExecution represents a specific Handler
  4. HandlerExecution passes parsed information to Dispatcher Servlet
  5. Handler Adapter represents a processor adapter that executes the handler according to specific rules
  6. Handler lets the specific Controller execute
  7. Controller returns specific execution information to HandlerAdapter
  8. HandlerAdapter returns the view to Dispatcher Servlet
  9. Dispatcher Servlet calls ViewResolver to resolve views passed by Handler Adapter
  10. View parser passes parsed logical view name to Dispatcher Servlet
  11. Dispatcher Servlet calls specific views
  12. Final Rendering

3. First Procedure

1. Create a new Moudle and add web support

2. Determine dependencies on imported springmvc

3. Configure the web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <!--1.register DispatcherServlet-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--Associate one springmvc Profile:[servlet-name]-servlet.xml-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
        <!--Startup Level-1-->
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!--/ Match all requests; (excluding.jsp)-->
    <!--/* Match all requests; (Includes.jsp)-->
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

4. Create springmvc-servlet.xml resource file

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

    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

    <!--view resolver:DispatcherServlet For him ModelAndView-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
        <!--prefix-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--Suffix-->
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

5. Create classes, write the business we want to operate on, implement the Controller interface, or add annotations, and return an m

package com.kun.controller;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//Note: Here we import the Controller interface first
public class HelloController implements Controller {

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //ModelAndView Model and View
        ModelAndView mv = new ModelAndView();
        //Encapsulate the object and place it in Model AndView.
        mv.addObject("msg","HelloSpringMVC!");
        //Encapsulate the view you want to jump to and place it in ModelAndView
        mv.setViewName("hello"); //: /WEB-INF/jsp/hello.jsp
        return mv;
    }

}

6. Injecting bean s

    <!--Handler-->
    <bean id="/hello" class="com.kun.controller.HelloController"/>

7. Create a JSP package, create a new hello.jsp file

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    ${msg}
</body>
</html>

Potential problems: 404

  1. Check the console output to see if any jar packages are missing
  2. If a jar package exists, check to see if there is a lib dependency in the IDea project publication

4. Annotation Realization

1. Create a new moudle and add web

2. Due to possible resource filtering problems in Maven

<build>
   <resources>
       <resource>
           <directory>src/main/java</directory>
           <includes>
               <include>**/*.properties</include>
               <include>**/*.xml</include>
           </includes>
           <filtering>false</filtering>
       </resource>
       <resource>
           <directory>src/main/resources</directory>
           <includes>
               <include>**/*.properties</include>
               <include>**/*.xml</include>
           </includes>
           <filtering>false</filtering>
       </resource>
   </resources>
</build>

3. Import Dependency

4. Configure web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
        version="4.0">

   <!--1.register servlet-->
   <servlet>
       <servlet-name>SpringMVC</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
       <!--Specify by initialization parameters SpringMVC Configuration file location, associated-->
       <init-param>
           <param-name>contextConfigLocation</param-name>
           <param-value>classpath:springmvc-servlet.xml</param-value>
       </init-param>
       <!-- Start sequence, the smaller the number, the earlier to start -->
       <load-on-startup>1</load-on-startup>
   </servlet>

   <!--All requests will be springmvc intercept -->
   <servlet-mapping>
       <servlet-name>SpringMVC</servlet-name>
       <url-pattern>/</url-pattern>
   </servlet-mapping>

</web-app>

Note: The difference between / and /*

  • /will not match.jsp, only for requests we write
  • /* will match*.jsp, a JSP attempt will occur to enter spring again, resulting in no controllersuoyihui report 404 found

Note the web version issue

Register dispatcher Servlet

Configuration file associated with springmvc

Start Level 1

Mapping path is/

5. Add the springmvc-servlet.xml configuration file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--    Auto Scan Package-->
    <context:component-scan base-package="com.kun.controller"/>
<!--    Give Way mvc Do not process static resources-->
    <mvc:default-servlet-handler/>
    <mvc:annotation-driven/>

    <!--view resolver:DispatcherServlet For him ModelAndView-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
        <!--prefix-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--Suffix-->
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

6. Create Control Class

@Controller
@RequestMapping("/hello")
public class HelloController {
    @RequestMapping("/h1")
    public String hello(Model model){
//        Encapsulate data
        model.addAttribute("msg","hellosSpringMvcController");
//        Return data
        return "hello";
    }
}

7. Create jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    ${msg}
</body>
</html>

Conclusion:

  1. Create a new web package
  2. Import related jar packages
  3. Write web.xml, register Dispatcher Servlet
  4. Write a springmvc configuration file
  5. Create Control Class
  6. Create jsp

Three main components: processor mapper, processor adapter, view parser

Posted by adamb10 on Tue, 16 Nov 2021 09:15:12 -0800