java - 当不在 webapp 目录的顶层时,Tomcat 找不到索引

标签 java spring tomcat angular angular-cli

我们的问题是我们根本不知道如何告诉 Tomcat 我们的 index.html 不再位于 webapp 文件夹的顶层,而是更深一层。

我们在服务器端的app是部署在Tomcat9上的Spring、Maven、Hibernate项目。在前面,我们使用具有 Angular-CLI 魔法的 Angular2。

后端和前端是两个独立的项目,因此前端人员不需要处理 Java,反之亦然。然后我们简单地使用一些构建魔法将可部署的 Angular2 工件组合到 webapp 文件夹中,以便 Maven 可以将其打包到 WAR 中。

由于 Angular-CLI 以及它想要如何布置项目,我们最终陷入了这种情况,如下所示。 index.html 直接引用 node_modules 一个目录,angular-cli 不喜欢我们改变它。

(1) 未构建的 angular-cli 项目

|-- web-project
   |-- src
      |-- index.html
   |-- node_modules
   |-- package.json
   |-- angular-cli.json

(2) 内置到 Maven 项目中时的结构。 dist 文件夹由 angular-cli 为可部署的工件生成。
|-- webapp
   |-- dist
      |-- index.html
   |-- node_modules
   |-- WEB-INF
      |-- web.xml
      |-- (bunch of spring configs)

在 Angular-CLI 之前,我们只使用了以下链接中主要的 Angular2 网站教程中描述的布局。自从我上次检查以来,本教程发生了很大变化。

Angular2 Main Tutorial

原始方式的布局与 Tomcat 完全一致,因为“index.html”文件位于顶层,结构如下。然而 Angular-CLI 清除了构建所需的一堆任务,所以 CLI 是要走的路。
|-- webapp
   |-- index.html
   |-- node_modules
   |-- WEB-INF

早些时候,我们遇到了一个岔路口,我们可以 (1) 告诉 tomcat 此事,或者 (2) 更改 Angular-CLI 设置。由于 CLI 是如此新,而且结构有点道理,我认为 Tomcat 会更好地处理这个难题,更多的人可以帮助我。

有两个文件似乎是解决这个问题的地方。

(1)resource-mapping.xml(一个spring配置)

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <!-- Handles HTTP GET requests for the index.html file -->
    <!-- Huh? How does this work? -->
    <resources mapping="/*" location="/" />

    <!-- Handles HTTP GET requests for /resources/** by serving static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <resources mapping="/node_modules/**" location="/node_modules/" />

    <resources mapping="/themes/**" location="/themes/" />

    <!-- Opens up the dist folder -->
    <resources mapping="/dist/**" location="/dist/" />

</beans:beans>

(2)主 Controller .java

package com.mydomain.myapp;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Handles requests for the application home page. 
 * @author Some friends and I
 */
@Controller
public class MainController {

    /**
     * Simply selects the home view statically by forwarding to index.html
     * 
     * @return "/index.html".
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home() {
        return "/index.html";
    }

}

在上面的 Spring 配置中,第一个映射以某种方式与 MainController java 文件一起工作以提供 index.html。我们的大部分问题源于不知道这实际上是如何或为什么起作用的,而且我们以前从来没有在顶层之外的任何地方使用过 index.html。

到目前为止,我们已经尝试了一些更明显的方法,将 dist 放在路径前面,如下所示:return "/dist/index.html" ,但无济于事。

我们尝试了很多组合,但我们总是得到基本相同的错误,只是改变了几个词。
type Exception report

message Could not resolve view with name '/dist/index.html' in servlet with name 'appServlet'

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Could not resolve view with name '/dist/index.html' in servlet with name 'appServlet'
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1211)
    org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1011)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:955)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
note The full stack trace of the root cause is available in the Apache Tomcat/9.0.0.M9 logs.

那么有人看到我在这里缺少什么吗?除了顶级之外,还有其他人在其他地方拥有 index.html 吗?

最佳答案

使用 web.xml 文件并添加标签

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>test</display-name>
  <welcome-file-list>
    <welcome-file>dist/index.html</welcome-file>
  </welcome-file-list>
  ....rest of tags...
</web-app>

关于java - 当不在 webapp 目录的顶层时,Tomcat 找不到索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40984435/

相关文章:

tomcat - 启动 Tomcat 服务器时出现 UnsupportedClassVersionError

客户端Java加密,服务器解密,使用PBKDF2WithHmacSHA1和AES/CBC/PKCS5Padding

spring - 如何使用 Spring 4 和注释编写单元测试来验证异步行为?

spring - 如何在 Spring 表单输入标签中设置默认值

java - ClassCastException,尽管使用Quartz Scheduler时类,包和类加载器相同

java - 限制嵌入式 Tomcat 中的密码

java - 为什么向 User-B 注册的数据包监听器不起作用?

java - 基本Java : Method to tell if a double is an integer

java - 正则表达式 Junit 测试

eclipse - 无法在Linux服务器上运行的tomcat上启动war应用程序