繁体   English   中英

使用 Spring 引导在 thymeleaf 模板中打印数据时出现问题

[英]Problem printing a data in thymeleaf template with Spring boot

我无法在 thymeleaf 模板上打印字符串,该模板是从 Spring 引导 controller 传递的。 这是一个非常基本的例子。 当我运行项目时,似乎没有错误。 从 controller 的唯一方法,我在模板中传递了一个要打印的字符串,但它没有出现打印。 谁能帮我? 提前致谢。

Thymeleaf 文件

<html xmlns:th="http://www.thymeleaf.org">
<body>
<p> texto de prueba </p>
<p th:text="${dato}"></p>
<p> otro texto de prueba </p>
</body>
</html>

和 Controller Spring 引导文件

package com.jumpering.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ProductController{

@RequestMapping("/")
public String index(Model model) {

   model.addAttribute("dato", "mensaje aleatorio");
   return "index";
   }
}

和我的 pom.xml 文件

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jumpering</groupId>
<artifactId>raconet</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>raconet</name>
<description>Lo Raconet</description>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</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.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

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

我的 pom.xml 也指 DB 驱动程序,因为在项目中我将使用 MySql 数据库。

改成

@GetMapping("/")
public String index(Model model) {
   model.addAttribute("dato", "mensaje aleatorio");
   return "index";
}

, 这是我的 pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath />
    </parent>
    <groupId>com.app</groupId>
    <artifactId>example</artifactId>
    <version>1</version>
    <packaging>jar</packaging>
    <name>Example</name>

    <properties>
        <java.version>1.8</java.version>
        <bootstrap.version>4.2.1</bootstrap.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-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
    </dependencies>

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

</project>

闲逛之后,我发现问题出在应用程序的“主”文件的位置:

在此处输入图像描述

请注意,“主”文件位于“com.example.demo”package 中。 这是问题所在。 为了让它工作,文件'main'必须在'com.example'中。 我不知道为什么会发生这种情况,但这就是代码为我工作的方式。

在此处输入图像描述

它位于“主”文件的这个位置,这里的代码确实有效。

你的 html 应该在顶部

<!DOCTYPE>    
<html xmlns:th="http://www.thymeleaf.org">
    <body>
    <p> texto de prueba </p>
    <p th:text="${dato}"></p>
    <p> otro texto de prueba </p>
 </body>
 </html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM