簡體   English   中英

找不到靜態文件Spring MVC + Thymeleaf

[英]Static files can not found Spring MVC + Thymeleaf

我有一個spring應用程序,這是一個開始的項目。 我使用Thymeleaf作為模板引擎。 但是我有一個問題,就是我無法訪問CSS或javascript等靜態文件。 這是我針對該應用程序的文件結構。
檔案結構


這是Thymeleaf引擎的配置文件。 而且我還嘗試添加資源處理:
帶有Thymeleaf的SpringBoot-找不到CSS
包com.ggk.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;


@Configuration
@EnableWebMvc
@ComponentScan("com.ggk")
public class ThymeleafConfig extends WebMvcConfigurerAdapter {

    @Bean
    public ServletContextTemplateResolver servletContextTemplateResolver() {
        ServletContextTemplateResolver servletContextTemplateResolver = new ServletContextTemplateResolver();
        servletContextTemplateResolver.setPrefix("/WEB-INF/templates/");
        servletContextTemplateResolver.setSuffix(".html");
        servletContextTemplateResolver.setTemplateMode("HTML5");
        servletContextTemplateResolver.setCacheable(false);

        return servletContextTemplateResolver;
    }

    @Bean
    @Autowired
    public SpringTemplateEngine springTemplateEngine(ServletContextTemplateResolver servletContextTemplateResolver) {
        SpringTemplateEngine springTemplateEngine = new SpringTemplateEngine();
        springTemplateEngine.setTemplateResolver(servletContextTemplateResolver);
        return springTemplateEngine;
    }
    @Bean
    @Autowired
    public ThymeleafViewResolver thymeleafViewResolver(SpringTemplateEngine springTemplateEngine) {
        ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
        thymeleafViewResolver.setTemplateEngine(springTemplateEngine);
        return thymeleafViewResolver;
    }






}

這是我的index.html添加CSS文件。

<link rel="stylesheet" href="../css/animate.css" th:href="@{/css/animate.css}" type="text/css"/>

我想您也不能直接通過chrome查看靜態資源,例如:

http://<domain_name>:<port>/<context_root>/css/animate.css

我認為它給您一個404錯誤。 如果是這種情況,則意味着您的應用需要配置才能提供靜態資源。 基本上,您需要在配置中添加一個ResourceHandler 有關示例,請參見以下鏈接:

https://stackoverflow.com/a/30663486/878361

總之,您需要:

  1. 擴展WebMvcConfigurerAdapter (您已經完成)
  2. 覆蓋addResourceHandlers方法並添加資源位置,如下所示:

      @Override\n 公共無效addResourceHandlers(ResourceHandlerRegistry注冊表){\n     Registry.addResourceHandler(“ / resources / **”)\n             .addResourceLocations(“ / resources /”);\n }\n

將CSS和圖像移至webapp。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM