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

标签 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 文本中。这是 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/

相关文章:

java - Spring MVC 3 : Define own Datatype-Binding

java - 使用 DAO 进行事务管理

java - Java 如何在其 16 位 char 类型中存储 UTF-16 字符?

java - 动态添加组件并调用repaint/validate/revalidate

javascript - 在 .html() 标签内格式化 html

java - @事务: Is there a way that hibernate can persist child transacttion when parent fails

java - 并发实践 - volatile++

javascript - 我们可以调用两个函数 onClick 事件吗

asp.net - 以漂亮的布局显示记录

java - 集成 springMVC 和 extjs