Java Web 应用程序中的上下文路径是如何设置的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35063994/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 23:41:39  来源:igfitidea点击:

How is the context path set in a Java web application?

javamaven

提问by Karthik Ayyalasomayajula

I like to know how the context path is set in a java web application.
More precisely in case of a maven project, does the context path get set from the pom.xml file?
Is the context path value referred anywhere in the web application or is it just the name of the WAR file?
Is there a possibility that the name of the WAR file and context path are different?

我想知道如何在 Java Web 应用程序中设置上下文路径。
更准确地说,在 maven 项目的情况下,上下文路径是否从 pom.xml 文件中设置?
上下文路径值是在 Web 应用程序中的任何位置引用还是只是 WAR 文件的名称?
WAR 文件的名称和上下文路径是否有可能不同?

采纳答案by Luiggi Mendoza

The context path is the name of the war file, despite if the project is built via ant, maven, gradle or whatever. If you want to change the context path of your app, then the simplest way would be to change the name of the generated war. In maven, this can be done via plugin, here's an example:

上下文路径是 war 文件的名称,不管项目是通过 ant、maven、gradle 还是其他方式构建的。如果您想更改应用程序的上下文路径,那么最简单的方法是更改​​生成的War的名称。在 maven 中,这可以通过插件完成,这是一个例子:

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <warName>kasnet-webapp</warName>
    </configuration>
</plugin>

Another way you can do it is using a specific configuration for the application server you're using as depicted here.

你可以做的另一种方法是使用你正在使用所描绘的应用服务器特定的配置在这里

回答by Nikhil Bhide

Adding answer to provide complete details.

添加答案以提供完整的详细信息。

There are three ways to do it:

有以下三种方法:

1. If you are not using Eclipse/MyEclipse to deploy the application onto application server -

1. 如果您不使用 Eclipse/MyEclipse 将应用程序部署到应用程序服务器上 -

You need to make use of maven-war plugin, you can specify warName in configuration section.

您需要使用 maven-war 插件,您可以在配置部分指定 warName。

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.0.0</version>
    <configuration>
        <warName>customwarname</warName>
    </configuration>
</plugin>

2. If you are using Eclipse/MyEclipse to deploy the application onto application server -

2. 如果您使用 Eclipse/MyEclipse 将应用程序部署到应用程序服务器上 -

If you are using eclipse and deploying war using eclipse then you can use following maven configuration.

如果您使用 eclipse 并使用 eclipse 部署 war,那么您可以使用以下 maven 配置。

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

Then, run following commands to update eclipse settings.

然后,运行以下命令来更新 eclipse 设置。

   mvn eclipse:eclipse -Dwtpversion=2.0

Restart Eclipse and then navigate to project properties, Properties->Web to view the reflected changes in root-context value or navigate to Deployment Assembly of the project to view the changes

重新启动 Eclipse,然后导航到项目属性,Properties->Web 以查看根上下文值中反映的更改或导航到项目的部署程序集以查看更改

Note that above can be achieved using m2eclipse by adding a new plugin.

请注意,上面可以通过添加一个新插件使用 m2eclipse 来实现。

3. Application server specific:You should prefer to follow server agnostic approach, but if are required to do it then you can configure root context url in server specific configuration file. You can find detailed approach here

3. 特定于应用程序服务器:您应该更喜欢遵循与服务器无关的方法,但如果需要这样做,那么您可以在特定于服务器的配置文件中配置根上下文 url。你可以在这里找到详细的方法

回答by Paulo A. Costa

I′m using Eclipse/Maven/Wildfly and the only solution was create a file "jboss-web.xml" at "WEB-INF" folder, inserting the content below;

我正在使用 Eclipse/Maven/Wildfly,唯一的解决方案是在“WEB-INF”文件夹中创建一个文件“jboss-web.xml”,插入下面的内容;

<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
    <context-root>MyContextName</context-root>
</jboss-web>