如何使用Spring MVC和Thymeleaf添加静态文件

编程入门 行业动态 更新时间:2024-10-24 12:33:16
本文介绍了如何使用Spring MVC和Thymeleaf添加静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的问题是如何添加像CSS和图像文件的静态文件,以便我可以使用它们。我使用Spring MVC和Thymeleaf。我看了关于这个问题的各种帖子,但他们没有帮助我,所以我问。根据这些帖子,我把我的CSS和图像文件在 resources / static / css 和 resources / static / images目录。

在模板(在 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 /static/css\").setCachePeriod(31556926); registry.addResourceHandler(/ resources / **)。addResourceLocations(/ resources / static / images)。setCachePeriod(31556926); } @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){ configurer.enable(); } }

然后在我的index.html文件,我包括了以下行,所以我可以包括样式文件(使用thymeleaf):

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

但我一直得到stylesmd.css加载失败的错误。

我的问题:

  • 我的样式和图像文件的位置是否正确。也就是说,我应该具体放置哪些文件夹。我尝试了各种位置,如 webapp 和 WEB-INF 此外,我有点困惑包括在 addResourceHandler(...)方法和在 addResourceLocations(...)
  • 是否引用样式表(使用thymeleaf)是否正确?
  • b $ b

    我知道这个问题上已经存在很多内容了,但是这对我来说不行,所以这就是为什么我问的。

    解决方案

    这是我的工作方式。 只有一行就足够了。

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

    < head> < link rel =stylesheettype =text / csshref =/ static / css / your.cssth:href =@ {/ static / css / your.css}/> ; < / head>

    如果它仍然失败,请尝试将templates文件夹作为子文件夹移动到resources文件夹。

    My question is how to add static files like CSS and image files so that I can use them. I am using Spring MVC and Thymeleaf. I looked at various posts on this subject, but they did't help me, so I am asking. As per those posts, I put my CSS and image file in the resources/static/css and resources/static/images directory.

    Under templates (under webapp/WEB-INF/templates) is where all my HTML files are stored, the ones who want to use the CSS and image files.

    I have the following LoginApplicationConfig file. The very two bottom methods I included so that my HTML files could use the styles and image files:

    @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(); } }

    Then in my index.html file, I included the following line so I could include the style file (using thymeleaf):

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

    But I keep getting the error that the stylesmd.css failed to load.

    My questions:

  • Is the placement of my styles and image files correct. That is, what folders should I specifically put them in. I tried various location like under the webapp and WEB-INF directory but that didn't work.
  • Are the bottom two methods in LoginApplicationConfigrequired? In addition, I was a bit confused on what to include in the addResourceHandler(...) method and what in the addResourceLocations(...) method.
  • Is my reference to the style sheet (using thymeleaf) correct?
  • I am aware a lot of content already exists on this question, but that did't work for me, so that's why I am asking.

    解决方案

    This is how I made it work. Only one line suffice.

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

    And

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

    If it keeps failing, try to move your "templates" folder into the resources folder as a subfolder.

    更多推荐

    如何使用Spring MVC和Thymeleaf添加静态文件

    本文发布于:2023-11-24 21:16:10,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1626861.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:如何使用   静态   文件   MVC   Spring

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!