我的问题是如何添加像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加载失败的错误。
我的问题:
我知道这个问题上已经存在很多内容了,但是这对我来说不行,所以这就是为什么我问的。
解决方案这是我的工作方式。 只有一行就足够了。
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添加静态文件
发布评论