我有一些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文本中。这是控制器方法。
@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;
}
发布于 2017-09-15 14:25:02
有多种方法可以实现这一点。
方法1
尝试创建到控制器方法的请求映射
@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
https://stackoverflow.com/questions/46232204
复制相似问题