我从这里运行了spring-boot-sample-web-static项目,对pom进行了改动
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
并添加此类以从同一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";
}
}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
难道我做错了什么?
cse*_*nga 36
静态文件应该从资源提供,而不是从控制器提供.
Spring Boot将自动添加位于以下任何目录中的静态Web资源:
Run Code Online (Sandbox Code Playgroud)/META-INF/resources/ /resources/ /static/ /public/
refs:
https
://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot https://spring.io/guides/gs/serving-web-content/
Ham*_*eji 21
在春天开机,/META-INF/resources/
,/resources/
,static/
和public/
目录都可以提供静态内容.
因此,您可以在目录下创建一个static/
或目录,并将静态内容放在那里.它们将通过以下方式访问:.(假设是8080)public/
resources/
http://localhost:8080/your-file.ext
server.port
您可以spring.resources.static-locations
在中使用自定义这些目录application.properties
.
例如:
spring.resources.static-locations=classpath:/custom/
Run Code Online (Sandbox Code Playgroud)
现在您可以使用下面的custom/
文件夹resources/
来提供静态文件.
我在用 :: 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/资源)的目录提供静态内容。它使用 Spring MVC 中的 ResourceHttpRequestHandler,以便您可以通过添加自己WebMvcConfigurer
的addResourceHandlers
方法并覆盖该方法来修改该行为。
默认情况下,资源映射/**
并位于/static
目录上。但是您可以在我们的 Web 上下文配置类中以编程方式自定义静态位置。
@Configuration @EnableWebMvc
public class Static_ResourceHandler implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// When overriding default behavior, you need to add default(/) as well as added static paths(/webapp).
// src/main/resources/static/...
registry
//.addResourceHandler("/**") // « /css/myStatic.css
.addResourceHandler("/static/**") // « /static/css/myStatic.css
.addResourceLocations("classpath:/static/") // Default Static Loaction
.setCachePeriod( 3600 )
.resourceChain(true) // 4.1
.addResolver(new GzipResourceResolver()) // 4.1
.addResolver(new PathResourceResolver()); //4.1
// src/main/resources/templates/static/...
registry
.addResourceHandler("/templates/**") // « /templates/style.css
.addResourceLocations("classpath:/templates/static/");
// Do not use the src/main/webapp/... directory if your application is packaged as a jar.
registry
.addResourceHandler("/webapp/**") // « /webapp/css/style.css
.addResourceLocations("/");
// File located on disk
registry
.addResourceHandler("/system/files/**")
.addResourceLocations("file:///D:/");
}
}
Run Code Online (Sandbox Code Playgroud)
http://localhost:8080/handlerPath/resource-path+name
/static /css/myStatic.css
/webapp /css/style.css
/templates /style.css
Run Code Online (Sandbox Code Playgroud)
在 Spring 中,每个请求都将通过 DispatcherServlet。为了避免通过 DispatcherServlet(Front contoller) 请求静态文件,我们配置了 MVC静态内容。
正如@STEEL
所说的静态资源不应该通过控制器。Thymleaf
是一个ViewResolver这需要视图名称形式控制器,然后将prefix
与suffix
给视图层。
归档时间: |
|
查看次数: |
68880 次 |
最近记录: |