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

IntelliJ IDEA 搭建 Spring MVC 入門項目:從創建到運行全流程圖解

     編輯:Java 綜合教學

IntelliJ IDEA 搭建 Spring MVC 入門項目全流程

IDEA 輕量、智能、啟動快,是現代 Java 開發首選 IDE。本文記錄從零創建 Spring MVC 項目(無 Maven),配置依賴、編寫 Controller、JSP 視圖、運行驗證全過程。

Spring MVC 核心優勢回顧

  • 關注點分離:HandlerMapping、LocaleResolver、ViewResolver 各司其職
  • 視圖解耦:邏輯視圖名 + ViewResolver 支持 JSP、Velocity、FreeMarker、PDF、Excel 等
  • Spring 生態無縫整合:IoC、AOP、事務、數據訪問一體化
  • 前端控制器模式:單一 DispatcherServlet 統籌調度

步驟 1:創建項目

  1. File → New → Project
  2. 選擇 Java Enterprise → 勾選 Web Application、Spring、Spring MVC
  3. 新建項目選項
  4. 項目名:mvcdemo,JDK 選擇 1.8+
  5. 項目命名
  6. Finish

步驟 2:手動添加 JAR 包(非 Maven 模式)

  1. 右鍵項目 → Open Module Settings (F4)
  2. Modules → Dependencies → + → JARs or directories
  3. 選擇 Spring 相關 JAR:spring-webmvc, spring-context, spring-beans, spring-core, spring-expression, spring-aop, commons-logging 等
  4. Module Settings
  5. 添加 JAR
  6. Scope 選 Compile
  7. 依賴列表

💡 強烈建議使用 Maven/Gradle 管理依賴,避免手動維护 JAR 地獄。

步驟 3:配置 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://xmlns.jcp.org/xml/ns/javaee https://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <display-name>Spring MVC Demo</display-name>
    
    <servlet>
        <servlet-name>mvcdemo</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvcdemo</servlet-name>
        <url-pattern>/</url-pattern> <!-- 攔截所有請求 -->
    </servlet-mapping>
</web-app>

步驟 4:配置 mvcdemo-servlet.xml (DispatcherServlet 上下文)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
       xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- 1. HandlerMapping: BeanNameUrlHandlerMapping (按 Bean 名稱匹配 URL) -->
    <bean class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping"/>

    <!-- 2. Controller: 實現 Controller 接口 -->
    <bean name="/hello" class="com.luoxn28.hello.HelloController"/>

    <!-- 3. ViewResolver: 內部資源視圖解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

步驟 5:編寫 Controller

package com.luoxn28.hello;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloController extends AbstractController {
    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) {
        String hello = request.getParameter("hello");
        System.out.println("HelloController 參數: " + hello);
        
        ModelAndView mav = new ModelAndView("hello"); // 邏輯視圖名
        mav.addObject("hello", hello); // 模型數據
        return mav;
    }
}

步驟 6:編寫視圖

index.jsp (入口表單)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html><body>
<form action="hello" method="post">
    hello: <input type="text" name="hello"/>
    <input type="submit" value="提交"/>
</form>
</body></html>

hello.jsp (結果頁)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html><body>
Spring MVC<br/>
${hello} <!-- EL 表達式輸出模型數據 -->
</body></html>

步驟 7:項目結構確認

mvcdemo/
├── src/
│   └── com/luoxn28/hello/HelloController.java
├── web/
│   ├── WEB-INF/
│   │   ├── mvcdemo-servlet.xml
│   │   ├── hello.jsp
│   │   └── web.xml
│   └── index.jsp
└── lib/ (手動添加的 JAR)

項目結構

步驟 8:配置 Artifacts 並部署到 Tomcat

  1. Module Settings → Artifacts → + → Web Application: Exploded → From Modules...
  2. 選擇模塊,OK
  3. Output directory 指向 Tomcat webapps/mvcdemo 或自定義
  4. Spring Facet 配置
  5. 若提示錯誤,點擊 Fix 自動修復
  6. Fix 提示

步驟 9:啟動驗證

  1. 點擊工具欄綠色三角形 ▶ (或 Shift+F10)
  2. 瀏覽器自動打開 https://localhost:8080/mvcdemo/
  3. 首頁表單
  4. 輸入 "luoxn28" 提交
  5. 跳轉顯示 "Spring MVC luoxn28"
  6. 結果頁

進階遷移建議

  1. 註解開發:替換 ControllerBeanNameHandlerMapping 為 <mvc:annotation-driven/> + @Controller + @RequestMapping
  2. REST 風格:@RestController + @GetMapping/@PostMapping 返回 JSON
  3. Maven 依賴管理:spring-boot-starter-web 一站式解決
  4. 熱部署:IDEA 啟用 Build → Build Project automatically + Registry → compiler.automake.allow.when.app.running

參考資源

Spring MVC 官方文檔、慕課網/極客時間 Spring MVC 實戰視頻課程

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