首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Eclipse + Maven 构建 Spring MVC 完整图文教程

     編輯:Java 相關知識

从零搭建 Spring MVC 标准化 Maven 工程

本教程基于 Eclipse + Maven 3.2.5 + Spring 4.1.4 + Tomcat 7,手把手构建规范的 Spring MVC 项目骨架,覆盖目录结构、依赖管理、核心配置、控制器开发全流程。

环境准备

  • JDK 1.7+,配置 JAVA_HOME 与 Path
  • Eclipse Luna + m2e 插件
  • Maven 3.2.5,配置 settings.xml 本地仓库与阿里云镜像
  • Tomcat 7,Eclipse 集成 Server Runtime

步骤 1:创建 Maven Webapp 项目

  1. File → New → Maven Project → 选择 maven-archetype-webapp
  2. Group Id: com.springstudy,Artifact Id: study
  3. 补全标准目录:src/main/java、src/test/java、src/main/resources、src/test/resources(Build Path → Source → Add Folder)

步骤 2:完善 pom.xml 依赖管理

<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>

步骤 3:配置 web.xml 核心组件

<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>

步骤 4:Spring MVC 核心配置 (spring-servlet.xml)

<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>

步骤 5:开发首个 Controller 与 View

// 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>

步骤 6:部署运行验证

  1. Eclipse 添加 Tomcat 7 Server,配置 Server Locations → Use Tomcat installation,Deploy path: webapps
  2. Add and Remove → 添加 study 项目
  3. Start Server,访问 https://localhost:8080/study/hello

常见坑点避坑指南

问题原因解决
404 Not Foundurl-pattern 配置错误检查 web.xml / 而非 /*
静态资源 404DispatcherServlet 拦截配置 <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、配置

父工程管理统一依赖版本,子模块按职责拆分,实现高内聚低耦合。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved