编程知识 cdmana.com

Basic configuration of spring MVC

One ,

1, Create a project

2, To configure maven

3, stay pom.xml Add dependency to change configuration

stay <properties> Add in label <spring.version>5.0.5.RELEASE</spring.version>

stay <dependencies> Add the following dependencies to the tag

  1. <dependency>
  2. <groupId>org.springframework</groupId>
  3. <artifactId>spring-context</artifactId>
  4. <version>${spring.version}</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework</groupId>
  8. <artifactId>spring-web</artifactId>
  9. <version>${spring.version}</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.springframework</groupId>
  13. <artifactId>spring-webmvc</artifactId>
  14. <version>${spring.version}</version>
  15. </dependency>
  16. <!--J2EE-->
  17. <dependency>
  18. <groupId>javax.servlet</groupId>
  19. <artifactId>javax.servlet-api</artifactId>
  20. <version>3.0.1</version>
  21. </dependency>
  22. <dependency>
  23. <groupId>javax.servlet</groupId>
  24. <artifactId>jsp-api</artifactId>
  25. <version>2.0</version>
  26. </dependency>

4, When creating a project, there is no previous Java and resource Files need to be added manually , And mark it , Marked as Java Files and resource files

 

5, To configure Tomcat

 

6, stay resource Resource file spring.xml And configuration

<?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
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
</beans>

7, modify web.xml

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<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"
         version="3.0">

  <display-name>Archetype Created Web Application</display-name>

  <servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <!-- Load the profile -->
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springMVC.xml</param-value>
    </init-param>
    <!-- servlet After the container starts , Load the servlet -->
    <load-on-startup>1</load-on-startup>
    <!-- servlet3 New characteristics ,servlet Asynchronous Support , Lifting performance  -->
    <async-supported>true</async-supported>
  </servlet>

  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <!--  Coding filter ,UTF8 code  -->
  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

8, Click on the run

Pop up this window in the browser , It is running successfully

版权声明
本文为[Yao Qing]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/01/20210131190248313p.html

Scroll to Top