我有一些 html 文本。在其中我想打印从数据库中获取的几个值。这是我创建的 html 表单。
<form id="deal-form"
th:object="${deal}" method="post">
<div class="border-t p-y-10">
<i class="fa fa-calendar" aria-hidden="true"></i> Duration<br/>
Ads between <span th:value = "${hotDealDetail}" th:utext="${duration}">time</span>
</div>
</form>
持续时间值是从数据库中获取的,并使用 Thymeleaf 包含在 html 文本中。这是 Controller 方法。
@ModelAttribute("hotDealDetail")
public String hotDealDetail( ModelMap model) {
model.addAttribute("deal", new Deal());
return "hot-deal-detail";
}
我没有看到任何错误。但不会打印从数据库中获取的值。我错过了什么?
编辑: 交易类
@Entity
@Table(name = "deal")
public class Deal {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
//in seconds
private double duration;
@OneToMany(mappedBy = "deal")
private List<DealEntry> dealEntries;
@Transient
private DealEntry newDealEntry;
public Deal() {
value = new BigDecimal(00.00);
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
public double getDuration() {
return duration;
}
public void setDuration(double duration) {
this.duration = duration;
}
最佳答案
有多种方法可以实现这一目标。
方法 1
尝试创建请求映射到您的 Controller 方法
@RequestMapping(value = "message", method = RequestMethod.GET)
public ModelAndView hotDealDetail() {
ModelAndView mav = new ModelAndView();
mav .addAttribute("deal", new Deal());
return mav;
}
方法2
@ModelAttribute("hotDealDetail")
public String hotDealDetail() {
return "some string without creating model";
}
方法 3
@RequestMapping(value = "hotDealDetail", method = RequestMethod.GET)
public String messages(Model model) {
model.addAttribute("hotDealDetail", new Deal());
return "hotDealDetail";
}
引用链接:http://www.thymeleaf.org/doc/articles/springmvcaccessdata.html
关于java - Thymeleaf:如何打印从数据库中获取的值到html文本中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46232204/