Spring MVC - the first spring MVC application
Our journey is the sea of stars, not the dust of the world
What is MVC
- MVC is the abbreviation of model, view and controller. It is a software design specification.
- It is a method to organize code by separating business logic, data and display.
- The main function of MVC is to reduce the two-way coupling between view and business logic.
- MVC is not a design pattern, MVC is an architecture pattern. Of course, there are differences between different MVCs.
**Model: * * data model, which provides data to be displayed, contains data and behavior. It can be considered as domain model or JavaBean component (including data and behavior), but now it is generally separated: Value Object (data Dao) and Service layer (behavior Service). That is, the model provides functions such as model data query and model data status update, including data and business.
**View: * * is responsible for displaying the model, which is generally the user interface we see and what customers want to see.
**Controller: * * receives the user's request and delegates it to the model for processing (state change). After processing, the returned model data is returned to the view, which is responsible for displaying. That is, the controller does the work of a dispatcher.
The most typical MVC is the pattern of JSP + servlet + javabean.
What is spring MVC
Spring MVC is a part of the Spring Framework and a lightweight Web framework based on Java to implement MVC.
Features of Spring MVC:
- Lightweight, easy to learn
- Efficient, request response based MVC framework
- Good compatibility with Spring and seamless combination
- Convention over configuration
- Powerful functions: RESTful, data validation, formatting, localization, theme, etc
- Concise and flexible
Spring's web framework is designed around the dispatcher Servlet.
The dispatcher servlet is used to distribute requests to different processors. Starting from Spring 2.5, users using Java 5 or above can develop based on annotations, which is very concise;
Because Spring MVC is good, simple, convenient and easy to learn, it is naturally seamlessly integrated with Spring (using Spring IOC and Aop), and the use convention is better than configuration Can carry out simple junit test Support Restful style Exception handling, localization, internationalization, data validation, type conversion, interceptors and so on... So we need to learn
The first spring program
-
Import dependency
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.9</version> </dependency>
-
Configure web XML, register DispatcherServlet
<?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 a springmvc Configuration file for:[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; (including.jsp)--> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
-
Write the configuration file of spring MVC! Name: SpringMVC servlet xml : [servletname]-servlet. xml
<?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 Give it to 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> <!--Handler--> <bean id="/hello" class="com.qifei.controller.HelloController"/> </beans>
-
Add process mapper
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
-
Add view parser
<!--view resolver :DispatcherServlet Give it to 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>
-
Write the business Controller that we want to operate, either implement the Controller interface or add annotations; You need to return a ModelAndView to load data and seal the view;
package com.qifei.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: let's import the Controller interface first public class HelloController implements Controller { public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { //ModelAndView models and views ModelAndView mv = new ModelAndView(); //Encapsulate the object and place it in ModelAndView. Model mv.addObject("msg","HelloSpringMVC!"); //Encapsulate the view to jump to and put it in ModelAndView mv.setViewName("hello"); //: /WEB-INF/jsp/hello.jsp return mv; } }
-
Give your class to the spring IOC container and register the bean
<!--Handler--> <bean id="/hello" class="com.qifei.controller.HelloController"/>
-
Write the jsp page to jump to, display the data stored in ModelandView, and our normal page;
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>qifei</title> </head> <body> ${msg} </body> </html>
-
Configure Tomcat startup test!
-
Possible problems: visit 404, troubleshooting steps:
- Check the console output to see if there is any missing jar package.
- If the jar package exists and the display cannot be output, add lib dependency in the project release of IDEA!
- Restart Tomcat to solve the problem!
Configure Tomcat startup test!
[External chain picture transfer...(img-lqIHbvwn-1628934820629)]
-
Possible problems: visit 404, troubleshooting steps:
- Check the console output to see if there is any missing jar package.
- If the jar package exists and the display cannot be output, add lib dependency in the project release of IDEA!
- Restart Tomcat to solve the problem!