当前位置: 首页 > 知识库问答 >
问题:

如何从Spring启动提供静态html?

公良鸿禧
2023-03-14

我在这里运行了spring boot示例web静态项目,对pom进行了此更改

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

并添加了此类以提供来自相同静态文件夹位置的重复页面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

我做错什么了吗?

共有3个答案

时同
2023-03-14

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

Spring Boot 2.0要求Java 8作为最低版本。许多现有的API已经过更新,以利用Java 8的特性,例如:接口上的默认方法、函数回调和javax等新API。时间

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

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

@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:/");
    }
}
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所说,静态资源不应该通过Controller。胸叶是一个ViewResolver,它采用视图名称形式控制器并添加前缀后缀到视图层。

浦琪
2023-03-14

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

因此,您可以在资源/目录下创建一个静态/公共/目录,并将静态内容放在那里。它们可以通过以下方式访问:http://localhost:8080/your-file.ext。(假设server.port是8080)

您可以使用spring自定义这些目录。资源。应用程序中的静态位置。属性。

例如:

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

现在,您可以使用资源下的自定义文件夹来提供静态文件。

这也可以在Spring Boot 2中使用Java配置:

@Configuration
public class StaticConfig implements WebMvcConfigurer {

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

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

窦华晖
2023-03-14

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

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/

 类似资料:
  • 问题内容: 我正在尝试提供静态html文件,但返回500错误(.py和模板目录上有editor.html的副本),这是我尝试过的全部操作: 这是响应: 问题答案: 将其简化为最简单的方法: 将静态资产放入子文件夹。 将Flask设置为默认值,也不要设置为默认值。 通过预配置访问静态内容以验证文件是否有效 如果仍然要重用静态文件,请使用,而不要使用斜杠: 这将 直接 在文件夹内查找文件。 假定您将上

  • 我正在通过spring boot启动一个嵌入式tomcat,并希望提供一个静态索引。html页面作为正在运行的应用程序的一部分。 但以下方法不起作用: 结果:当我调用localhost:8080时,我只看到单词“index”,而没有看到我的html页面。为什么?

  • 问题内容: 我无法让Spring-boot项目提供静态内容。 我已经放在一个命名的文件夹下。在其中,我有一个名为的文件夹。当我打包应用程序并运行它时,它找不到我放在该文件夹中的图像。 我试图把静态文件中,并但没有任何工程。 如果我我可以看到文件在正确文件夹的jar中:例如,但是调用:http://localhost:8080/images/head.png,我得到的只是一个404 有什么想法为什么

  • 问题内容: 我正在尝试: 但是它继续提供我static_path中拥有的服务(如上所述,我在两个单独的路径中有两个不同的,但是我希望能够覆盖)。 问题答案: 从应用程序设置中删除。 然后将您的处理程序设置为:

  • 我试图提供一个静态资源(css文件)。 我已经注册了位置和处理程序 所以Tomcat的记录器显示到资源的正确映射 将URL路径[/resources/**]映射到类型为[类org.springframework.web.servlet.resource.ResourceHttpRequestHandler]的处理程序上 当浏览器呈现视图时,检查器显示404错误,试图获取静态资源。 应用初始化器。J

  • 问题内容: 我正在尝试在Web应用程序中提供静态资源,并且尝试了: 但是在Spring 5中不推荐使用WebMvcConfigurerAdapter。现在如何访问静态资源? 问题答案: 春季5-静态资源 从文档中: