【发布时间】:2016-12-06 06:35:47
【问题描述】:
我们的问题是我们根本不知道如何告诉 Tomcat 我们的 index.html 不再位于 webapp 文件夹的顶层,它是一个更深的文件夹。
我们服务器上的应用是部署在Tomcat9上的Spring、Maven、Hibernate项目。在前面,我们使用 Angular2 和 Angular-CLI 魔法。
后端和前端是两个独立的项目,因此前端人员不需要处理 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 网站教程中描述的布局。自从我上次检查以来,本教程发生了很大变化。
原始方式的布局在 Tomcat 上工作得很好,因为“index.html”文件位于顶层,具有如下结构。然而 Angular-CLI 清除了构建所需的一堆任务,所以 CLI 是要走的路。
|-- webapp
|-- index.html
|-- node_modules
|-- WEB-INF
之前我们有一个岔路口,我们可以 (1) 告诉 tomcat 这件事,或者 (2) 更改 Angular-CLI 设置。由于 CLI 很新,而且结构有点意思,我认为 Tomcat 会更好地处理这个难题,更多的窥视可以帮助我。
有两个文件似乎可以用来解决这个问题。
(1) resource-mapping.xml(一个弹簧配置)
<?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) MainController.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 吗?
【问题讨论】:
标签: java spring tomcat angular angular-cli