【问题标题】:Thymeleaf does not resolve model via spring boot and controllerThymeleaf 不通过 Spring Boot 和控制器解析模型
【发布时间】:2017-02-02 22:17:36
【问题描述】:

我想玩一下 Spring Boot 和 Thymeleaf。因此,我通过 Spring Initialzr 加载了我的 Spring Boot 应用程序并将 Web 和 Thymeleaf 设置为依赖项。 可悲的是,我无法解析任何动态 html,只有静态的。

当请求 localhost:8080/ 我收到这个:The message is:${message}

将提供一些代码来展示我的设置(尽可能少的代码)

WebApp.java

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

import java.util.Arrays;

@SpringBootApplication
public class WebApp {

  public static void main(String[] args) {
    ApplicationContext ctx = SpringApplication.run(WebApp.class, args);
  }
}

HelloController.java

package com.example;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.method.annotation.ModelFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;

@Controller
public class HelloController {

  @RequestMapping("/")
  public ModelAndView handler() {
    ModelAndView mav = new ModelAndView("twitter");
    mav.addObject("message", "message");
    return mav;
  }
}

twitter.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"> </meta>
    <title>Test</title>
</head>
<body>
The message is:${message}

</body>
</html>

pom.mxl

<?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>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </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-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>1.4.1.RELEASE</version>
        </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>

【问题讨论】:

    标签: java maven spring-boot thymeleaf


    【解决方案1】:

    我猜你必须改变你的看法:

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="UTF-8"> </meta>
            <title>Test</title>
        </head>
        <body>
            The message is: <span th:text="${message}"></span>
        </body>
    </html>
    

    【讨论】:

    • 等等...不要告诉我“th”是百里香叶首字母缩略词的标签库。我一直认为它代表 html 使用的“table-header”。我对 HTML tbh 的经验很少。
    • 是的,我读到了,但是由于它在示例中使用了表格,因此我不知道“th”是百里香特定的关键字
    猜你喜欢
    • 2020-03-14
    • 1970-01-01
    • 2016-01-27
    • 2021-09-25
    • 2020-10-07
    • 1970-01-01
    • 2018-11-30
    • 2019-06-11
    • 2017-04-09
    相关资源
    最近更新 更多