1、配置springMVC,构建简单springMVC程序,需要先导入以下包,首先,需要导入springContext,springMVC也是基于spring。然后导入spring-webmvc,这是springMVC的核心依赖包,然后需要导入Servlet-api,这是因为springMVC需要配置一个dispathServlet,需要引入servlet依赖,需要使用前端资源,导入jsp依赖。
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<!-- springMvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<!-- servlet configuration-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<!--jsp-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
2、web.xml的配置,主要是配置dispatcherServlet,dispatcherServlet本质上是一个Servlet,需先配置Servlet,再配置Servlet-mapping。在Servlet配置中需配置springmvc.xml的位置。Servlet-mapping中需配置要拦截什么资源。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!--配置解决中问乱码问题的过滤器-->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置springMVC前端控制器 dispatcherServlet-->
<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:springmvc.xml</param-value>
</init-param>
<!-- 加载时机,启动时立即加载-->
<load-on-startup>1</load-on-startup>
</servlet>
<!--映射 dispatcherServlet-->
<servlet-mapping>
<!---->
<servlet-name>dispatcherServlet</servlet-name>
<!--拦截所有资源,不包括jsp-->
<url-pattern>/</url-pattern>
<!-- 拦截素有资源,包括Jsp -->
<!--<url-pattern>/*</url-pattern>-->
</servlet-mapping>
</web-app>
3、配置SpringMVC.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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置注解扫描包-->
<context:component-scan base-package="com.wang"></context:component-scan>
<!--开启springmvc注解 相当于配置了处理器映射器以及处理器适配器 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 视图解析器 -->
<bean name="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>