使用Spring Boot的HTML表格中的Controller数组 [英] Use Controller array in HTML Table with Spring Boot
问题描述
我的控制器代码是:
private JdbcTemplate jdbcTemplate;
@Autowired
ConfigurationController configcon = new ConfigurationController(jdbcTemplate);
@RequestMapping(value =/)
public String index(Model model){
model.addAttribute(users,configcon.getQuery(customers)) ;
返回forward:/index.html;
}
但是如何在webapp中使用这个数组(例如用户) index.html?
我想在html表格中显示数据库值。
Please Advice。
p>谢谢。
您需要一个模板引擎。 Spring支持:
- Freemarker( list,else,items,sep,break )
- Groovy( 7. MarkupTemplateEngine )
- Thymeleaf( 6.迭代)
- Velocity( Foreach循环)
- 小胡子(非空列表)
来源: docs
这些语言允许您根据模型动态生成HTML页面。使用Thymeleaf,您可以使用 th:每个
属性遍历模型,例如:
<表>
< thead>
< tr>
< th> ID< / th>
< th>名称< / th>
< / tr>
< / thead>
< tbody>
< tr th:each =customer:$ {customers}>
< td th:text =$ {customer.id}>& nbsp;< / td>
< td th:text =$ {customer.name}>& nbsp;< / td>
< / tr>
< / tbody>
< / table>
在本例中,我循环遍历模型 $ {customers}
(因为您在控制器中将其命名),并为每个客户生成一行两列,一列用于ID,另一列用于名称。这些代表了客户类中的属性(带适当的getter / setter)。
每个模板引擎都提供了循环模型的不同方式,对于这个答案太多了。
My Controller function return array successfully.
My Controller Code is :
private JdbcTemplate jdbcTemplate;
@Autowired
ConfigurationController configcon = new ConfigurationController(jdbcTemplate);
@RequestMapping(value = "/")
public String index(Model model) {
model.addAttribute("users", configcon.getQuery("customers"));
return "forward:/index.html" ;
}
But How to use this array (For ex., users) in webapp/index.html?
I want to display the database values in html table.
Please Advice.
Thank you.
You need a templating engine for that. Spring supports:
- Freemarker (list, else, items, sep, break)
- Groovy (7. The MarkupTemplateEngine)
- Thymeleaf (6. Iteration)
- Velocity (Foreach loop)
- Mustache (Non-Empty lists)
Source: docs
These languages allow you to dynamically generate a HTML page based on your model. With Thymeleaf you could use the th:each
attribute to loop over your model, for example:
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr th:each="customer : ${customers}">
<td th:text="${customer.id}"> </td>
<td th:text="${customer.name}"> </td>
</tr>
</tbody>
</table>
In this example I'm looping over the model ${customers}
(because you named it like that in your controller), and for each customer a row is generated with two columns, one for the ID and another one for the name. These represent properties (with proper getter/setter) in your customer class.
Each of the templating engines provide a different way for looping over your model, showing them all would probably be too much for this answer.
这篇关于使用Spring Boot的HTML表格中的Controller数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!