java - Spring boot项目未加载资源

标签 java twitter-bootstrap spring-boot freemarker

我有具有这种结构的 Spring Boot 项目

srs-
 -main
   -java
      -MvcConfigurer.java
      -LkApplication.java
   -resource
      -static
         -css
      -templates
         -layout
           -defoultLayout.ftl
           -header.ftl
           -footer.ftl
        -index.ftl

defoultLayout.ftl 中我包含 CSS 文件

<head>
    <meta charset="utf-8">
    <link rel='stylesheet' href='../../static/css/bootstrap.min.css'>
    <link rel='stylesheet' href='../../static/css/core.css'>
</head>

但它没有加载。我有一些具有类似结构的项目,它们都已加载,但没有。

我的类(class):

@SpringBootApplication
public class LkApplication {

    public static void main(String[] args) {
        SpringApplication.run(LkApplication.class, args);
    }
}

@Configuration
@EnableWebMvc
public class MvcConfigurer extends WebMvcConfigurerAdapter {
    @Bean
    public ViewResolver viewResolver() {
        FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
        resolver.setCache(true);
        resolver.setPrefix("");
        resolver.setSuffix(".ftl");
        resolver.setContentType("text/html; charset=UTF-8");
        return resolver;
    }

    @Bean
    public FreeMarkerConfigurer freemarkerConfig() throws IOException, TemplateException {
        FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory();
        factory.setTemplateLoaderPaths("classpath:templates", "src/main/resource/templates");
        factory.setDefaultEncoding("UTF-8");
        FreeMarkerConfigurer result = new FreeMarkerConfigurer();
        result.setConfiguration(factory.createConfiguration());
        return result;
    }

    @Override
    public void configureDefaultServletHandling(
            DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

如何将资源文件加载到我的 springboot 项目中? 我使用:springboot、freemarker、bootstrap

最佳答案

如果是maven或者gradle,标准文件夹结构应该是src/main/resources 。然后,构建会将它们放入您的(假设的)可执行 spring boot jar 中。

如果您没有安全阻止任何端点,您应该能够按如下方式引用这些文件;

<link rel='stylesheet' href='/css/core.css'>

注意“静态”已从路径中删除。

关于java - Spring boot项目未加载资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38089756/

相关文章:

java - com.opensymphony.workflow.loader.SpringWorkflowFactory 的位置?

java - FindBugs插件Eclipse发现的错误

css - 我怎样才能打破网格?有这方面的普遍做法吗?

spring-boot - 如何配置Spring Boot(Kotlin)应用程序以将/重定向到/swagger-ui.html?

java - 如何清除Redis Stream中的记录,只留下N条最新记录?

java - Spring Boot 应用程序不会 Autowiring 字段

java - 在 Android Studio 中设置了错误的 JDK 路径

java - 填充 FlowLayout 的空白空间

html - 固定顶部+底部导航栏,中间有可滚动内容

twitter-bootstrap - 达到窗口高度时如何使div能够滚动