Java 如何从spring boot提供静态html?

Java 如何从spring boot提供静态html?,java,spring-boot,Java,Spring Boot,我从运行SpringBoot示例web静态项目,对pom进行了此更改 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> json url工作正常,但当我尝试访问localhost:8080/tw时,我得到一个空白页

我从运行SpringBoot示例web静态项目,对pom进行了此更改

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
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

我做错什么了吗?

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

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";
      }

  }
SpringBoot将自动添加位于 以下任一目录:

/META-INF/resources/  
/resources/  
/static/  
/public/
参考文献:


在Spring boot中,
/META-INF/resources/
/resources/
静态/
公共/
目录可用于提供静态内容

因此,您可以在
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/");
    }
}

此混淆将
自定义
目录的内容映射到
http://localhost:8080/static/**
url.

您可以通过
thymeleaf
(参考:)在JAVA Spring boot应用程序中快速提供静态内容

我假设您已经添加了SpringBoot插件
apply插件:“org.springframework.Boot”
和必要的
buildscript

然后继续并将thymeleaf添加到您的
构建中。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
要提供此文件,您需要创建一个控制器

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

我正在使用Spring Framework 5的Spring Boot::(v2.0.4.

SpringBoot2.0需要Java8作为最低版本。许多现有的API已经更新,以利用Java8的特性,例如:接口上的默认方法、函数回调和新的API,如javax.time

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

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

@Configuration@EnableWebMvc
公共类静态资源处理器实现WebMVCConfiguer{
@凌驾
public void addResourceHandlers(ResourceHandlerRegistry注册表){
//重写默认行为时,您需要添加默认(/)以及添加的静态路径(/webapp)。
//src/main/resources/static/。。。
登记处
//.addResourceHandler(“/**”)/«/css/myStatic.css
.addResourceHandler(“/static/**”/«/static/css/myStatic.css
.addResourceLocations(“类路径:/static/”///默认静态操作
.setCachePeriod(3600)
.resourceChain(true)//4.1
.addResolver(新的GzipResourceResolver())//4.1
.addResolver(新的PathResourceResolver());//4.1
//src/main/resources/templates/static/。。。
登记处
.addResourceHandler(“/templates/**”/«/templates/style.css
.addResourceLocations(“类路径:/templates/static/”;
//如果应用程序打包为jar,请不要使用src/main/webapp/…目录。
登记处
.addResourceHandler(“/webapp/**”)/«/webapp/css/style.css
.addResourceLocations(“/”);
//文件位于磁盘上
登记处
.addResourceHandler(“/system/files/**”)
.addResourceLocations(“file:///D:/");
}
}
http://localhost:8080/handlerPath/resource-路径+名称
/static/css/myStatic.css
/webapp/css/style.css
/模板/style.css
在春天,每个请求都将通过DispatcherServlet。为了避免通过DispatcherServlet(前端控制器)进行静态文件请求,我们配置了MVC


正如
@STEEL
所说,静态资源不应该通过控制器
Thymleaf
是一个视图解析程序,它以视图名称作为控制器,并将
前缀和
后缀添加到视图层。

如前所述,某些文件夹(/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( ...) {

我必须向pom.xml添加thymeleaf依赖项。如果没有此依赖项,Spring引导无法找到静态资源

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

org.springframework.boot
弹簧启动装置

噢,好吧,我看到localhost:8080/index2.html实际上可以从浏览器中查看,并且没有办法从控制器提供服务?我必须添加spring-boot-starter-t