2017-09-15 86 views
0

我有一些html文本。在裏面我想打印從數據庫中取得的幾個值。這是我創建的html表單。Thymeleaf:如何在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

有可以實現多種方式。

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

+0

這些都不是工作 – sndu

+0

你有沒有調試代碼您能燒開任何您已設置 – Pradeep

+0

對不起,我沒有模型屬性?我現在有另一個問題。我想獲得相關ID的這個持續時間 – sndu

相關問題