简体   繁体   English

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

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

I like to know how the context path is set in a java web application. 我想知道如何在java Web应用程序中设置上下文路径。
More precisely in case of a maven project, does the context path get set from the pom.xml file? 更确切地说,在maven项目的情况下,是否从pom.xml文件设置了上下文路径?
Is the context path value referred anywhere in the web application or is it just the name of the WAR file? 上下文路径值是在Web应用程序中的任何位置引用的,还是仅仅是WAR文件的名称?
Is there a possibility that the name of the WAR file and context path are different? WAR文件的名称和上下文路径是否有可能不同?

The context path is the name of the war file, despite if the project is built via ant, maven, gradle or whatever. 尽管项目是通过ant,maven,gradle或其他方式构建的,但上下文路径是war文件的名称。 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: 在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 . 你可以做的另一种方法是使用你正在使用所描绘的应用服务器特定的配置在这里

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以查看根上下文值的反映更改或导航到项目的Deployment Assembly以查看更改

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. 3.特定于应用程序服务器:您应该更喜欢遵循服务器无关的方法,但如果需要这样做,那么您可以在服务器特定的配置文件中配置根上下文URL。 You can find detailed approach here 你可以在这里找到详细的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM