【问题标题】:Cannot save .html file in eclipse无法在 Eclipse 中保存 .html 文件
【发布时间】:2026-02-05 10:05:01
【问题描述】:

我正在 Eclipse 中开发一个 javascript 项目。静态 html 和 javascript 文件需要访问我的服务的 restful 端点,所以我将它们放在带有这些端点的 java 项目中,这样我就可以访问它们而不会出现跨域问题。

不过,突然之间,如果我尝试将更改保存到我的 html 文件,除非我停止运行 java 应用程序,否则我无法保存。

事件的顺序是这样的:

Run java web app with Jetty

Can save changes to html file.

Open html file in chrome with url: http://127.0.0.1:8901/myapp/myapp-admin.html

Cannot save changes to html file.

Close chrome.

Still can't save changes.

Stop jetty running in eclipse.

Can save changes.

当我尝试保存时出现以下错误:

Save could not be completed. Try File > Save As... if the problem persists.

Reason:
Could not write file:
C:\{path to file}\myapp-admin.html

(The requested operation cannot be performed on a file with a user-mapped section open)

如果我尝试使用另存为覆盖该文件,它仍然无法正常工作,并出现错误:

Save could not be completed. Could not write file: {etc.}

这是 html 文件:

<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<head>


<title>myapp Admin</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
    <h1>myapp</h1>

</body>
</html>

我唯一改变的是这些在 web.xml 中的映射方式。基本上,在我使用弹簧控制器等以弹簧方式渲染它们之前。

现在我正在静态渲染它们,使用:

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.html</url-pattern>
    <url-pattern>*.js</url-pattern>
    <url-pattern>*.css</url-pattern>
</servlet-mapping>

但我不知道为什么会导致这种问题。

Spring 的映射如下:

<!-- Declare a Spring MVC DispatcherServlet as usual -->
<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- Configure DispatcherServlet to use AnnotationConfigWebApplicationContext 
        instead of the default XmlWebApplicationContext -->
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>
          org.springframework.web.context.support.AnnotationConfigWebApplicationContext
      </param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>

谁能帮我弄清楚这里出了什么问题,以及是否有办法让它像我想要的那样工作?

【问题讨论】:

    标签: java html eclipse jetty web.xml


    【解决方案1】:

    我通过从 Jetty 切换到 Tomcat 解决了这个问题。我只是使用 Maven 插件,所以切换时间为 30 秒。我不明白为什么您必须专门配置它,因为它是在您的开发环境中进行热部署的一项功能。为此,我不认为 Jetty 解决了问题,而是创造了一个新问题。

    【讨论】:

      【解决方案2】:

      事实证明,使用 Jetty 8.1.9 需要一个不同的解决方案。作为this answer describes,我必须将maxCachedFiles 设置为0。

      【讨论】:

        【解决方案3】:

        您在 Windows 上遇到了经典的文件锁定问题。 (这不会发生在任何版本的 unix、linux 或 osx 上)

        尝试按照troubleshooting locked files on windows 中的指南为您的特定 web 应用禁用 Jetty 的高级性能功能。

        (链接到 Jetty 9 文档,因为您没有指定您使用的 Jetty 版本)

        【讨论】:

        • 你太棒了。我很惊讶我在谷歌搜索中没有遇到这个解决方案。你会认为它会更容易找到。无论哪种方式,修改 webdefault.xml 都为我解决了这个问题。 (顺便说一句,我正在使用 Jetty 8。)