如何在Spring Boot Embedded Tomcat中为外部静态HTML文件提供服务? [英] How to service external static HTML files in Spring Boot Embedded tomcat?

查看:248
本文介绍了如何在Spring Boot Embedded Tomcat中为外部静态HTML文件提供服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Spring框架和Spring Boot的新手.
我已经实现了一个非常简单的RESTful Spring Boot Web应用程序.
您可以在另一个问题中看到几乎完整的源代码: Spring Boot:如何外部化JDBC数据源配置?

I'm new to Spring framework and Spring Boot.
I've implemented a very simple RESTful Spring Boot web application.
You can see the nearly full source code in another question: Spring Boot: How to externalize JDBC datasource configuration?

该应用程序如何为外部静态HTML,css js文件提供服务?
例如,目录结构可以如下:

How can the app service external static HTML, css js files?
For example, the directory structure may be as follows:

MyApp\
   MyApp.jar (this is the Spring Boot app that services the static files below)
   static\
       index.htm
       images\
           logo.jpg
       js\
           main.js
           sub.js
       css\
           app.css
       part\
           main.htm
           sub.htm

我已经阅读了构建包含静态HTML文件的.WAR文件的方法,但是由于即使在单个HTML文件修改时,它也需要重建和重新部署WAR文件,因此该方法是不可接受的.

I've read the method to build a .WAR file that contains static HTML files, but since it requires rebuild and redeploy of WAR file even on single HTML file modification, that method is unacceptable.

由于我对Spring的了解非常有限,因此最好提供确切而具体的答案.

An exact and concrete answer is preferable since my knowledge of Spring is very limited.

推荐答案

我从

I see from another of your questions that what you actually want is to be able to change the path to static resources in your application from the default values. Leaving aside the question of why you would want to do that, there are several possible answers.

  • 一个是可以提供类型为WebMvcConfigurerAdapter的普通Spring MVC @Bean,并使用addResourceHandlers()方法向静态资源添加其他路径(有关默认值,请参见WebMvcAutoConfiguration).
  • 另一种方法是使用ConfigurableEmbeddedServletContainerFactory功能来设置servlet上下文根路径.
  • 完整的核选项"是提供类型为EmbeddedServletContainerFactory@Bean定义,以您想要的方式设置servlet容器.如果您使用现有的具体实现之一,则它们会扩展您已经找到的Abstract*类,因此它们甚至有一个名为documentRoot的属性的设置器.您还可以使用类型为EmbeddedServletContainerCustomizer@Bean进行很多常见的操作.
  • One is that you can provide a normal Spring MVC @Bean of type WebMvcConfigurerAdapter and use the addResourceHandlers() method to add additional paths to static resources (see WebMvcAutoConfiguration for the defaults).
  • Another approach is to use the ConfigurableEmbeddedServletContainerFactory features to set the servlet context root path.
  • The full "nuclear option" for that is to provide a @Bean definition of type EmbeddedServletContainerFactory that set up the servlet container in the way you want it. If you use one of the existing concrete implementations they extend the Abstract* class that you already found, so they even have a setter for a property called documentRoot. You can also do a lot of common manipulations using a @Bean of type EmbeddedServletContainerCustomizer.

这篇关于如何在Spring Boot Embedded Tomcat中为外部静态HTML文件提供服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆