簡體   English   中英

Thymeleaf:如何在html文本中打印從數據庫獲取的值?

[英]Thymeleaf: How to print a value taken from database inside html text?

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

嘗試創建請求映射到您的控制器方法

@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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM