SpringMVC配置文件
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/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--扫描注解包--> <context:component-scan base-package="com.cn"/> <!--配置视图解析器--> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--设置视图名前缀--> <property name="prefix" value="/WEB-INF/view/" /> <!--设置视图名后缀--> <property name="suffix" value=".jsp" /> </bean> <!--配置自定义类型转换服务--> <!--开启类型转化服务--> <!--开启SpringMvc注解驱动--> <mvc:annotation-driven conversion-service="conversionService"/> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <!--注册自定义类型转换器(可多个)--> <property name="converters"> <list> <bean class="com.cn.converter.StringToDateConverter"/> </list> </property> </bean> <!--配置支持文件上传--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!--设置单个文件大小限制(byte)--> <property name="maxUploadSizePerFile" value="#{10*1024*1024}" /> <!--设置整个文件大小限制(byte)--> <property name="maxUploadSize" value="#{100*1024*1024}"/> </bean> <!--配置拦截器(可多个)--> <mvc:interceptors> <mvc:interceptor> <!--设置拦截器拦截的url地址--> <mvc:mapping path="/user/**"/> <!--设置拦截器不拦截的url地址--> <mvc:exclude-mapping path="/user/hello2.do"/> <!--设置自定义的拦截器--> <bean class="com.cn.interceptor.MyHandlerInterceptor"/> </mvc:interceptor> </mvc:interceptors> <!--配置允许访问静态资源(如:图片、css文件、js文件等)--> <mvc:default-servlet-handler /> <!--配置 CROS 跨域--> <mvc:cors> <mvc:mapping path="/**"/> </mvc:cors> </beans>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cn</groupId> <artifactId>springmvcdemo</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>springmvcdemo Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <!-- 添加springmvc依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.6</version> </dependency> <!-- 添加servlet 依赖 --> <dependency> <groupId>jakarta.servlet</groupId> <artifactId>jakarta.servlet-api</artifactId> <version>5.0.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.18</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> </dependency> <!-- JSTL 依赖包--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> <scope>runtime</scope> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <!--数据校验依赖包--> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>2.0.1.Final</version> </dependency> <dependency> <groupId>org.hibernate.validator</groupId> <artifactId>hibernate-validator</artifactId> <version>6.0.16.Final</version> </dependency> <!--文件上传依赖包--> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency> <!-- jackson 依赖包 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.11.4</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.11.4</version> </dependency> </dependencies> <build> <finalName>springmvcdemo</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>8</source> <target>8</target> </configuration> </plugin> </plugins> </build> </project>