基于MAVEN构建Spring MVC初始化配置
初始化一个Spring MVC配置的例子,会逐步完善。
1.pom.xml配置
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>com.alonsite</groupId> 5 <artifactId>springcloud</artifactId> 6 <version>0.0.1-SNAPSHOT</version> 7 <name>springcloud Maven Webapp</name> 8 <url>http://maven.apache.org</url> 9 <properties> 10 <commons-lang.version>2.6</commons-lang.version> 11 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 12 <spring.version>3.1.2.RELEASE</spring.version> 13 <!-- maven默认使用jdk1.5编译,此配置设置使用1.8 --> 14 <maven.compiler.source>1.8</maven.compiler.source> 15 <maven.compiler.target>1.8</maven.compiler.target> 16 </properties> 17 18 <dependencies> 19 <dependency> 20 <groupId>junit</groupId> 21 <artifactId>junit</artifactId> 22 <version>3.8.1</version> 23 <scope>test</scope> 24 </dependency> 25 26 <dependency> 27 <groupId>org.springframework</groupId> 28 <artifactId>spring-webmvc</artifactId> 29 <version>${spring.version}</version> 30 </dependency> 31 32 <dependency> 33 <groupId>org.springframework</groupId> 34 <artifactId>spring-jdbc</artifactId> 35 <version>${spring.version}</version> 36 </dependency> 37 38 <dependency> 39 <groupId>org.springframework</groupId> 40 <artifactId>spring-context</artifactId> 41 <version>${spring.version}</version> 42 </dependency> 43 44 <dependency> 45 <groupId>org.springframework</groupId> 46 <artifactId>spring-aop</artifactId> 47 <version>${spring.version}</version> 48 </dependency> 49 50 <dependency> 51 <groupId>org.springframework</groupId> 52 <artifactId>spring-core</artifactId> 53 <version>${spring.version}</version> 54 </dependency> 55 56 <dependency> 57 <groupId>org.springframework</groupId> 58 <artifactId>spring-test</artifactId> 59 <version>${spring.version}</version> 60 </dependency> 61 62 <dependency> 63 <groupId>javax.servlet</groupId> 64 <artifactId>servlet-api</artifactId> 65 <version>2.5</version> 66 </dependency> 67 68 <dependency> 69 <groupId>commons-fileupload</groupId> 70 <artifactId>commons-fileupload</artifactId> 71 <version>1.3.1</version> 72 </dependency> 73 74 <dependency> 75 <groupId>commons-lang</groupId> 76 <artifactId>commons-lang</artifactId> 77 <version>${commons-lang.version}</version> 78 </dependency> 79 80 <dependency> 81 <groupId>commons-io</groupId> 82 <artifactId>commons-io</artifactId> 83 <version>2.4</version> 84 </dependency> 85 </dependencies> 86 <build> 87 <finalName>springcloud</finalName> 88 <plugins> 89 <!-- jetty web容器插件 --> 90 <plugin> 91 <groupId>org.eclipse.jetty</groupId> 92 <artifactId>jetty-maven-plugin</artifactId> 93 <version>9.2.2.v20140723</version> 94 </plugin> 95 </plugins> 96 </build> 97 </project>
2.web.xml配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 5 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 6 <display-name>Archetype Created Web Application</display-name> 7 8 <servlet> 9 <!-- servlet名称 --> 10 <servlet-name>springcloud</servlet-name> 11 <!-- servlet对应的java类 --> 12 <servlet-class> 13 org.springframework.web.servlet.DispatcherServlet 14 </servlet-class> 15 <!-- servlet参数信息 --> 16 <init-param> 17 <!-- servlet配置文件路径 --> 18 <param-name>contextConfigLocation</param-name> 19 <param-value>/WEB-INF/springcloud-config.xml</param-value> 20 </init-param> 21 <!-- 在web应用启动时立即加载servlet --> 22 <load-on-startup>1</load-on-startup> 23 </servlet> 24 25 <!-- servlet映射声明 --> 26 <servlet-mapping> 27 <!-- 请求对应的servlet名称 --> 28 <servlet-name>springcloud</servlet-name> 29 <!-- 监听当前域的所有请求 --> 30 <url-pattern>/</url-pattern> 31 </servlet-mapping> 32 33 </web-app>
3.controller-config配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xsi:schemaLocation=" 6 http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 12 13 <context:annotation-config /> 14 15 <context:component-scan base-package="springcloud.controller"> 16 <context:include-filter type="annotation" 17 expression="org.springframework.stereotype.Controller" /> 18 </context:component-scan> 19 20 <bean 21 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 22 <property name="prefix" value="/WEB-INF/jsp/" /> 23 <property name="suffix" value=".jsp" /> 24 </bean> 25 26 <!--200*1024*1024即200M resolveLazily属性启用是为了推迟文件解析,以便捕获文件大小异常 --> 27 <bean id="multipartResolver" 28 class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 29 <property name="maxUploadSize" value="209715200" /> 30 <property name="defaultEncoding" value="UTF-8" /> 31 <property name="resolveLazily" value="true" /> 32 </bean> 33 34 </beans>