繁体   English   中英

如何从 Spring Boot 提供静态 html?

[英]How can I serve static html from spring boot?

我从这里运行了 spring-boot-sample-web-static 项目,对 pom 做了这个改动

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>

并添加了此类以从相同的static文件夹位置提供重复的页面 index2.html:

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class Rester {

    @RequestMapping(value = "/rand", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    private RandomObj jsonEndpoint() {
        return new RandomObj();
    }

    @RequestMapping(value = "/tw")
    public String somePg() {
        return "index2";
    }
}

json url 工作正常,但是当我尝试访问 localhost:8080/tw 时,我得到一个空白页面,并且控制台中出现此错误:

2017-02-22 15:37:22.076 ERROR 21494 --- [nio-8080-exec-9] o.s.boot.web.support.ErrorPageFilter     : Cannot forward to error page for request [/tw] as the response has already been committed. As a result, the response may have the wrong status code. If your application is running on WebSphere Application Server you may be able to resolve this problem by setting com.ibm.ws.webcontainer.invokeFlushAfterService to false

难道我做错了什么?

静态文件应该由资源提供,而不是来自控制器。

Spring Boot 将自动添加位于以下任何目录中的静态 Web 资源:

 /META-INF/resources/ /resources/ /static/ /public/

参考:
https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot
https://spring.io/guides/gs/serving-web-content/

在 Spring Boot 中, /META-INF/resources//resources/static/public/目录可用于提供静态内容。

所以你可以在resources/目录下创建一个static/public/目录并将你的静态内容放在那里。 它们可以通过以下方式访问: http://localhost:8080/your-file.ext (假设server.port是 8080)

您可以使用application.properties中的spring.resources.static-locations自定义这些目录。

例如:

spring.resources.static-locations=classpath:/custom/

现在您可以使用resources/下的custom/文件夹来提供静态文件。

更新:

这也可以使用 java 配置:

@Configuration
public class StaticConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/custom/");
    }
}

此配置将custom目录的内容映射到http://localhost:8080/static/** url。

我正在使用:: Spring Boot :: (v2.0.4. RELEASE ) with Spring Framework 5

Spring Boot 2.0 要求 Java 8 作为最低版本。 许多现有 API 已更新以利用 Java 8 功能,例如:接口上的默认方法、函数式回调和新的 API(例如 javax.time)。

静态内容

默认情况下,Spring Boot 提供来自类路径或 ServletContext 根目录中名为/static (或/public/resources/META-INF/ resources)的目录中的静态内容。 它使用 Spring MVC 中的 ResourceHttpRequestHandler,因此您可以通过添加自己的WebMvcConfigurer并覆盖addResourceHandlers方法来修改该行为。

默认情况下,资源映射在/**并位于/static目录中。 但是您可以在我们的 Web 上下文配置类中以编程方式自定义静态位置。

http://localhost:8080/handlerPath/resource-path+name
                    /static         /css/myStatic.css
                    /webapp         /css/style.css
                    /templates      /style.css
 http://localhost:8080/handlerPath/resource-path+name /static /css/myStatic.css /webapp /css/style.css /templates /style.css

在 Spring 中,每个请求都将通过 DispatcherServlet。 为了避免通过 DispatcherServlet(Front contoller) 请求静态文件,我们配置了 MVC 静态内容

正如@STEEL所说,静态资源不应该通过控制器。 Thymleaf是一个 ViewResolver,它采用视图名称形式的控制器,并为视图层添加prefixsuffix

正如之前所写,一些文件夹(/META-INF/resources/、/resources/、/static/、/public/)默认提供静态内容,控制器错误配置可能会破坏这种行为。

人们在@RestController注释中定义控制器的基本url,而不是控制器顶部的@RequestMapping注释,这是一个常见的陷阱。

这是错误的:

@RestController("/api/base")
public class MyController {

    @PostMapping
    public String myPostMethod( ...) {

上面的例子会阻止你打开 index.html。 Spring 期望在根有一个 POST 方法,因为myPostMethod被映射到“/”路径。

你必须改用这个:

@RestController
@RequestMapping("/api/base")
public class MyController {

    @PostMapping
    public String myPostMethod( ...) {

您可以通过thymeleaf在 JAVA Spring-boot App 中快速提供静态内容(参考:源代码

我假设您已经添加了 Spring Boot 插件apply plugin: 'org.springframework.boot'和必要的buildscript

然后继续前进,ADD thymeleafbuild.gradle ==>

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

假设您已在src/main/resources添加了home.html要提供此文件,您需要创建一个控制器。

package com.ajinkya.th.controller;

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

  @Controller
  public class HomePageController {

      @RequestMapping("/")
      public String homePage() {
        return "home";
      }

  }

就是这样 ! 现在重启你的 gradle 服务器。 ./gradlew bootRun

我不得不将 thymeleaf 依赖添加到 pom.xml。 没有这个依赖,Spring boot 就找不到静态资源。

 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

你可以在spring boot项目中看到spring boot official示例,名为spring-boot-sample-web-static
首先,您需要extends SpringBootServletInitializer ;
其次,您需要在启动Application类中override configure方法

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    return builder.sources(Application.class);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM