Spring MVC framework construction - idea

01? 2022-02-13 07:30:11 阅读数:831

spring mvc framework construction idea

1、 establish maven engineering

 Wechat screenshot _20211214160925.png

 Wechat screenshot _20211214161032.png

2、 To configure pom.xml

  •  Wechat screenshot _20211214161450.png

Add dependency

 <groupId>org.example</groupId>
<artifactId>springMVC02</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>14</maven.compiler.source>
<maven.compiler.target>14</maven.compiler.target>
</properties>
<dependencies>
<!-- SpringMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.1</version>
</dependency>
<!-- journal -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<!-- ServletAPI -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- Spring5 and Thymeleaf Integrated package -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.12.RELEASE</version>
</dependency>
<!-- Handle json data jar package -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.1</version>
</dependency>
<!-- File upload dependency -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
Copy code 

Refresh pom.xml, Be sure to refresh

 Wechat screenshot _20211214161820.png

3、 establish webapp modular

 Wechat screenshot _20211214161939.png

Created webapp You need blue dots to be successful

 Wechat screenshot _20211214162107.png

4、 stay webapp Add below WEB-INF\web.xml

 Wechat screenshot _20211214162304.png

 Wechat screenshot _20211214162351.png

 Wechat screenshot _20211214162508.png

5、 To configure web.xml

 Wechat screenshot _20211214162629.png

<!-- Configure coding filter -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Configure how requests are processed put and delete filter -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Register front end controller -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:springMVC07 <!--springMVC Configuration file name -->
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Copy code 

6、 establish springMVC The configuration file , The documents here need to be the same as those above web.xml The configuration name at the registered front-end controller is consistent

 Wechat screenshot _20211214163331.png

 Wechat screenshot _20211214163424.png

7、 To configure springMVC07.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"
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 http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- Scan components -->
<context:component-scan base-package="controller"/>
<!-- Register view resolver -->
<bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<!-- set priority -->
<property name="order" value="1"/>
<property name="characterEncoding" value="UTF-8"/>
<property name="templateEngine">
<bean class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver">
<bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<!-- View prefix -->
<property name="prefix" value="/WEB-INF/templates/"/>
<!-- View suffix -->
<property name="suffix" value=".html"/>
<property name="templateMode" value="HTML5"/>
<property name="characterEncoding" value="UTF-8" />
</bean>
</property>
</bean>
</property>
</bean>
<!-- Open access to static resources -->
<mvc:default-servlet-handler/>
<!-- Turn on mvc Annotation drive -->
<mvc:annotation-driven/>
<!-- Profile upload parser , Convert the uploaded file to MiltipartFile object -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>
</beans>
Copy code 

Be careful , <context:component-scan base-package=""/>, The package name of the control class to be scanned when in double quotation marks , Need and java The next package name corresponds to

 Wechat screenshot _20211214163925.png

meanwhile , In view prefix value="/WEB-INF/templates/",templates by WEB-INF The corresponding package name

 Wechat screenshot _20211214164308.png

8、 To configure tomcat The server

 Wechat screenshot _20211214164451.png

 Wechat screenshot _20211214164511.png

 Wechat screenshot _20211214164537.png

 Wechat screenshot _20211214164552.png

 Wechat screenshot _20211214164651.png

such springMVC The whole frame of is finished


 

copyright:author[01?],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130730099889.html