You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在VS Code中配置Java EE环境并实现Web应用热重载?

推荐方案

方案1:Spring Boot(最适合新手,零复杂配置)

Spring Boot自带嵌入式Tomcat,不用手动配置外部服务器,热重载也能一键搞定:

  1. 新建Maven项目,在pom.xml中添加Spring Boot Web依赖和DevTools(热重载用):
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.2.0</version> <!-- 用最新稳定版即可 -->
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
  1. 写一个简单的Controller类,放在src/main/java/[你的包名]下:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @GetMapping("/")
    public String hello() {
        return "hello world";
    }
}
  1. 写启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
  1. 启动项目后,访问http://localhost:8080就能看到hello world。修改代码后,只要按Ctrl+F9(IDEA)或Ctrl+Shift+B(Eclipse),项目会自动重载,刷新浏览器就能看到变化。

方案2:传统Maven Web项目+Tomcat热部署插件

如果坚持用本地Tomcat,可通过Maven插件实现热重载:

  1. 在pom.xml中添加Tomcat插件配置:
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <port>8080</port>
                <path>/</path>
                <reloadable>true</reloadable> <!-- 开启热重载 -->
            </configuration>
        </plugin>
    </plugins>
</build>
  1. 在src/main/webapp/WEB-INF下建web.xml(基础版):
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>
  1. 在src/main/webapp下建index.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Hello</title>
</head>
<body>
hello world
</body>
</html>
  1. 打开终端,执行mvn tomcat7:run启动项目,访问http://localhost:8080即可。修改JSP或Java代码后,插件会自动检测变化并重载,无需重启服务器。

方案3:IDE配置本地Tomcat(以IDEA为例)

如果用IDEA开发,直接配置本地Tomcat并开启热重载:

  1. 打开IDEA的Run/Debug Configuration,添加Tomcat Server -> Local。
  2. 在Server标签页,设置Tomcat安装路径,然后在"On 'Update' action"和"On frame deactivation"选项中,选择"Update classes and resources"。
  3. 部署你的Web项目到Tomcat,设置Application context为/。
  4. 启动Tomcat,访问http://localhost:8080看到hello world。修改代码后,按Ctrl+F9触发更新,浏览器刷新就能看到变化。

内容的提问来源于stack exchange,提问作者M C

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 12:50:11