如何使用Spring MVC和Thymeleaf添加静态文件 [英] How to add static files using Spring MVC and 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
- 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
andWEB-INF
directory but that didn't work. - Are the bottom two methods in
LoginApplicationConfig
required? In addition, I was a bit confused on what to include in theaddResourceHandler(...)
method and what in theaddResourceLocations(...)
method. - Is my reference to the style sheet (using thymeleaf) correct?
我知道这个问题上已经存在很多内容了,但是这对我来说不行,所以这就是为什么我问的。
这是我的工作方式。
只有一行就足够了。
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:
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添加静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!