2016-07-07 67 views
0

如果我將路徑更改爲與「/」不同的任何東西,則會得到404錯誤。我的意思是,如果使用爲什麼使用/ manager/text部署tomcat7-maven-plugin上下文

<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服務器屬性)。

請注意,在瀏覽器中,我確實看到localhost:8080/calories-tracker,無論我在以前啓動時是否啓動/部署「mvn clean install tomcat7:run-war」而不是「mvn tomcat7:deploy」 Tomcat服務器。

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 - 的settings.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編寫了上面所做的更改。

+0

我克隆了你的項目,並在tomcat7插件中放了一個'/test'。它將應用程序部署到測試上下文路徑,但是css和圖像會混亂,因爲應用程序期望在根路徑中找到它們。 – Apostolos

+0

Apostolos,我該如何解決這個問題?在應用程序中,我必須更改它?我對AngularJS非常有限,是否需要在某些AngularJS配置中進行更改? –

+0

讓我檢查一下,如果我發現如何正確設置路徑,我會發布我的答案:) – Apostolos

回答

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: [] 
     }; 

則存儲上下文路徑在一個範圍可變的,並且可以在指定的控制器上下文中使用它。也許這不是最好的方式,但它的工作原理!

編輯: 首先你需要爲需要

<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端點,然後決定如何處理樣式鏈接。

+0

Apostolos,我可以看到你提供了一個非常好的技巧,但我有點困惑如何實現它。如果你能指出任何Hello World這樣做,或者你從git下載應用程序,並指出我必須改變的地方,我會問一個問題嗎?坦率地說,最初我認爲這只是我部署到服務器的問題,但在AngularJs中編寫某些解決方法以使上下文路徑更靈活似乎是正常的。由於我非常喜歡Angularjs,因此所有提示都將得到高度評價。 –

相關問題