如何在 Eclipse 中更改动态 Web 项目的上下文根?

发布于 2024-08-24 19:22:43 字数 498 浏览 4 评论 0原文

我在 Eclipse 中开发了一个动态 Web 项目。 我可以使用以下 URL 通过浏览器访问该应用程序:

http://localhost:8080/MyDynamicWebApp

我想将访问 URL 更改为:

http://localhost:8080/app

为此,我从项目“属性 | Web 项目设置 | 上下文根”更改了上下文根。 但是,Web 应用程序仍然具有相同的访问 URL。我已经在Tomcat上重新部署了应用程序并重新启动了Tomcat,但访问URL与之前相同。

我发现 WAR 文件中没有附加 server.xml 文件。如果没有附加 server.xml 文件,Tomcat 如何确定我的 Web 应用程序的上下文根为 /MyDynamicWebApp 并允许我通过此上下文根 URL 访问应用程序?

I developed a dynamic web project in Eclipse.
I can access the app through my browser using the following URL:

http://localhost:8080/MyDynamicWebApp

I want to change the access URL to:

http://localhost:8080/app

To do so, I changed the context root from the project "Properties | Web Project Settings | Context Root".
However, the web app still has the same access URL. I have re-deployed the application on Tomcat and re-started the Tomcat, but the access URL is the same as earlier.

I found that there was no server.xml file attached with the WAR file. Without the server.xml file attached, how is the Tomcat determining that the context root of my web app is /MyDynamicWebApp and allowing me to access the application through this context root URL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

治碍 2024-08-31 19:22:44

更改项目属性中的上下文根后,您必须从 Tomcat 中删除 Web 应用程序(使用服务器上下文菜单上的“添加和删除...”),重新部署,然后重新添加应用程序并重新部署。这对我有用。

如果您感到惊讶,您还有另一个选择:在“服务器”视图中选择 Tomcat 服务器。双击该服务器(或在上下文菜单中选择“打开”)会出现一个多页编辑器,其中有一个“模块”页面。您可以在此处更改模块的根上下文(在此页面上称为“路径”)。

After changing the context root in project properties you have to remove your web application from Tomcat (using Add and Remove... on the context menu of the server), redeploy, then re-add your application and redeploy. It worked for me.

If you are struck you have another choice: select the Tomcat server in the Servers view. Double clicking on that server (or selecting Open in the context menu) brings a multipage editor where there is a Modules page. Here you can change the root context of your module (called Path on this page).

雅心素梦 2024-08-31 19:22:44

如果您从 Eclipse 运行 Tomcat,则它不会使用实际 Tomcat 安装中的配置。它使用在“Servers”项目下创建并存储的 Tomcat 配置。如果查看 Eclipse 工作区,您应该会看到一个名为“Servers”的项目。展开“Servers”项目,您将看到 server.xml。打开此文件并一直滚动到底部,您应该看到类似这样的内容:-

<Context docBase="abc" path="/abc" reloadable="true" source="org.eclipse.jst.jee.server:abc"/>

在这里,您可以将项目上下文路径更改为其他路径。

希望这有帮助。

If you are running Tomcat from Eclipse, it doesn't use the configuration from your actual Tomcat installation. It uses the Tomcat configuration that it created and stored under "Servers" project. If you view your Eclipse workspace, you should see a project called "Servers". Expand that "Servers" project and you will come across server.xml. Open this file and scroll all the way to the bottom, and you should see something like this:-

<Context docBase="abc" path="/abc" reloadable="true" source="org.eclipse.jst.jee.server:abc"/>

Here, you can just change your project context path to something else.

Hope this helps.

只是一片海 2024-08-31 19:22:44

我尝试了 Russ Bateman 在帖子

http://localhost:8080/Myapphttp://localhost:8080/somepath/Myapp

中建议的解决方案,但没有成功对我来说,因为我需要一个 *.war 文件来保存配置,而不是本地计算机上的服务器的单个实例。

参考

为了做到这一点,我需要将 jboss-web.xml 放置在 WEB-INF 中

<?xml version="1.0" encoding="UTF-8"?>
 <!--
Copyright (c) 2008 Object Computing, Inc.
All rights reserved.
-->
<!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Application 4.2//EN"
"http://www.jboss.org/j2ee/dtd/jboss-web_4_2.dtd">

  <jboss-web>
  <context-root>somepath/Myapp</context-root>
  </jboss-web>

I tried out solution suggested by Russ Bateman Here in the post

http://localhost:8080/Myapp to http://localhost:8080/somepath/Myapp

But Didnt worked for me as I needed to have a *.war file that can hold the config and not the individual instance of server on my localmachine.

Reference

In order to do that I need jboss-web.xml placed in WEB-INF

<?xml version="1.0" encoding="UTF-8"?>
 <!--
Copyright (c) 2008 Object Computing, Inc.
All rights reserved.
-->
<!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Application 4.2//EN"
"http://www.jboss.org/j2ee/dtd/jboss-web_4_2.dtd">

  <jboss-web>
  <context-root>somepath/Myapp</context-root>
  </jboss-web>
余生共白头 2024-08-31 19:22:44

在 Eclipse Web 模块编辑对话框中将路径设置为空,使我能够在 URL 中不包含任何路径组件(即 ROOT)的情况下访问项目。

如果在“服务器”视图中选择 Tomcat,则可以通过按 F3 访问 Web 模块编辑对话框或双击它。

一些屏幕截图:


< /a>
< /a>

Setting the path to nothing in the Eclipse web modules edit dialog enabled me to access the project without any path component in the URL (i.e. ROOT)

You can reach the web modules edit dialog by pressing F3 if you select Tomcat in the "Servers" view or by double clicking on it.

Some screenshots:



戏剧牡丹亭 2024-08-31 19:22:44

Apache tomcat 将项目上下文路径保留在 server.xml 路径中。
对于 Eclipse 上的每个 Web 项目,都有一个可以更改它的标签。
假设服务器上部署了两三个项目。
对于每一个上下文路径都存储在 .
该标签位于在 eclipse 上创建的服务器内的 server.xml 文件上。

我在服务器中的上下文根路径上有一个项目是:

<Context docBase="Test" path="/test" reloadable="true" source="org.eclipse.jst.jee.server:Test1"/>

此路径代表 Web 应用程序的上下文路径。
通过更改此路径,您的 Web 应用程序上下文路径将会更改。

Apache tomcat keeps the project context path in server.xml path.
For each web project on Eclipse, there is tag from there you can change it.
Suppose, there are two or three project deployed on server.
For each one context path is stored in .
This tag is located on server.xml file within Server created on eclipse.

I have one project for there on context root path in server is:

<Context docBase="Test" path="/test" reloadable="true" source="org.eclipse.jst.jee.server:Test1"/>

This path represents context path of your web application.
By changing this path, your web app context path will change.

提赋 2024-08-31 19:22:44

我知道答案已经被接受了。但是,万一使用 Maven 的人想要实现同样的目标,只需将 Maven build 中的 finalName 设置为您想要提供的任何名称并执行 maven -> ;更新项目

<build>
    <finalName><any-name></finalName>
    <plugins><provide-plugins-needed></plugins>
<build>

I know the answer has been accepted already. But, just in case anyone using Maven wants to achieve the same thing, just set the finalName in the maven build to whatever name you want to give and do a maven -> update project

<build>
    <finalName><any-name></finalName>
    <plugins><provide-plugins-needed></plugins>
<build>
烦人精 2024-08-31 19:22:44

如果项目是 Maven,请更改 pom.xml 中的“finalName”并将项目更新为 Maven。这对我有用。

If the project is maven, change the "finalName" in pom.xml and Update Project as Maven.This worked for me.

套路撩心 2024-08-31 19:22:44

在 Glassfish 中,您还必须更改文件 WEB-INF/glassfish-web.xml,

<glassfish-web-app>
    <context-root>/myapp</context-root>
</glassfish-web-app>

这样当您单击“运行方式>在服务器上运行”时,它将正确打开。

In Glassfish you must also change the file WEB-INF/glassfish-web.xml

<glassfish-web-app>
    <context-root>/myapp</context-root>
</glassfish-web-app>

So when you click in "Run as> Run on server" it will open correctly.

怪异←思 2024-08-31 19:22:44

我只是想补充一点,如果您根本不希望应用程序名称出现在根上下文中,则只需在 Eclipse 中添加“/”(无引号,仅是正斜杠)即可 --> Web 项目设置 -->上下文根条目

将把 web 应用程序部署到 http://localhost:8080/

当然,这会导致问题对于您尝试在服务器上运行的其他网络应用程序,因此请注意这一点。

我花了很长时间才把它们拼凑起来……所以即使这篇文章已经有 8 年历史了,希望这仍然能对某人有所帮助!

I just wanted to add that if you don't want your application name in the root context at all, you and just put "/" (no quotes, just the forward slash) in the Eclipse --> Web Project Settings --> Context Root entry

That will deploy the webapp to just http://localhost:8080/

Of course, this will cause problems with other webapps you try to run on the server, so heads up with that.

Took me forever to piece that together... so even though this post is 8 years old, hopefully this will still help someone!

吃→可爱长大的 2024-08-31 19:22:44

在 java 项目中,打开 .settings 文件夹。在那里找到名为“org.eclipse.wst.common.component”的文件。更改标签

此外,您可能想更改项目属性中的上下文根

In the java project, open .settings folder. there locate the file named "org.eclipse.wst.common.component" . Change tag <wb-module deploy-name="NEW_NAME"> .

Also you may want to change context root in project properties

七七 2024-08-31 19:22:44

如果使用 eclipse 来部署您的应用程序。我们现在可以使用这个 Maven 插件

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-eclipse-plugin</artifactId>
    <version>2.10</version>
    <configuration>
        <wtpversion>2.0</wtpversion>
        <wtpContextName>newContextroot</wtpContextName>
    </configuration>
</plugin>

转到您的项目根文件夹并在该位置打开 cmd 提示符
键入此命令:

mvn eclipse:eclipse -Dwtpversion=2.0

您可能需要重新启动 eclipse,或在服务器视图中删除服务器并创建 agian 以查看影响。我想知道这个练习在现实生活中是否有意义但有效。

If using eclipse to deploy your application . We can use this maven plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-eclipse-plugin</artifactId>
    <version>2.10</version>
    <configuration>
        <wtpversion>2.0</wtpversion>
        <wtpContextName>newContextroot</wtpContextName>
    </configuration>
</plugin>

now go to your project root folder and open cmd prompt at that location
type this command :

mvn eclipse:eclipse -Dwtpversion=2.0

You may need to restart eclipse , or in server view delete server and create agian to see affect. I wonder this exercise make sense in real life but works.

苦笑流年记忆 2024-08-31 19:22:44

如果使用maven java ee 7/8企业应用,需要编辑EAR项目的pom.xml

<build>
    <plugins>
        ...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-ear-plugin</artifactId>
            <version>2.8</version>
            <configuration>
                <version>6</version>
                <defaultLibBundleDir>lib</defaultLibBundleDir>
                <modules>
                    <webModule>
                        <groupId>com.sample</groupId>
                        <artifactId>ProjectName-web</artifactId>
                        <contextRoot>/myproject</contextRoot>
                    </webModule>
                </modules>
            </configuration>
        </plugin>
        ...
    </plugins>
</build>

If using maven java ee 7/8 enterprise application, need to edit the pom.xml of the EAR project

<build>
    <plugins>
        ...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-ear-plugin</artifactId>
            <version>2.8</version>
            <configuration>
                <version>6</version>
                <defaultLibBundleDir>lib</defaultLibBundleDir>
                <modules>
                    <webModule>
                        <groupId>com.sample</groupId>
                        <artifactId>ProjectName-web</artifactId>
                        <contextRoot>/myproject</contextRoot>
                    </webModule>
                </modules>
            </configuration>
        </plugin>
        ...
    </plugins>
</build>
青瓷清茶倾城歌 2024-08-31 19:22:44

我有一些替代资源...
但以编程方式将通过更改服务器中的 server.xml 文件......

这就是现实世界项目的配置方式....更改上下文中的路径。

<Context docBase="projectName" path="/reachingPath" reloadable="true" source="org.eclipse.jst.jee.server:projectName"/></Host>

I'm there are some alternate resource...
But the programmatically way will be by changing the server.xml file from your server .....

This is how a real world project is configured ....change the path in context.

<Context docBase="projectName" path="/reachingPath" reloadable="true" source="org.eclipse.jst.jee.server:projectName"/></Host>
一曲爱恨情仇 2024-08-31 19:22:44

它与 Maven 配合得很好。

<properties>
   <m2eclipse.wtp.contextRoot>put-the-context-here</m2eclipse.wtp.contextRoot>
</properties>

It works very well with Maven.

<properties>
   <m2eclipse.wtp.contextRoot>put-the-context-here</m2eclipse.wtp.contextRoot>
</properties>
云朵有点甜 2024-08-31 19:22:43

其中一些答案提供了解决方法。实际上必须发生的是清理并重新发布项目以“激活”新的 URI。这是通过右键单击您的服务器(在“服务器”视图中)并选择“清理”来完成的。然后你开始(或重新启动)。这里的大多数其他答案都建议您做一些实际上可以实现这一目标的事情。

正在更改的文件是 workspace/.metadata/.plugins/org.eclipse.wst.server.core/publish/publish.dat 除非您的计算机中有多个服务器在这种情况下,它将是同一路径上的publishN.dat。


不确定这是否符合礼仪 — 我正在编辑此答案以给出 Eclipse Indigo 的确切步骤。

  1. 在项目的属性中,选择Web 项目设置

  2. 上下文根更改为应用

    Eclipse 项目属性 Web 项目设置的屏幕截图

  3. 选择窗口 >显示视图>服务器

  4. 通过单击红色方框(“停止服务器”工具提示)或右键单击服务器列表以选择“停止”来停止服务器。

  5. 在您要使用的服务器上,右键单击以选择“清理...”。

    在此处输入图像描述

  6. 在此确认对话框中单击“确定”。

    要求更新服务器配置以匹配更改的上下文根的对话框的屏幕截图

现在您可以使用新的“app”运行您的应用程序网址如:

http://localhost:8080/app/

在 Eclipse 之外的生产服务器上执行此操作甚至更容易 -->重命名战争文件。将您的 Vaadin 应用导出为 WAR 文件(文件 > 导出 > Web > WAR 文件)。将 WAR 文件移至 Web 服务器的 servlet 容器(例如 Tomcat)。重命名您的 WAR 文件,在本例中为 app.war。当您启动 servlet 容器时,大多数(例如 Tomcat)将自动部署应用程序,其中包括将 war 文件展开到文件夹中。在本例中,我们应该看到一个名为 app 的文件夹。你应该可以走了。测试您的网址。对于诸如 *example.com" 之类的域,这将是:

http://www.example.com/app/

如果使用可视化附加组件,Vaadin 工具包程序员可能需要重建他们的小部件集。

Some of these answers give workarounds. What actually must happen is that you clean and re-publish your project to "activate" the new URI. This is done by right-clicking your server (in the Servers view) and choosing Clean. Then you start (or restart it). Most of the other answers here suggest you do things that in effect accomplish this.

The file that's changing is workspace/.metadata/.plugins/org.eclipse.wst.server.core/publish/publish.dat unless, that is, you've got more than one server in your workspace in which case it will be publishN.dat on that same path.


Not sure if this is proper etiquette or not — I am editing this answer to give exact steps for Eclipse Indigo.

  1. In your project's Properties, choose Web Project Settings.

  2. Change Context root to app.

    screen shot of Eclipse project properties Web Project Settings

  3. Choose Window > Show View > Servers.

  4. Stop the server by either clicking the red square box ("Stop the server" tooltip) or context-click on the server listing to choose "Stop".

  5. On the server you want to use, context-click to choose "Clean…".

    enter image description here

  6. Click OK in this confirmation dialog box.

    Screenshot of dialog asking to update server configuration to match the changed context root

Now you can run your app with the new "app" URL such as:

http://localhost:8080/app/

Doing this outside of Eclipse, on your production server, is even easier --> Rename the war file. Export your Vaadin app as a WAR file (File > Export > Web > WAR file). Move the WAR file to your web server's servlet container such as Tomcat. Rename your WAR file, in this case to app.war. When you start the servlet container, most such as Tomcat will auto-deploy the app, which includes expanding the war file to a folder. In this case, we should see a folder named app. You should be good to go. Test your URL. For a domain such as *example.com" this would be:

http://www.example.com/app/

Vaadin toolkit programmers may need to rebuild their widget set if using visual add ons.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文