Java 在 Eclipse 动态 Web 项目中包含 HTML 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6363134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Include HTML File in Eclipse Dynamic Web Project
提问by ZKSteffel
I know there has to be a simple answer to this, but Google's not helping at the moment. I've got a dynamic web project in Eclipse which is using Java/Jersey to run a web service. All my java resources are called in a URI like projectname/test/statistics
, etc. for different resources. My hangup is on a relatively simple point:
How do I include an HTML file in this project?
I'm planning to have this file as a sort of 'home page', in order to handle logins, etc. I'd like the URI to be something like projectname/
or projectname/login
, but I can't seem to find where I can define the path to reference the HTML file. Is there something I need to add to the HTML itself, or is it a setting in the Eclipse project?
我知道对此必须有一个简单的答案,但 Google 目前没有帮助。我在 Eclipse 中有一个动态 Web 项目,它使用 Java/Jersey 来运行 Web 服务。我的所有 java 资源都在 URI 中调用,例如projectname/test/statistics
等,用于不同的资源。我的挂断是在一个相对简单的点上:
我如何在这个项目中包含一个 HTML 文件?
我打算将此文件作为一种“主页”,以便处理登录等。我希望 URI 类似于projectname/
or projectname/login
,但我似乎找不到可以定义的位置引用 HTML 文件的路径。有什么我需要添加到 HTML 本身,还是 Eclipse 项目中的设置?
Update:
Soo... I still can't run my html file through eclipse. What I have to do right now is deploy a war to Tomcat without the html, then put the html file in a completely separate folder to run it. This means that I have to type a different context root whenever I call my file.
更新:
Soo...我仍然无法通过 Eclipse 运行我的 html 文件。我现在要做的是在没有 html 的情况下向 Tomcat 部署War,然后将 html 文件放在一个完全独立的文件夹中以运行它。这意味着每当我调用我的文件时,我都必须输入不同的上下文根。
采纳答案by ZKSteffel
My workaround:
Since I haven't been able to get this working directly in Eclipse, I've been placing my html page manually in the directory where my service is deployed to Tomcat. Placing it in the ROOT folder lets me hit the page at localhost:8080/login.html
, and I can deploy it to other folders, say Test, to hit it at localhost:8080/Test/login.html
, but due to my security settings (at the moment), I cannot place it in the same folder as the rest of my deployed project.
我的解决方法:
由于我无法直接在 Eclipse 中进行此操作,因此我一直将我的 html 页面手动放置在我的服务部署到 Tomcat 的目录中。将它放在 ROOT 文件夹中可以让我点击页面localhost:8080/login.html
,我可以将它部署到其他文件夹,比如测试,点击它localhost:8080/Test/login.html
,但由于我的安全设置(目前),我不能将它放在同一个文件夹中作为我部署项目的其余部分。
回答by highlycaffeinated
If you created your project in Eclipse, you probably have a WebContent folder off of the root of your project. Files placed in there will be served relative to your project's context root.
如果您在 Eclipse 中创建了项目,则您的项目根目录下可能有一个 WebContent 文件夹。放置在那里的文件将相对于您的项目的上下文根提供。
For example, if your context root is MyWebApp
, and you put the file hello.html
into the WebContent folder, you can see your file at http://servername/MyWebApp/hello.html
.
例如,如果您的上下文根是MyWebApp
,并且您将文件hello.html
放入 WebContent 文件夹,则可以在http://servername/MyWebApp/hello.html
.
Your context root can be found/changed by going to the project properties and selecting "Web Project Settings".
您可以通过转到项目属性并选择“Web 项目设置”来找到/更改您的上下文根。
回答by Siddharth
I solved this problem by changing web.xml from:
我通过更改 web.xml 解决了这个问题:
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
to
到
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
And then http://servername/MyWebApp
launched index.html correctly.
然后http://servername/MyWebApp
正确启动 index.html。