在Spring MVC中使用百里香引用.css文件 [英] reference a .css file with thymeleaf in spring mvc

查看:90
本文介绍了在Spring MVC中使用百里香引用.css文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Spring MVC和Thymeleaf做一个项目.我有一个问题,如果我具有以下文件夹结构,该如何引用我的CSS文件:

I am doing a project with spring MVC and Thymeleaf. I have a question about how I should reference my CSS files if I have this folder structure:

src
  main
    webapp
     resources
       myCssFolder
         myCssFile.css
     web-inf
       spring
       views
         myViewFolder
           index.html

我的配置类是这样的:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/css/**").addResourceLocations("/css/**");
    registry.addResourceHandler("/img/**").addResourceLocations("/img/**");
    registry.addResourceHandler("/js/**").addResourceLocations("/js/**");
    registry.addResourceHandler("/sound/**").addResourceLocations("/sound/**");
    registry.addResourceHandler("/fonts/**").addResourceLocations("/fonts/**");
}

然后我在索引文件中这样调用href:

And I call href in my index file like this:

href="resources/css/bootstrap.min.css"

但是我的页面中有些元素有些混乱,例如CSS无法正常工作.

But there are some elements that are kind of messed up in my page, for example the CSS is not working.

推荐答案

您将需要使用th:href属性来引用CSS文件.这是百里香叶教程的样本.如果百里香无法评估th:href值,则默认为href值.

You will need to use th:href attribute for referring css files. Here is a sample from thymeleaf tutorial. If thymeleaf can not evaluate th:href value, it defaults to href value.

<head>
    <title>Good Thymes Virtual Grocery</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" type="text/css" media="all"  
      href="../../css/gtvg.css" th:href="@{/css/gtvg.css}" />
 </head>

这篇关于在Spring MVC中使用百里香引用.css文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆