簡體   English   中英

Thymeleaf無法通過Spring Boot和控制器解析模型

[英]Thymeleaf does not resolve model via spring boot and controller

我想和Spring Boot和Thymeleaf一起玩。 因此,我通過Spring Initialzr加載了Spring Boot App,並將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>

我想你必須改變看法:

<!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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM