SpringMvc - 导入的包以及配置文件
1.要导入的包
pom.xml
<!--版本控制--> <properties> <spring.version>5.3.13</spring.version> </properties> <dependencies> <!--spring mvc start--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!--spring mvc end--> <!--servlet-api start--> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-servlet-api</artifactId> <version>9.0.54</version> </dependency> <!--servlet-api end--> <!--thymeleaf start--> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf</artifactId> <version>3.0.11.RELEASE</version> </dependency> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring4</artifactId> <version>3.0.11.RELEASE</version> </dependency> <!--thymeleaf end--> <!--fastjson start--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.78</version> </dependency> <!--fastjson end--> </dependencies>
2.注册DispatcherServlet
web.xml
<?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.springmvc 前端控制器 --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--1.1初始化参数--> <init-param> <!--1.2加载配置文件--> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <!-- 当值为0或者大于0时,表示容器在应用启动时就加载并初始化这个servlet 当值小于0或者没有指定时,则表示容器在该servlet被选择时才会去加载。 正数的值越小,该servlet的优先级越高,应用启动时就越先加载。--> <load-on-startup>1</load-on-startup> </servlet> <!--2.注册dispatcherServlet--> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <!-- /:接收不到.jsp的请求 /*:能接收到.jsp的请求 --> <url-pattern>/</url-pattern> </servlet-mapping> <!--3.配置乱码过滤器(主要解决post请求乱码)--> <filter> <filter-name>encoding</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> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/</url-pattern> </filter-mapping> </web-app>