Boot mustach如何在模型中导航Java映射
我有一张地图,上面有一个关键的产品代码和相应的产品名称。我希望能够使用映射来查找和显示使用产品密钥的产品名称。我不知道如何在小胡子上表现出来。我将地图存储在SpringMVC应用程序中的模型对象中。它必须是一个映射,但映射似乎是进行查找的最自然的方式。我不知道如何用另一个方便的结构来表示这一点。我已经显示了我的表片段。如何显示地图?有可能吗?“002022”->“香蕉”将是一个条目示例Boot mustach如何在模型中导航Java映射,java,spring-boot,mustache,Java,Spring Boot,Mustache,我有一张地图,上面有一个关键的产品代码和相应的产品名称。我希望能够使用映射来查找和显示使用产品密钥的产品名称。我不知道如何在小胡子上表现出来。我将地图存储在SpringMVC应用程序中的模型对象中。它必须是一个映射,但映射似乎是进行查找的最自然的方式。我不知道如何用另一个方便的结构来表示这一点。我已经显示了我的表片段。如何显示地图?有可能吗?“002022”->“香蕉”将是一个条目示例 @GetMapping("/products/display") public S
@GetMapping("/products/display")
public String displayProductsByCode(Model model){
Map<String,String> m = repo.findProductsByCode();
model.addAttribute("productLookup",m);
return "productDisplay";
}
<table>
<tr>
<thead>
<th> Product Code></th>
<th> Product Name </th>
</thead>
</tr>
{{# ????}}
<tr>
<td> ..I want the product code here</td>
</tr>
{{/ ???? }}
</table>
@GetMapping(“/products/display”)
公共字符串displayProductsByCode(模型){
Map m=repo.findProductsByCode();
model.addAttribute(“productLookup”,m);
返回“productDisplay”;
}
产品代码>
品名
{{# ????}}
..我要这里的产品代码
{{/ ???? }}
创建一个类,用于保存产品代码和产品名称并将其显示给视图
public class Product {
private String code;
private String name;
public Product() {
}
public Product(String code, String name) {
this.code = code;
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
在控制器中创建此产品类型的列表。如何实现获取所有产品取决于您。以下是一个例子:
@GetMapping("/product/display")
public String displayProductByCode(Map<String, Object> model){
//your logic here to retrieve products. Below simple example:
List<Product> listForModel = new ArrayList<>();
Product product1 = new Product("002022", "Banana");
Product product2 = new Product("003033", "Mango");
listForModel.add(product1);
listForModel.add(product2);
model.put("productsById", listForModel);
return "index";
}
@GetMapping(“/product/display”)
公共字符串displayProductByCode(地图模型){
//您在此处检索产品的逻辑。下面是一个简单的示例:
List listForModel=new ArrayList();
产品1=新产品(“002022”、“香蕉”);
产品2=新产品(“003033”、“芒果”);
listForModel.add(product1);
listForModel.add(产品2);
model.put(“productsById”,listForModel);
返回“索引”;
}
这是index.html文件
<table cellspacing="10px">
<tr>
<th>Product Code</th>
<th>Product Name</th>
</tr>
{{#productsById}}
<tr>
<td>{{code}}</td>
<td>{{name}}</td>
</tr>
{{/productsById}}
</table>
产品代码
品名
{{{#productsById}
{{code}}
{{name}}
{{/productsById}
谢谢你,但我已经想好了。我想知道是否有一种方法可以通过一个带有键、值对的映射来实现,但最终使用了一个对象。