静态文件找不到Spring MVC + Thymeleaf

时间:2016-03-04 22:43:03

标签: java css spring spring-mvc thymeleaf

我有一个spring应用程序,它是一个启动项目。我使用Thymeleaf作为模板引擎。但我有问题,我无法达到静态文件,如CSS或JavaScript。这是此应用程序的文件结构。
File structure


这是Thymeleaf引擎的配置文件。我还尝试添加资源处理:
SpringBoot with Thymeleaf - css not found
    包com.ggk.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;


@Configuration
@EnableWebMvc
@ComponentScan("com.ggk")
public class ThymeleafConfig extends WebMvcConfigurerAdapter {

    @Bean
    public ServletContextTemplateResolver servletContextTemplateResolver() {
        ServletContextTemplateResolver servletContextTemplateResolver = new ServletContextTemplateResolver();
        servletContextTemplateResolver.setPrefix("/WEB-INF/templates/");
        servletContextTemplateResolver.setSuffix(".html");
        servletContextTemplateResolver.setTemplateMode("HTML5");
        servletContextTemplateResolver.setCacheable(false);

        return servletContextTemplateResolver;
    }

    @Bean
    @Autowired
    public SpringTemplateEngine springTemplateEngine(ServletContextTemplateResolver servletContextTemplateResolver) {
        SpringTemplateEngine springTemplateEngine = new SpringTemplateEngine();
        springTemplateEngine.setTemplateResolver(servletContextTemplateResolver);
        return springTemplateEngine;
    }
    @Bean
    @Autowired
    public ThymeleafViewResolver thymeleafViewResolver(SpringTemplateEngine springTemplateEngine) {
        ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
        thymeleafViewResolver.setTemplateEngine(springTemplateEngine);
        return thymeleafViewResolver;
    }






}

这是我的index.html添加css文件。

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

2 个答案:

答案 0 :(得分:3)

我猜您也无法直接通过chrome查看静态资源,例如:

http://<domain_name>:<port>/<context_root>/css/animate.css

我认为它会给你404错误。如果是这种情况,则意味着您的应用需要配置来提供静态资源。基本上您需要在配置中添加ResourceHandler。请参阅以下链接以获取示例:

https://stackoverflow.com/a/30663486/878361

总之,您需要:

  1. 延长WebMvcConfigurerAdapter(您已经完成)
  2. 覆盖addResourceHandlers方法并添加资源位置,如下所示:

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**")
                .addResourceLocations("/resources/");
    }
    

答案 1 :(得分:1)

将css和图像移动到webapp。