使用 Spring 和 Thymeleaf 的静态资源 [英] Static resources using Spring and Thymeleaf
问题描述
我正在尝试使用以下代码访问 HTML 上的静态资源:
I'm trying to access my static resources on my HTML using the following code:
<link th:href="@{css/main.css}" rel="stylesheet" type="text/css" />
但是当我放置 @{static/css/main.css}
时才有效.我知道设置resources文件夹的时候不需要每次调用静态文件都设置static文件夹.
But just works when I put @{static/css/main.css}
. I know that when you set the resources folder, you don't need to set the static folder every time when call a static file.
我的文件夹结构:
/webapp
=== /static
==========/css
==========/js
=== /WEB-INF
==========/views
在 Spring 上设置 mvc 配置:
Setting on Spring the mvc configs:
....
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Bean
public ViewResolver viewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
resolver.setCharacterEncoding("UTF-8");
return resolver;
}
private TemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setTemplateResolver(templateResolver());
return engine;
}
private ITemplateResolver templateResolver() {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setApplicationContext(applicationContext);
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".html");
resolver.setCacheable(false); // On production , turn TRUE
resolver.setTemplateMode(TemplateMode.HTML);
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("/static/");
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
我使用的是 Spring 4 和 Thymeleaf 3 测试版.我使用的每个 css-js-image 文件都需要在路径上写上静态".如果不编写完整路径,这种编码方式将无法使用.为什么?
I'm using Spring 4 and Thymeleaf 3 beta. Every css-js-image file that I'm using I need to write 'static' on the path. This way that are coded don't work to use without write the full path. Why?
推荐答案
registry.addResourceHandler("/static/**").addResourceLocations("/static/");
^^^^^^^^ ^^^^^^^
----------- These two are different ------
因为您告诉 spring mvc 为您的类路径中 /static
文件夹中带有 /static/
路径前缀的每个请求提供服务.因此,当您向 static/css/main.css
发出请求时,它将与您的资源处理程序路径匹配并成功提供服务.
Because you're telling spring mvc to serve every request with /static/
path prefix from /static
folder in your classpath. So, when you fire a request to static/css/main.css
, it will be matched with your resource handler path and will be served successfully.
我知道设置resources文件夹的时候不需要设置每次调用静态文件时都在静态文件夹中.
I know that when you set the resources folder, you don't need to set the static folder everytime when call a static file.
我的猜测是您将 /static/**
路径前缀与 /static
文件夹名称混淆了.@{static/css/main.css}
中的 static
引用您定义的 /static/**
路径前缀:
My guess is you're confusing the /static/**
path prefix with /static
folder name. static
in @{static/css/main.css}
is referencing to /static/**
path prefix you defined in:
registry.addResourceHandler("/static/**")...
不是文件夹名称:
...addResourceLocations("/static/")
例如,如果您像下面这样定义资源处理程序:
For example, if you define your resource handler like following:
registry.addResourceHandler("/content/**").addResourceLocations("/static/");
然后您应该将您的请求发送到 content/css/main.css
.
Then you should send your request to, say, content/css/main.css
.
更新:如果你坚持使用 css/main.css
作为你的路径,你应该像这样定义你的资源处理程序:
Update: If you insist to use css/main.css
as your path, you should define your resource handler like this:
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
并将您的 /static/
文件夹放在 src/main/resources
中.
And also put your /static/
folder in src/main/resources
.
这篇关于使用 Spring 和 Thymeleaf 的静态资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!