本教程基于 Eclipse + Maven 3.2.5 + Spring 4.1.4 + Tomcat 7,手把手构建规范的 Spring MVC 项目骨架,覆盖目录结构、依赖管理、核心配置、控制器开发全流程。
maven-archetype-webappcom.springstudy,Artifact Id: studysrc/main/java、src/test/java、src/main/resources、src/test/resources(Build Path → Source → Add Folder)<properties>
<spring.version>4.1.4.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Spring MVC 核心 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>study</finalName>
<plugins>
<!-- 编译版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- Tomcat 部署插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/study</path>
<port>8080</port>
</configuration>
</plugin>
</plugins>
</build>
<web-app version="2.5" xmlns="https://java.sun.com/xml/ns/javaee">
<!-- Spring 容器启动 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:configs/spring-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring MVC 前端控制器 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:configs/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:context="https://www.springframework.org/schema/context"
xmlns:mvc="https://www.springframework.org/schema/mvc">
<!-- 组件扫描 -->
<context:component-scan base-package="com.springstudy.controller" />
<!-- 注解驱动 -->
<mvc:annotation-driven />
<!-- 静态资源映射 -->
<mvc:resources mapping="/styles/**" location="/styles/" />
<mvc:resources mapping="/scripts/**" location="/scripts/" />
<mvc:resources mapping="/images/**" location="/images/" />
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
// HelloController.java
@Controller
public class HelloController {
@RequestMapping("/hello")
public ModelAndView hello() {
ModelAndView mv = new ModelAndView();
mv.addObject("spring", "Spring MVC");
mv.setViewName("hello"); // 对应 /WEB-INF/views/hello.jsp
return mv;
}
}
<!-- hello.jsp -->
<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>Spring MVC Demo</title></head>
<body>Hello ${spring}!</body></html>
Server Locations → Use Tomcat installation,Deploy path: webappshttps://localhost:8080/study/hello| 问题 | 原因 | 解决 |
|---|---|---|
| 404 Not Found | url-pattern 配置错误 | 检查 web.xml / 而非 /* |
| 静态资源 404 | DispatcherServlet 拦截 | 配置 <mvc:resources/> 或 <mvc:default-servlet-handler/> |
| 中文乱码 | 编码未统一 | web.xml 配置 CharacterEncodingFilter,JSP pageEncoding="UTF-8" |
| 依赖冲突 | 传递依赖版本不一致 | Maven Dependency Hierarchy 分析,<exclusion> 排除 |
parent-project (pom) ├── common-module (jar) // 工具类、实体、常量 ├── dao-module (jar) // MyBatis Mapper、Repository ├── service-module (jar) // 业务逻辑、事务 └── web-module (war) // Controller、View、配置
父工程管理统一依赖版本,子模块按职责拆分,实现高内聚低耦合。