For SpringMVC profile
- MVC:
model
view
controller
- SpringMVC is an MVC implemented with Spring. It is a part of Spring Framework and a lightweight Web Framework based on Java to implement MVC.
- SpringMVC features:
- Lightweight and easy to learn
- Efficient, response-based MVC framework
- Good compatibility with Spring, seamless integration
- Convention over Configuration
- Powerful functions: RESTful, data validation, formatting, localization, themes, etc
- Concise and flexible
Implementation principles of SpringMVC
Among them
- The DispatcherServlet represents the front controller and is the control center for the entire SpringMVC. The user sends a request, and the DispatcherServlet receives the request and intercepts it.
- HandlerMapping maps processors. DispatcherServlet call HandlerMapping HandlerMapping, according to the request url search Handler
- HandlerExecution represents a specific Handler whose primary purpose is to find the controller based on the URL and pass the parsed information to the DispatcherServlet
- A HandlerAdapter represents a processor adapter that executes handlers according to specific rules, and handlers let specific controllers execute.
- The DispatcherServlet calls the ViewResolver to resolve the logical view name passed by the HandlerAdapter.
SpringMVC getting started applet
- Create a Maven project and delete the SRC file as the parent project.
- Import dependence
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.10. RELEASE</version>
</dependency>
</dependencies>
Copy the code
- Create a new submodule and add Web support.
- Make sure the submodule has the dependencies it needs to import SpringMVC.
Configure web. XML
Note the configuration here!!
<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">
<! -- Register DispatcherServlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<! -- Associated with a SpringMVC configuration file -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<! -- Boot level -- 1-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--
/ 匹配所有请求 .jsp除外
/* 匹配所有请求 包括.jsp
-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Copy the code
- Configure the core configuration file for Spring, named
springmvc-servlet.xml
<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">
<! -- Processor mapper -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<! -- Processor adapter -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<! -- View resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
Copy the code
- Write operation business Controller.java
package controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloController implements Controller {
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
// Create model and view objects
ModelAndView mv = new ModelAndView();
// Encapsulate the object and put the model in mv
mv.addObject("message"."HelloSpringMVC!");
// Encapsulate the view and place it in mv
mv.setViewName("hello");
/ / return the mv
returnmv; }}Copy the code
- in
springmvc-servlet.xml
Register controller.java beans
<! Register controller bean -->
<bean id="/hello" class="controller.HelloController"/>
Copy the code
- Write the hello.jsp to jump to in web-INF
<%-- Created by IntelliJ idea. User: Created by Date:2021/3/27
Time: 16:42
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${message}
</body>
</html>
Copy the code
- Configure Tomcat and run the tests.
At this point, the SpringMVC program is complete, but there may be a 404 error, this is the problem of IDEA itself, you can import the jar package, it seems to have been imported, but it is not, in this case, you need to import the jar package again, as follows :(first open the project structure)
- The test results