0

如果我将路径更改为不同于“/”的任何内容,我会收到 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 )

我取消了“/”的部署</p>

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 进行了上述更改。

4

1 回答 1

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 端点,然后决定如何处理样式链接。

于 2016-07-07T16:10:59.353 回答