SpringMVC (first acquaintance)

1. What is SpringMVC?
Spring MVC is a follow-up product of SpringFrameWork and has been integrated into Spring Web Flow.
The web module provided by the Spring framework includes a full-featured MVC module for developing web applications. So when using Spring for WEB development, you can choose to use Spring's SpringMVC framework. Integrate other WEB MVC development frameworks, such as Struts (generally not used now), Struts2 (usually used by old projects), etc.
SpringMVC is the mvc development framework of the web layer, which belongs to a part of the WEB module of the Spring framework .
2. Develop a program based on SpringMVC
1. Create a project (web project)
Insert picture description here

2. Improve the project structure
3. Import dependencies

<!-- 配置开发SpringMVC所以来的jar包 -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>5.1.5.RELEASE</version>
</dependency>
<!-- 配置ServletAPI依赖 -->
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>3.0.1</version>
  <scope>provided</scope>
</dependency>
<!--配置JSP依赖包-->
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
  <groupId>javax.servlet.jsp</groupId>
  <artifactId>javax.servlet.jsp-api</artifactId>
  <version>2.2.1</version>
  <scope>provided</scope>
</dependency>
<!--配置JSTL的依赖-->
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>jstl</artifactId>
  <version>1.2</version>
</dependency>

4. Configure the central processing unit in the web.xml file [DispatcherServlet]
DispatcherServlet itself is a servlet, so configure the central processing unit to use servlet/servlet-mapping configuration in the web.xml file

<servlet>
  <servlet-name>dispatcherServle</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 </servlet>
<servlet-mapping>
  <servlet-name>dispatcherServle</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

Note: There is no * after /"/"
5. Create a custom controller

package com.wangxing.springmvc.controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
//控制器类【处理用户请求】
public class HelloController implements Controller {
    //用户请求处理方法
    @Override
    public ModelAndView handleRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws Exception {
        String info="hello,word";
        ModelAndView mav=new ModelAndView();
        mav.addObject("info",info);
        mav.setViewName("test.jsp");
        return mav;
    }
}

6. Create a springMVC configuration file in resources.

<?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">
    <!--配置URL解析器-->
    <!--org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping-->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
    <!--配置控制器适配器-->
    <!--org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter-->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
    <!--配置自定义的控制器-->
    <!--name:自定义的控制器的访问路径-->
    <!--class:自定义的控制器的包名+类名-->
    <bean name="/hello" class="com.wangxing.springmvc.controller.HelloController"></bean>
    <!--配置视图解析器-->
    <!--org.springframework.web.servlet.view.InternalResourceViewResolver-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix"  value=""></property>
    </bean>
</beans>

7. Configure and load the configuration file of springmvc in the web.xml file

<servlet>
  <servlet-name>dispatcherServle</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <!--配置加载springmvc的配置文件-->
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:springmvc.xml</param-value>
  </init-param>
</servlet>

8. Create jsp file

<%@page language="java" pageEncoding="UTF-8" isELIgnored="false" %>
<html>
<body>
<h2>${info}</h2>
</body>
</html>

9. Configure the server deployment program
10. Browser test http://localhost:8080/SpringMVCDemo1_war/hello

2. The execution process of SpringMVC
Insert picture description here

1. When the server is started, the central controller [DispatcherServlet] configured in the web.xml file is initialized, and the configured springMVC configuration file is loaded.
2. The client browser sends an http request.
3. The http request is intercepted by the central controller [DispatcherServlet] and forwarded to the url parser for analysis.
4. The Url parser parses the http request to get the specific request path.
5. The Url parser returns the specific request path obtained by the analysis to the central controller [DispatcherServlet]
6. The central controller [DispatcherServlet] transfers the specific request path obtained to the controller adapter.
7. The controller adapter searches for the corresponding request processing class [controller class] in the springmvc configuration file according to the specific request path.
8. The request processing class [controller class] executes specific request processing and obtains the ModelAndView object [1. data. 2. Jump address], hand the ModelAndView object to the controller adapter, and the controller adapter returns the ModelAndView object to the central controller [DispatcherServlet].
9. The central controller [DispatcherServlet] transfers the ModelAndView object to the view resolver for analysis.
10. The view parser parses the ModelAndView object, gets a specific data display path, and returns this specific data display path to the central controller [DispatcherServlet].
11. After the central controller [DispatcherServlet] gets the specific data display path, it converts and executes the resource represented by the path into an html data.
12. Return the converted html data to the client browser. ****

3. The access path configuration requested by
SpringMVC 3.1 The DispatcherServlet configuration in the web.xml file

1.1/
The name attribute value of the controller bean element of the corresponding spring configuration file "/xxxx"
corresponds to http://127.0.0.1:8080/ The name attribute value "xxxx.do" of the controller bean element of the spring configuration file corresponding to springmvc1/add
1.2 .do
corresponds
to http://127.0.0.1:8080/springmvc1/add.do
/ This is a wrong way
3.2 The name attribute value of the
controller in the springmvc configuration file The name attribute value of the controller should be set in
accordance with the configuration form of the DispatcherServlet configuration in the web.xml file
2.1 The DispatcherServlet configuration in the web.xml file /then the control in the springmvc configuration file The name attribute value of the controller should be "/xxxx"
http://127.0.0.1:8080/springmvc1/add
2.2*.do configured by DispatcherServlet in the web.xml file, then the name attribute value of the controller in the springmvc configuration file should be "/Xxxx.do"
http://127.0.0.1:8080/springmvc1/add.do
3.3 The return value of the request processing method handleRequest in the controller class for request processing ModelAndView
3.3.1 Create a common java class to implement the org.springframework.web.servlet.mvc.Controller
interface
3.3.2 Override the handleRequest method [Method for processing requests]
3.3.3 Configure in the springmvc configuration file For example: package com.wangxing.springmvc.controller; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; //Controller class [handling user requests] public class HelloController implements Controller { // User request processing method @Override public ModelAndView handleRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws Exception { String info="hello, Netstar software"; ModelAndView mav=new ModelAndView(); mav.addObject("info",info);














mav.setViewName("test.jsp");
return mav;
}
}

The return value of the request processing method handleRequest ModelAndView
1. ModelAndView -java class
2. The first part of the ModelAndView class is a Model [model] encapsulated data The
second part is A View [view] page element that displays data 3. The
ModelAndView class is used to encapsulate data and display data page elements.
4. Construct the method ModelAndView() to create an object of the ModelAndView class
ModelAndView mav=new ModelAndView();
5. The method of encapsulating data addObject(attributeName, attributeValue) is equivalent to setAttribute(key, value)
mav.addObject("info"," hello,
Netstar software"); 6. Set the name of the page element that displays the data setViewName(viewName)

      mav.setViewName("test.jsp");
          mav.setViewName("test");
 		  mav.setViewName("test.html");
mav.setViewName("控制器对应的请求处理路径");
mav.setViewName("test");---forword跳转[转发]
mav.setViewName("forward:test.jsp");
mav.setViewName("redirect:test.jsp");

----sendRedirect jump [redirect]
3.4 view parser
For example:

<!--配置视图解析器-->
<!--org.springframework.web.servlet.view.InternalResourceViewResolver-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀配置:[工程访问路径“http://localhost:8080/SpringMVCDemo1_war/”]-->
    <property name="prefix" value="/"></property>
<!--后缀配置-->
    <property name="suffix"  value=""></property>
</bean>

Using the view name obtained in the request processing class, a complete access path is obtained by combining the prefix and suffix of the view parser.
prefix+view name+suffix: complete element access path
For example:

ModelAndView  mav=new ModelAndView();
 		  mav.setViewName("test.jsp");
    
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="prefix" value="/"></property>
    		<property name="suffix"  value=""></property>
	  </bean>

The full path after parsing by the view resolver

http://localhost:8080/SpringMVCDemo1_war/test.jsp

E.g:

ModelAndView  mav=new ModelAndView();
 		  mav.setViewName("test");
    
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="prefix" value="/"></property>
    		<property name="suffix"  value=".html"></property>
	  </bean>

The full path after parsing by the view resolver
http://localhost:8080/SpringMVCDemo1_war/test.html

4. DispatcherServlet central processor settings to load the SpringMVC configuration file settings
1. When the springMVC configuration file is in the resources directory, the
resources root directory

 <!--配置加载springmvc的配置文件-->
    	<init-param>
      		<param-name>contextConfigLocation</param-name>
      		<param-value>classpath:xxxxxxxxxxxx.xml</param-value>
       </init-param>

Insert picture description here

In the subfolder [spring] of the resources directory

<!--配置加载springmvc的配置文件-->
    <init-param>
      	<param-name>contextConfigLocation</param-name>
      	<param-value>classpath:spring/xxxxxxxxxxxx.xml</param-value>
    </init-param>

Insert picture description here

2. When the springMVC configuration file is in the WEB-INF directory
2.1 The name of the Spring configuration file
[the value of the servlet-name element corresponding to the central controller-servlet.xml]
2.2 There is no need to appear in the servlet configuration element in the web.xml file

Insert picture description here

Guess you like

Origin blog.csdn.net/guoguo0717/article/details/110174236