【发布时间】:2016-07-07 02:00:01
【问题描述】:
如果我将路径更改为不同于“/”的任何内容,我会收到 404 错误。我的意思是,如果使用
<plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><path>/any_word</path><url>http://localhost:8080/manager/text</url>
在下面的 pom.xml 中然后我会得到
http://localhost:8080/authenticate 404 (Not Found)
在我看来,上下文路径存在某些问题,但我不知道如何解决它。我设法让应用程序在我的本地 Tomcat 中运行,但我当然不能在生产中应用相同的方法。我删除了在 Tomcat 中找到的“/”默认值。然后,我拿走了“any_word”,只在 pom.xml 中留下了“/”。它使应用程序完美运行,但正如您想象的那样,我无法将应用程序部署到生产中的根路径“/”,因为服务器管理员显然需要我提供一个特定且唯一的上下文路径。
我在下面添加了与我的问题相关的所有步骤和来源。从我个人的角度来看,我从不太重要到更重要的顺序排序(我确实认为 pom.xml 可能有问题,但我可能错了,我必须更改一些 spring 配置或 Tomcat Server 属性)。
请注意,在浏览器中,无论我是否在之前启动的 Tomcat 中启动/部署“mvn clean install tomcat7:run-war”而不是“mvn tomcat7:deploy”,我都会看到 localhost:8080/calories-tracker服务器。
1 - 在 TomcatManager (http://localhost:8080/manager/html/list)
我取消部署“/”
2 - Windows 的命令提示符
C:> mvn tomcat7:deploy
3 - 简介(“开发”)
@Configuration
@Profile("development")
@EnableTransactionManagement
public class DevelopmentConfiguration {
@Bean(name = "datasource")
public DriverManagerDataSource dataSource() {
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DriverManagerDataSource dataSource) {
4 - catalina.properties
spring.profiles.active=development
5 - 设置.xml
<servers>
<server>
<id>tomcat8</id>
<username>admin</username>
<password>admin</password>
</server>
</servers>
6 - WebAppInitializer
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[]{RootContextConfig.class, DevelopmentConfiguration.class, TestConfiguration.class,
AppSecurityConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] {ServletContextConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
7 - Web.xml
<web-app>
<display-name>Archetype Created Web Application</display-name>
<welcome-file-list>
<welcome-file>/resources/calories-tracker.html</welcome-file>
</welcome-file-list>
</web-app>
8- Pom.xml
<build>
<finalName>calories-tracker</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
<url>http://localhost:8080/manager/text</url>
<server>tomcat8</server>
<keystoreFile>${basedir}/other/keystore.jks</keystoreFile>
<keystorePass>secret</keystorePass>
</configuration>
</plugin>
</plugins>
</build>
***在创建问题几分钟后添加 原始项目可以从https://github.com/jhades/spring-mvc-angularjs-sample-app 下载。我刚刚使用 Tomcat 和 Pom 进行了上述更改。
【问题讨论】:
-
我克隆了你的项目并将
<path>/test</path>放入tomcat7插件中。它部署应用程序以测试上下文路径,但 css 和图像搞砸了,因为应用程序希望在根路径中找到它们。 -
Apostolos,我该如何解决这个问题?我必须在应用程序的哪个位置更改它?我对 AngularJS 非常有限,是否必须在某些 AngularJS 配置中进行更改?
-
让我检查一下,如果我发现如何正确设置路径,我会发布我的答案:)
-
所以你现在遇到的问题是css和图像? deploy 对路径没问题,对吗?
-
是的,你是对的。好吧,假设我使用
/any_path localhost:8080/manager/text</url> 我可以看到 wellcome 页面,这意味着部署正常,但所有 css 和图像都丢失了。拜托,你能告诉我我必须在哪里更改以使应用程序查找 localhos:8080/any_path/resources 而不是 localhos:8080/resources?
标签: spring maven tomcat maven-tomcat-plugin tomcat-manager