java - Thymeleaf 没有看到来自 Controller 的引用

标签 java postgresql spring-boot spring-data thymeleaf

我正在尝试使用 Thymeleaf 在 html 页面上显示本地数据库 (PostgreSQL) 中的所有客户。我的项目正在使用 Spring Boot。

连接和从数据库获取数据不是这个问题的重点(或者是?)。关键是 thymeleaf 看不到我通过 Controller 传递的模型。

主页.html

!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:th="http://www.thymeleaf.org">

<head>
<title>Opera</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Opera home page'" />

<ul th:each="customer : ${customers}">
<li>
    <span th:text="${customers.getFirstName}">Name</span>
    <span th:text="${customers.getLastName}">Surname</span>
    <span th:text="${customers.getPhone}">Phone</span>
    <span th:text="${customers.getEmail}">e-mail</span>
</li>
</ul>
</body>
</html>

HomePageController.java

@Controller
@RequestMapping("/")
public class HomePageController {

    private CustomerRepository customerRepository;

    @Autowired
    public HomePageController(CustomerRepository customerRepository){
        this.customerRepository = customerRepository;
    }


    @RequestMapping(method = RequestMethod.GET)
    public String homePage(Model model) {
        List<Customer> customers = Lists.newArrayList(customerRepository.findAll());
        model.addAttribute("customers", customers);
        return "homePage";
    }
}

CustomerRepository.java

@Repository
public interface CustomerRepository extends CrudRepository<Customer, Long> {
}

客户.java

@Entity
@Getter @Setter
public class Customer {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private Long id;
    @NotNull private String firstName;
    @NotNull private String lastName;
    @NotNull private String phoneNumber;
    @NotNull private String email;

    protected Customer(){}

    public Customer(Long id, String firstName, String lastName, String phoneNumber, String email){
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.phoneNumber = phoneNumber;
        this.email = email;
    }

    @Override
    public String toString(){
        return String.format(
            "Customer[id=%d, firstName='%s', lastName='%s', phoneNumber='%s', emailr='%s']",this.getId(),
            firstName, lastName, phoneNumber, email);
    }
}

显示问题的 html 文件的屏幕截图: screen

编辑:一些英文更正:(

最佳答案

不要在模板中使用 getter。这样做:

<span th:text="${customer.firstName}">Name</span>

抱歉,您还必须删除 s

关于java - Thymeleaf 没有看到来自 Controller 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41595079/

相关文章:

java - 从服务器请求 JSON 时客户端卡住

java - 如何向 Graphics2D 对象绘制的字符串添加多种字体

java - 将另一个api的返回数据集成到服务方法中

mysql - JPA - 如何使 JSON POST 请求中不需要字段

java - CompletableFuture 如何先返回 FALSE 还是等到全部完成才返回 TRUE

java - 单元测试 - 一种方法而不是 50

regex - 为 NLP locutions 编码文本的高效 SQL 语句

mysql - 大量定时数据记录的数据库设计

postgresql - 使用 Pentaho 访问 PostgreSQL 中的非公共(public)模式

java - 在 Spring 启动项目中将实体到 DTO 转换放在哪里?