2017-09-15 207 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

相关问题