【问题标题】:why deploy tomcat7-maven-plugin with /manager/text messed up the context为什么用 /manager/text 部署 tomcat7-maven-plugin 会弄乱上下文
【发布时间】: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 进行了上述更改。

【问题讨论】:

  • 我克隆了你的项目并将&lt;path&gt;/test&lt;/path&gt;放入tomcat7插件中。它部署应用程序以测试上下文路径,但 css 和图像搞砸了,因为应用程序希望在根路径中找到它们。
  • Apostolos,我该如何解决这个问题?我必须在应用程序的哪个位置更改它?我对 AngularJS 非常有限,是否必须在某些 AngularJS 配置中进行更改?
  • 让我检查一下,如果我发现如何正确设置路径,我会发布我的答案:)
  • 所以你现在遇到的问题是css和图像? deploy 对路径没问题,对吗?
  • 是的,你是对的。好吧,假设我使用 /any_pathlocalhost:8080/manager/text</url> 我可以看到 wellcome 页面,这意味着部署正常,但所有 css 和图像都丢失了。拜托,你能告诉我我必须在哪里更改以使应用程序查找 localhos:8080/any_path/resources 而不是 localhos:8080/resources?

标签: spring maven tomcat maven-tomcat-plugin tomcat-manager


【解决方案1】:

您需要将 $location 添加到您的控制器。

angular.module('common', ['ngMessages'])
    .controller('BaseFormCtrl', ['$scope', '$http', '$location', function ($scope, $http, $location) {

        var fieldWithFocus;

        $scope.path = $location.absUrl().split('/')[3];

        $scope.vm = {
            submitted: false,
            errorMessages: []
        };

然后您将上下文路径存储在范围变量中,您可以在指定的控制器上下文中使用它。也许这不是最整洁的方式,但它确实有效!

编辑: 首先,您需要在登录页面中将以下标记更改为require

<script type="text/javascript" data-main="./js/run-loggin-app"
        src="../bower_components/requirejs/require.js"></script>

那么,如果你想看看它的实际效果

<link rel="stylesheet" type="text/css" href="/{{path}}/resources/bower_components/pure/pure.css">
<link rel="stylesheet" type="text/css" href="/{{path}}/resources/bower_components/pure/grids-responsive.css">
<link rel="stylesheet" type="text/css" href="/{{path}}/resources/public/css/pure-theme.css">
<link rel="stylesheet" type="text/css" href="/{{path}}/resources//public/css/calories-tracker.css">

但这需要几毫秒的时间来加载,这意味着您将看到一个没有任何 css 的页面,直到有角度加载。

您可以看到将链接更改为相对路径的区别。

<link rel="stylesheet" type="text/css" href="../bower_components/pure/pure.css">
<link rel="stylesheet" type="text/css" href="../bower_components/pure/grids-responsive.css">
<link rel="stylesheet" type="text/css" href="../public/css/pure-theme.css">
<link rel="stylesheet" type="text/css" href="../public/css/calories-tracker.css">

您可以首先更改链接和 POST/GET 端点,然后决定如何处理样式链接。

【讨论】:

  • Apostolos,我可以看到你提供了一个非常好的技巧,但我有点困惑如何实现它。如果您可以指出任何 Hello World 这样做,或者您从 git 下载应用程序并指出我必须更改的确切位置,我会问很多吗?老实说,最初我认为这只是我如何部署到服务器的问题,但在 AngularJs 中编写某些解决方法以使上下文路径灵活似乎是正常的。由于我是 Angularjs 的初学者,所有的技巧都将受到高度赞赏。
猜你喜欢
  • 2013-12-03
  • 1970-01-01
  • 1970-01-01
  • 2011-11-22
  • 1970-01-01
  • 2019-04-10
  • 1970-01-01
  • 2012-07-01
  • 2021-09-29
相关资源
最近更新 更多