在Spring Boot中添加JSF依赖项会阻止从文件系统读取静态资源

时间:2018-09-02 03:21:22

标签: spring-boot jsf

这是我的项目结构(MWE):

.
├── build.gradle
└── src
    ├── main
    │   └── java
    │       └── af
    │           └── aps
    │               ├── Application.java
    │               └── config
    │                   └── MVCConfig.java
    └── public
        └── index.html

build.gradle

ext.appVersion = '0.0.1'

buildscript {
    ext.springBootVersion = '2.0.4.RELEASE'
    repositories {
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'war'

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web') // mvc

    // compile group: 'com.sun.faces', name: 'jsf-api', version: '2.2.18'
    // compile group: 'com.sun.faces', name: 'jsf-impl', version: '2.2.18'
}

Application.java

package app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
    }

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

MVCConfig.java

package app.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MVCConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("file:/home/hamid/workspace/java/app/src/public/")
                .setCachePeriod(0);
    }
}

index.html

<!DOCTYPE html>
<html>
<head>
    <title>Home page</title>
</head>
<body>The home page</body>
</html>

我正在尝试从文件系统提供静态资源。确实有效。我可以看到主页,当我对其进行更改并刷新浏览器时,可以看到所做的更改,但是当我将JSF库添加到我的项目中时,出现了404错误,找不到静态资源。我已经尽一切可能用Google搜索并搜索了文档,但我不知道出了什么问题。

1 个答案:

答案 0 :(得分:0)

文件夹src/public不在类路径中,因此它的内容不能由spring-boot提供。

只需将src/public移至src/main/resources/public,一切都将正常工作。