使用 jstl foreach 遍历包含字符串和字符串列表的映射
Iterating over Map containing String and List of String using jstl foreach
我正在尝试遍历包含手机列表和 prices.I 列表的地图,我在 jsp 页面中获取手机和价格,但无法以正确的格式生成输出。我想要输出如下
手机 1
价格
手机 2
价格
但我得到了
移动
价格价格
控制器代码
@RequestMapping(value="/demo")
public ModelAndView demo() {
ModelAndView model=new ModelAndView("demo");
Map<String, List<String>> map = new HashMap<String, List<String>>();
List<String> phone = new ArrayList<String>();
List<String> price=new ArrayList<String>();
phone.add("iphone");
phone.add("nokia");
price.add("70000");
price.add("20000");
map.put("mobile",phone);
map.put("price",price);
model.addObject("student", map);
return model;
}
jsp代码
<c:forEach var="s" items="${student}">
<c:forEach var="s1" items="${s.value }" varStatus="loop">
${s1}
</br>
</c:forEach>
<br/>
</c:forEach>
希望对您有所帮助
创建一个 POJO Bean
public class ItemModel{
private String mobile;
private int price;
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile= mobile;
}
public void setPrice(int price) {
this.price= price;
}
public int getPrice() {
return price;
}
}
修改您的控制器部分
@RequestMapping(value="/demo")
public ModelAndView demo() {
ModelAndView model=new ModelAndView("demo");
List<ItemModel> itemList = new ArrayList<ItemModel>();
ItemModel item1 = new ItemModel();
item1.setMobile("iphone");
item1.setPrice(55000);
ItemModel item2 = new ItemModel();
item2.setMobile("nokia");
item2.setPrice(20000);
itemList.add(item1);
itemList.add(item2);
model.addObject("student", itemList);
return model;
}
客户端:
<c:forEach var="s" items="${student}">
Mobile=${s.mobile}
Price=${s.price}
</c:forEach>
控制器中的 answer.code 将保持不变。
jsp代码。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<%@ page isELIgnored="false" %>
</head>
<body>
<c:set var="i" value="0" scope="page" />
<c:forEach var="item" items="${student['mobile']}" varStatus="status">
<div>
<c:out value="${student['mobile'][i]}"></c:out>
${student['price'][i]};
<c:set var="i" value="${i+1}" scope="page" />
</div>
</c:forEach>
<body>
<html>
迭代地图的简单方法(我已经测试过这段代码)
<c:forEach items="${model}" var="element">
<p>Key</p> <c:out value="${element.key}"></c:out>
<p>Value</p><c:out value="${element.value}"></c:out>
</c:forEach>
在 servlet/controller 中(对于 spring 进行相应更改)
HashMap<Integer, String> map = new HashMap<Integer,String>();
map.put(1, "Java");
map.put(2, "for");
map.put(1, "fun");
request.setAttribute("model", map);
我正在尝试遍历包含手机列表和 prices.I 列表的地图,我在 jsp 页面中获取手机和价格,但无法以正确的格式生成输出。我想要输出如下
手机 1
价格
手机 2
价格
但我得到了
移动
价格价格
控制器代码
@RequestMapping(value="/demo")
public ModelAndView demo() {
ModelAndView model=new ModelAndView("demo");
Map<String, List<String>> map = new HashMap<String, List<String>>();
List<String> phone = new ArrayList<String>();
List<String> price=new ArrayList<String>();
phone.add("iphone");
phone.add("nokia");
price.add("70000");
price.add("20000");
map.put("mobile",phone);
map.put("price",price);
model.addObject("student", map);
return model;
}
jsp代码
<c:forEach var="s" items="${student}">
<c:forEach var="s1" items="${s.value }" varStatus="loop">
${s1}
</br>
</c:forEach>
<br/>
</c:forEach>
希望对您有所帮助
创建一个 POJO Bean
public class ItemModel{
private String mobile;
private int price;
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile= mobile;
}
public void setPrice(int price) {
this.price= price;
}
public int getPrice() {
return price;
}
}
修改您的控制器部分
@RequestMapping(value="/demo")
public ModelAndView demo() {
ModelAndView model=new ModelAndView("demo");
List<ItemModel> itemList = new ArrayList<ItemModel>();
ItemModel item1 = new ItemModel();
item1.setMobile("iphone");
item1.setPrice(55000);
ItemModel item2 = new ItemModel();
item2.setMobile("nokia");
item2.setPrice(20000);
itemList.add(item1);
itemList.add(item2);
model.addObject("student", itemList);
return model;
}
客户端:
<c:forEach var="s" items="${student}">
Mobile=${s.mobile}
Price=${s.price}
</c:forEach>
控制器中的 answer.code 将保持不变。
jsp代码。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<%@ page isELIgnored="false" %>
</head>
<body>
<c:set var="i" value="0" scope="page" />
<c:forEach var="item" items="${student['mobile']}" varStatus="status">
<div>
<c:out value="${student['mobile'][i]}"></c:out>
${student['price'][i]};
<c:set var="i" value="${i+1}" scope="page" />
</div>
</c:forEach>
<body>
<html>
迭代地图的简单方法(我已经测试过这段代码)
<c:forEach items="${model}" var="element">
<p>Key</p> <c:out value="${element.key}"></c:out>
<p>Value</p><c:out value="${element.value}"></c:out>
</c:forEach>
在 servlet/controller 中(对于 spring 进行相应更改)
HashMap<Integer, String> map = new HashMap<Integer,String>();
map.put(1, "Java");
map.put(2, "for");
map.put(1, "fun");
request.setAttribute("model", map);