【问题标题】:Thymeleaf how to use model atrribute correctly its throwing null errorThymeleaf 如何正确使用模型属性其抛出空错误
【发布时间】:2020-11-27 23:44:07
【问题描述】:
public class Customer {
@Id
@GeneratedValue
private Integer id;

private String email;

private Long Apprvamount;

@JsonIgnore
@OneToMany(mappedBy="customer", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Set<MyTransaction> transactions;

@Transient
private Long rewardPoints;
public Long getRewardPoints() {
    function to calculate rewardPoints;
}

控制器添加模型属性

@GetMapping("/customers/{id}")
public String getCustomer(@PathVariable Integer id,Model model) throws RecordNotFoundException {
    Customer customer = rewardsService.getCustomerById(id);
    model.addAttribute("customer", customer);
    return "profile";
}

百里香

<body>
   <div class="card-body">
        <h2 th:text="${customer.getEmail()}"></h2>
        <p th:text="${customer.getApprvamount()}"></p>
        <p th:text="${customer.getRewardPoints()}"></p>
        <p class="my-5">
            <a href="/{id}(id=${customer.getId()})/emi" class="btn btn-primary">
                <i> All</i></a>
        </p>

它有时会为 getRewardPoints 抛出 null 错误,它适用于电子邮件和 approvamount。并且 url 没有得到计算。存在获取器和设置器。这种方法有什么问题?

【问题讨论】:

  • 你到底想在href上穿什么。应该怎么样?

标签: spring-boot thymeleaf rest


【解决方案1】:

您可能拼错了客户:

model.addAttribute("costumer", customer);

【讨论】:

  • 感谢现在电子邮件部分正在工作,但 url 和 rewardPoints 不工作
  • 在您的描述中,您仍然拼写“客户”而不是“客户”。
  • 网址完全是灾难。有关链接 URL,请参阅 Thymeleaf 参考。它应该类似于:&lt;a href="@{/{id}/emi(id=${customer.id})}" class="btn btn-primary"&gt;&lt;i&gt;All&lt;/i&gt;&lt;/a&gt;
  • 非常感谢。这是你提到的语法错误。现在 url 正在工作。但仍然 rewardPoints 抛出错误。正确的方法是什么?
  • rewardPoints 是 Long 并且可能设置为 null。在这种情况下,你需要这样的东西:Using Thymeleaf when the value is null
猜你喜欢
  • 2020-06-27
  • 2013-07-24
  • 2018-11-06
  • 2016-07-12
  • 2019-01-03
  • 1970-01-01
  • 2012-02-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多