Introduction to Spring MVC

Keywords: Java Spring xml JSP

This article belongs to the introduction of Spring MVC. It belongs to the basic knowledge. It is only for learning and sharing. If there are any shortcomings, please point out.

What is Spring MVC?

Spring MVC is a Spring-based MVC framework, which inherits the excellent features of Spring and is currently the most frequently used Java EE Web MVC framework.

What are the advantages of Spring MVC?

  1. Spring MVC runs faster than Struts (Spring MVC is singleton mode, threads are not safe, so don't use member variables, Struts is multithreaded, threads are safe)
  2. Spring MVC is simpler, more efficient and easy to read.

Spring MVC Architecture

As shown in the following figure:

 

 

Spring MVC Environment Construction

1. Create a New Web Project [Dynamic Web Project]

File - > New - > Dynamic Web Project, just follow the steps. As shown in the following figure:

2. The Jar (Java Archive) package needed to import Spring MVC

There are eight Jar packages that need to be imported, which are divided into three parts, as follows:

//Log Pack
commons-logging-1.1.1.jar
//spring Core package
spring-aop-4.0.6.RELEASE.jar
spring-beans-4.0.6.RELEASE.jar
spring-context-4.0.6.RELEASE.jar
spring-core-4.0.6.RELEASE.jar
spring-expression-4.0.6.RELEASE.jar
//WebMVC package
spring-web-4.0.6.RELEASE.jar
spring-webmvc-4.0.6.RELEASE.jar

3. Configuring web.xml

Mainly configure the core controller, as shown in the following figure:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 3   <!-- SpringMVC Is used Servlet To be a controller, default is/WEB-INF/[servlet-name]-servlet.xml This path, such as: dispatcher-servlet.xml -->
 4   <!-- Why is it wrong when it's used? Tomcat Do not report errors at startup?
 5       Sturts As long as the configuration is misaligned, it will be misaligned at startup. SpringMVC It was created on the first visit. Servlet
 6    -->
 7   <servlet>
 8       <servlet-name>dispatcher</servlet-name>
 9       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
10       <!-- The following statement is configured in tomcat Start the core controller at startup -->
11       <load-on-startup>1</load-on-startup>
12       <!-- /WEB-INF/dispater-servlet.xml -->
13   </servlet>
14   <servlet-mapping>
15       <servlet-name>dispatcher</servlet-name>
16       <!-- Request Entry -->
17       <url-pattern>/</url-pattern>
18   </servlet-mapping>
19   
20   <display-name>FirstSpringMvc</display-name>
21   <welcome-file-list>
22     <welcome-file>index.html</welcome-file>
23     <welcome-file>index.htm</welcome-file>
24     <welcome-file>index.jsp</welcome-file>
25     <welcome-file>default.html</welcome-file>
26     <welcome-file>default.htm</welcome-file>
27     <welcome-file>default.jsp</welcome-file>
28   </welcome-file-list>
29 </web-app>

4. Create a new class to implement the Cotroller interface

A Controller has only one handleRequest method to implement to handle page requests

 1 package com.hex.springmvc;
 2 
 3 import javax.servlet.http.HttpServletRequest;
 4 import javax.servlet.http.HttpServletResponse;
 5 
 6 import org.springframework.web.servlet.ModelAndView;
 7 import org.springframework.web.servlet.mvc.Controller;
 8 
 9 /**
10  * Implementing Controller Interface
11  * @author Administrator
12  *
13  */
14 public class HelloWorldController implements Controller {
15 
16     @Override
17     public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
18         ModelAndView mav=new ModelAndView();
19         mav.addObject("msg", "Hello World,Hello, SpringMVC");
20         mav.setViewName("WEB-INF/jsp/HelloWorld.jsp");
21         System.out.println("The controller is executed!!!");
22         return mav;
23     }
24 
25 }

5. Added Controller Profile

Spring MVC has a default configuration file, which defaults to the path of / WEB-INF/[servlet-name]-servlet.xml, such as dispatcher-servlet.xml.

A bean represents a Controller and is uniquely represented by an id attribute. As follows:

1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://www.springframework.org/schema/beans
5  http://www.springframework.org/schema/beans/spring-beans.xsd">
6     <bean id="/HelloWorld" class="com.hex.springmvc.HelloWorldController"></bean>
7 </beans>

6. Added JSP (Java Server Pages) page

The ${msg} denotes the parameter content passed by the receiving Controller, as follows:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>SpringMVC Welcome</title>
 8 </head>
 9 <body>
10     <h1>Welcome Information</h1>
11     <h1>${msg}</h1>
12 </body>
13 </html>

7. Visits

Start Tomact, open the browser, enter the address http://localhost:8080/FirstSpring Mvc/HelloWorld, as shown below, to indicate success

8. File structure

The entire file structure is shown in the following figure:

Spring MVC support for static resources

When configuring the core controller, <url-pattern>/</url-pattern> will result in inaccessible static resources (css,js,img) in Spring MVC framework, which requires configuring support for static resources, as follows:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4  xmlns:mvc="http://www.springframework.org/schema/mvc"
 5 xsi:schemaLocation="http://www.springframework.org/schema/beans
 6  http://www.springframework.org/schema/beans/spring-beans.xsd
 7  http://www.springframework.org/schema/mvc
 8  http://www.springframework.org/schema/mvc/spring-mvc.xsd">
 9      <!-- Support for static resources -->
10      <mvc:default-servlet-handler/>
11     <bean id="/HelloWorld" class="com.hex.springmvc.HelloWorldController"></bean>
12 </beans>

After configuration, the picture can be accessed as follows:

Remarks

Like the fresh wind in the mountains, like the warm light of the ancient city

Posted by The_Anomaly on Sun, 11 Aug 2019 08:50:36 -0700