在spring-boot中无法通过thymeleaf库找到静态资源

时间:2018-07-09 18:20:43

标签: spring-boot thymeleaf

我想对Thymeleaf的使用方法进行快速测试,请查看我的两个屏幕截图:

enter image description here

home.html在我的templates /目录下,当我访问http://localhost:8090/时可以正确显示。但是,它无法链接到home.html文件底部的两个静态文件:

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="js/home.js"></script>
<link href="css/home.css" rel="stylesheet" type="text/css"/>

请参阅错误消息: enter image description here

除了弹簧配置之外,我没有特定的百里香叶配置:

## web
server.port=8090

## velocity
spring.velocity.allowRequestOverride=false
spring.velocity.allowSessionOverride=false
spring.velocity.cache=false
spring.velocity.checkTemplateLocation=true
spring.velocity.contentType=text/html
spring.velocity.toolbox-config-location=/WEB-INF/toolbox.xml
spring.velocity.exposeRequestAttributes=false
spring.velocity.exposeSessionAttributes=false
spring.velocity.exposeSpringMacroHelpers=false
spring.velocity.resourceLoaderPath=/WEB-INF/templates/
spring.velocity.suffix=.vm
spring.velocity.templateEncoding=UTF-8
spring.velocity.charset=UTF-8
spring.velocity.properties.input.encoding=UTF-8
spring.velocity.properties.output.encoding=UTF-8
spring.velocity.requestContextAttribute=rc

spring.mvc.favicon.enabled = false

logging.config=classpath:log4j2.xml

还有我的控制器:

@RequestMapping("/")
    public String welcome(Model model) {
        LOGGER.info("Run Successfully");
        return "home";
    }

我的主班:

@SpringBootApplication
@Import({WebConfigurer.class})
@ComponentScan("com.jdd.jnlu.qe")
@PropertySource({"classpath:boot.properties", "classpath:important.properties", "classpath:jss.properties",
        "classpath:system.properties", "classpath:ump.properties"})
@EnableAutoConfiguration
public class JnluQEWebStart extends SpringBootServletInitializer {

    public static void main(String[] args) {

        SpringApplication.run(JnluQEWebStart.class, args);
    }
}

1 个答案:

答案 0 :(得分:0)

由于您正在使用Thymeleaf,请尝试以下操作:

<script th:src="@{/js/home.js}"></script>
<link th:href="@{/css/home.css}" rel="stylesheet" type="text/css"/>

别忘了在您的页面中注册Thymeleaf:

<html xmlns:th="http://www.thymeleaf.org">