簡體   English   中英

如何使用Spring MVC和Thymeleaf添加靜態文件

[英]How to add static files using Spring MVC and Thymeleaf

我的問題是如何添加靜態文件(如CSS和圖像文件)以便可以使用它們。 我正在使用Spring MVC和Thymeleaf。 我查看了有關此主題的各種帖子,但它們沒有幫助我,所以我問。 根據這些帖子,我將CSS和圖像文件放在resources/static/cssresources/static/images directory

演示

templates下(在webapp/WEB-INF/templates )是我所有HTML文件的存儲位置,即那些想使用CSS和圖像文件的文件。

我有以下LoginApplicationConfig文件。 我包括了兩個最底層的方法,以便我的HTML文件可以使用樣式和圖像文件:

@EnableWebMvc
@Configuration
@ComponentScan({ "com.myapp.spring.*" })
@Import(value = { LoginSecurityConfig.class })
public class LoginApplicationConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware{

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Bean
      public ViewResolver viewResolver() {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(templateEngine());
        resolver.setCharacterEncoding("UTF-8");
        return resolver;
    }

    @Bean
      public TemplateEngine templateEngine() {
        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.setEnableSpringELCompiler(true);
        engine.setTemplateResolver(templateResolver());
        return engine;
    }

    private ITemplateResolver templateResolver() {
        SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        resolver.setApplicationContext(applicationContext);
        resolver.setPrefix("/WEB-INF/templates/");
        resolver.setTemplateMode(TemplateMode.HTML);
        return resolver;
    }

    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/static/css").setCachePeriod(31556926);
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/static/images").setCachePeriod(31556926);
    }
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

}

然后在我的index.html文件中,包括以下行,以便可以包括樣式文件(使用百里香):

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

但是我一直收到錯誤消息,說stylesmd.css無法加載。

我的問題:

  1. 我的樣式和圖像文件的位置是否正確。 也就是說,我應該將它們專門放置在哪些文件夾中。我嘗試了各種位置,例如webappWEB-INF目錄下的內容,但這沒有用。
  2. LoginApplicationConfig的底部兩種方法LoginApplicationConfig必需? 另外,我是在包括的內容有點糊塗addResourceHandler(...)的方法,什么addResourceLocations(...)方法。
  3. 我對樣式表的引用(使用百里香)是否正確?

我知道這個問題已經有很多內容了,但這對我沒有用,所以這就是我要問的原因。

這就是我的工作方式。 僅一行就足夠了。

registry.addResourceHandler("/static/**").addResourceLocations("/static/");

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

如果仍然失敗,請嘗試將“ templates”文件夾作為子文件夾移動到資源文件夾中。

暫無
暫無

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

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