简体   繁体   中英

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

I have some html text. Inside it I want to print several values taken from the database. This is the html form I have created.

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

Duration value is taken from the database and included inside html text using Thymeleaf. This is the controller method.

@ModelAttribute("hotDealDetail")
    public String hotDealDetail( ModelMap model) {
        model.addAttribute("deal", new Deal());
    return "hot-deal-detail";
}

I see no errors. But the values taken from the database is not printed. What am I missing?

edit: deal class

@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;
    }

There are multiple ways you can achieve that.

Approach 1

Try creating request mapping to your controller method

@RequestMapping(value = "message", method = RequestMethod.GET)
public ModelAndView hotDealDetail() {
    ModelAndView mav = new ModelAndView();
    mav .addAttribute("deal", new Deal());
    return mav;
}

Approach 2

@ModelAttribute("hotDealDetail")
public String hotDealDetail() {
    return "some string without creating model";
}

Approach 3

@RequestMapping(value = "hotDealDetail", method = RequestMethod.GET)
public String messages(Model model) {
    model.addAttribute("hotDealDetail", new Deal());
    return "hotDealDetail";
}

Ref Link : http://www.thymeleaf.org/doc/articles/springmvcaccessdata.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM