0

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

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Did you debug the code?Are you able to seethe model attribute whatever you have set
sorry I didn't. I have another question now. I want to get this duration for the relevant id as well
Please try debugging Don't mess with names why your specifying names as identical ? Try giving some different names while setting :)To refer to your Deal Object's duration th:utext="${deal.duration}"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.