相关疑难解决方法(0)

使用Thymeleaf在Spring Boot中加载静态资源

我想使用页面thymeleaf.但我对静态文件有一些问题.我研究了问题(1,2,3有类似的问题),但它并不能帮助我.

Spring Boot在应用程序中使用了一个框架.我的文件看起来像: 在此输入图像描述

的test.html

<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <script src="js/test.js" th:src="@{/test.js}"/>
</head>
<body>
<button onclick="testFunction('test value')">Button</button>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

test.js

function testFunction(test) {
    console.log(test);
}
Run Code Online (Sandbox Code Playgroud)

配置类

@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/js/");
        super.addResourceHandlers(registry);
    }
}
Run Code Online (Sandbox Code Playgroud)

和问题,当我加载test.html文件没有加载javascript.

@GetMapping(value = "web/test")
public String getTestHtmlPage() {
    return "test";
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

/api/v1 是一个配置 application.properties => server.servlet-path=/api/v1

我做错了什么?你能帮助我吗?

谢谢大家!

java spring thymeleaf spring-boot

6
推荐指数
1
解决办法
9689
查看次数

Spring-Boot + Spring-MVC + Thymeleaf + Apache Tiles

我已经有一个带有apache tile和thymeleaf的客户端模块,效果很好。我想将其转换为spring-boot,并希望逐步进行,但是我真的坚持使用它。我不想改变太多,当有人可以告诉我,我应该首先做什么并使其运行时,我会喜欢的。我已经尝试过在javaConfig中编写servlet,但是我也陷入了困境。也许有人可以帮我。如果需要更多信息,请随时询问。

另一个问题是,我需要从xml更改为javaconfig吗?我更喜欢最简单的方法。但是,一旦我将spring-starter依赖项添加到pom中,应用程序就不再起作用。

在此处输入图片说明

=======

POM:

<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>

<parent>
    <groupId>at.compax.bbsng</groupId>
    <artifactId>bbsng-client</artifactId>
    <version>0.1.0-SNAPSHOT</version>
</parent>

<artifactId>bbsng-client-mvc</artifactId>
<name>bbsng-client-mvc</name>
<packaging>war</packaging>

<properties>
    <org.apache.tiles-version>2.2.2</org.apache.tiles-version>
    <org.thymeleaf-version>2.0.16</org.thymeleaf-version>
    <slf4j-version>1.7.5</slf4j-version>
    <jackson.version>1.9.10</jackson.version>
</properties>

<dependencies>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <!-- Apache Tiles -->
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-template</artifactId>
        <version>${org.apache.tiles-version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-servlet</artifactId>
        <version>${org.apache.tiles-version}</version>
    </dependency>

    <!-- ThyMeLeaf ... -->
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf</artifactId>
        <version>${org.thymeleaf-version}</version>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring3</artifactId>
        <version>${org.thymeleaf-version}</version>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-tiles2</artifactId>
        <version>2.0.0</version>
    </dependency>

    <!-- Jackson JSON Mapper -->
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>${jackson.version}</version> …
Run Code Online (Sandbox Code Playgroud)

spring-mvc apache-tiles thymeleaf spring-boot spring-java-config

4
推荐指数
1
解决办法
2万
查看次数