【问题标题】:'spring.thymeleaf.servlet.content-type' is an unknown property'spring.thymeleaf.servlet.content-type' 是一个未知属性
【发布时间】:2018-06-09 09:11:26
【问题描述】:

我正在运行我的第一个 spring 项目,我正在使用 sts 作为 ide,我想将 Thymeleaf 用于页面模板,但是 th:href 链接不能作为链接,而是纯文本,但我可以'找不到我想要的页面,无法重定向到 thymeleaf 页面:

#==================================
# = JSP Configuration
#==================================
#spring.mvc.view.prefix: /WEB-INF/views/
#spring.mvc.view.suffix: .jsp
#
#==================================
# = Thymeleaf configurations
#==================================
#spring.thymeleaf.check-template-location=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.cache=false
spring.thymeleaf.encoding=UTF-8
server.servlet.context-path=/
#
#==================================
# = Datasource configuration
#==================================
spring.jpa.database=POSTGRESQL
spring.datasource.platform=postgres
#spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.jpa.show-sql=true
logging.level.org.hibernate.SQL=debug
logging.level.org.hibernate.type.descriptor.sql=trace
logging.level.org.springframework.web=INFO
#spring.jpa.hibernate.ddl-auto=create
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost/siw
spring.datasource.username=postgres
spring.datasource.password=postgres

spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
#
#==================================
# = Webserver configuration
#==================================
server.port= 8080 
#
#==================================
# = Misc configuration
#==================================
spring.messages.basename=messages/validation
#
#==================================
# = Security configuration
#==================================
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration
spring.security.user.name=paolo
spring.security.user.password=paolo

这是我用来通过 maven 查找和解决库依赖关系的 Pom:

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

    <groupId>it.uniroma3</groupId>
    <artifactId>progettoSIW</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>progettoSIW</name>
    <description>project for SIW</description>

    <!--    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
        <relativePath /> 
    </parent>
    -->

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
        <relativePath/>
    </parent>


    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

如何修复它,我在 html 中使用 this 来指定使用 thymeleaf: xmlns:th="http://www.thymeleaf.org"

附:如果我使用 .../resource/templates 作为索引的位置,我找不到它;我需要把它放在.../resources/static

【问题讨论】:

    标签: java spring maven spring-boot thymeleaf


    【解决方案1】:

    添加 th:ref 如下:

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body><div class="starter-template">
        <h1>Spring Boot Web Thymeleaf Example</h1>
        <h2>
            <span th:text="'Message: ' + ${message}"></span>
            <a th:href="@{${link}}">view</a>
           <!-- <span th:link="${link}" th:text="${link}"></span>-->
        </h2>
    </div>
    </body>
    </html>
    

    请参考here中的工作代码

    关于您的其他问题,设置自定义模板位置,您需要像这样修改 application.properties 文件

    spring.thymeleaf.check-template=true
    spring.thymeleaf.check-template-location=true
    spring.thymeleaf.enabled=true
    spring.thymeleaf.prefix=classpath:/static/
    spring.thymeleaf.suffix=.html
    

    【讨论】:

    • 这并没有解决我的问题,如果页面是静态的,则不会被读取
    • 请看修改后的答案
    猜你喜欢
    • 2021-09-24
    • 2021-12-07
    • 2017-10-19
    • 2018-08-28
    • 2023-01-04
    • 2018-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多