对于SpringMvc来说,搭建这框架,首先引入相应的包。如下:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.abc</groupId>
<artifactId>testSpringMvc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.2.0.RELEASE</spring.version>
<jackson.version>2.6.7</jackson.version>
</properties>
<dependencies>
<!-- 文件上传的依赖 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!-- Jackson Json处理工具包 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.4</version>
</dependency>
<!-- log4j end -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<skipTests>true</skipTests>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
<finalName>testSpringMvc</finalName> <!-- 这里是项目名 -->
</build>
</project>
以上包有点多,这里不做仔细介绍了。有兴趣的可以看看具体代表含义。
由于是web端项目,所以web.xml文件中也应该有相应的内容。具体如下:
<?xml version="1.0" encoding="UTF-8"?> <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" id="WebApp_ID" version="3.0"> <display-name>testSpringMvc</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <display-name>springMVC</display-name> <!-- 编码过滤器,以UTF8编码,解决POST乱码问题 --> <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> <servlet> <!--在web.xml(web程序的入口) 配置了DispatcherServlet,并且指定所有以.do结尾的请求全部交给DispatcherServlet来处理--> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!--配置/*会拦截所有请求,包括静态页面 --> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
接下来就是比较重要的配置文件,就是SpringMvc中的配置文件,命名规则按照{name}-servlet.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:p="http://www.springframework.org/schema/p" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置注解驱动,会默认的加载SpringMVC推荐使用 RequestMappingHandlerMapping, RequestMappingHandlerAdapter --> <mvc:annotation-driven /> <!-- 开启注解扫描 扫描控制器包,扫描包下面类头部的@Controller @RequestMapping--> <context:component-scan base-package="cn.cool.controller"/> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 视图前缀 --> <property name="prefix" value="/WEB-INF/views/"></property> <!-- 视图后缀 --> <property name="suffix" value=".jsp"></property> </bean> <!-- <mvc:interceptors> --> <!-- <mvc:interceptor> --> <!-- <mvc:mapping path="拦截的请求地址定义"/> --> <!-- <bean class="拦截器类,该类必须实现HandlerInterceptor接口"></bean> --> <!-- </mvc:interceptor> --> <!-- </mvc:interceptors> --> <!-- 定义文件上传解析器 --> <!-- <bean id="multipartResolver" --> <!-- class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> --> <!-- 设定默认编码 --> <!-- <property name="defaultEncoding" value="UTF-8"></property> --> <!-- 设定文件上传的最大值5MB,5*1024*1024 --> <!-- <property name="maxUploadSize" value="5242880"></property> --> <!-- </bean> --> <!-- 解决静态资源被拦截的问题 --> <mvc:default-servlet-handler/> </beans>
这些都是一些比较重要的配置文件,剩下的就是类,和页面。这里不做过多的解释。