【问题标题】:Spring Mvc and Angular2 run on Tomcat serverSpring Mvc 和 Angular2 在 Tomcat 服务器上运行
【发布时间】:2018-06-23 04:28:47
【问题描述】:

我希望 Angular 2 项目中的 index.html 作为 Spring MVC 应用程序的欢迎页面运行。我们不能使用maven项目,只需要创建spring MVC项目。

我已经尝试将 angular dist 文件夹复制到 web 目录中,

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

这是我的文件夹结构,

我收到 HTTP 状态 404 -错误

谁能帮帮我

提前致谢!!!

【问题讨论】:

  • 将 index.html 中的 base href 更改为 。复制 tomcat 服务器 webapps 中的 dist 文件夹并启动服务器并点击 ip:port/dist/
  • 我在回答中添加了更多更新。

标签: angular spring-mvc tomcat7


【解决方案1】:

有两件事你需要注意

FIRST - 您需要将 Angular 工件保存在 WEB-INF 目录之外。您应该将dist 文件夹中的内容直接复制到您的web 目录中。

Ref - JSP in /WEB-INF returns "HTTP Status 404 The requested resource is not available" 了解更多详情。

SECOND - 你的index.html 应该包含正确的base href 而不仅仅是默认的"/"。你的base href 应该是

<base href="/myUrl/">

myUrl 是您的 Web 应用程序上下文。

您可以手动修改 index.html 中的基本 href,也可以在像这样构建 Angular 应用程序时提供它。

ng build --base-href /myUrl/

Ref - Set base href dynamically - Angular 2 / 4 / 5 了解更多详情。

更新

如果你真的想将你的 Angular 工件保存在 /web/dist 目录中,那么

您的web.xml 应该有以下内容

<welcome-file-list>
    <welcome-file>dist/index.html</welcome-file>
</welcome-file-list>

你的index.html应该包含

<base href="/myUrl/dist/">

你应该定义一个端点

@GetMapping("/dist")
public void forward(HttpServletResponse response) throws IOException {
    response.sendRedirect("/myUrl/dist/index.html");
}

然后您可以使用以下任何 url 访问您的 Angular 应用程序

http://host:port/myUrl
http://host:port/myUrl/dist
http://host:port/myUrl/dist/index.html

并且重新加载也不会造成任何问题。

更新 - 2

上述端点可能无法重新加载 html5 角度路由 url。因此,您可以应用下面的过滤器来代替上述端点,该过滤器将能够处理重新加载。

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.
             ....
             .addFilterAfter(new OncePerRequestFilter() {
                   // add the values you want to redirect for
                   private Pattern patt = Pattern.compile("/dist/.*");

                   @Override
                   protected void doFilterInternal(HttpServletRequest request, 
                                                   HttpServletResponse response, 
                                                   FilterChain filterChain)
                                    throws ServletException, IOException {
                        if(this.patt.matcher(request.getRequestURI()).matches()) {
                            RequestDispatcher rd = request.getRequestDispatcher("/dist/index.html");
                            rd.forward(request, response);
                        } else {
                            filterChain.doFilter(request, response);
                        }
                 }
        }, FilterSecurityInterceptor.class)
        .... 

Ref - https://github.com/spring-guides/tut-spring-security-and-angular-js/issues/68#issuecomment-187675742 了解更多详情。

【讨论】:

  • 那么,恐怕您必须将 dist 文件夹的内容复制到您的 web 目录中。我会相应地更新我的答案。
  • @Robert - 这是因为当您加载主页http://host:port/myUrl 时,网址变为http://host:port/myUrl/dist/,而当您重新加载浏览器时,它在此网址上找不到任何内容。因此,最好将内容从 dist 复制到 web 目录。
  • @Robert - 而且,如果您点击 url http://host:port/myUrl/dist/index.html,它将加载页面,但由于页面中设置了基本 url,url 再次变为 http://host:port/myUrl/dist/。因此,最好将内容从 dist 复制到 web 目录而不是整个 dist 目录。
  • @Robert - 如果您真的想将 Angular 工件保存在 /web/dist 目录中,请查看我的更新答案。
猜你喜欢
  • 2016-10-30
  • 1970-01-01
  • 2021-08-20
  • 1970-01-01
  • 2013-09-07
  • 1970-01-01
  • 2017-07-16
  • 1970-01-01
  • 2017-06-02
相关资源
最近更新 更多