Thymeleaf:如何在html文本中打印从数据库中获取的值?

时间:2017-09-15 05:26:24

标签: java html spring-mvc thymeleaf

我有一些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;
    }

1 个答案:

答案 0 :(得分:1)

有多种方法可以实现这一目标。

方法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