【问题标题】:Thymeleaf: How to print a value taken from database inside html text?Thymeleaf:如何在 html 文本中打印从数据库中获取的值?
【发布时间】:2018-02-24 05:04:08
【问题描述】:

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

【问题讨论】:

    标签: java html spring-mvc thymeleaf


    【解决方案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

    【讨论】:

    • 你调试代码了吗?无论你设置了什么,你都能看到模型属性
    • 对不起,我没有。我现在还有一个问题。我也想获得相关 id 的持续时间
    • 请尝试调试不要混淆名称为什么您指定的名称相同?设置时尝试给出一些不同的名称:)参考您的交易对象的持续时间 th:utext="${deal.duration}"
    猜你喜欢
    • 2018-02-22
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    相关资源
    最近更新 更多