对于一个简单的webapp,我试图在jsp端打印ArrayList的值非常不直观 . 我很难将这件事拼凑在一起 .
这是一个Spring Boot应用程序,以下是我发送的响应 -
@GetMapping("/")
public String welcome(Map<String, Object> model) {
List<APojo> lis = new ArrayList<>();
APojo po = new APojo();
po.setName("Apple");
po.setName("Ball");
lis.add(po);
model.put("fruits", lis);
return "index";
}
index.jsp页面 -
<html>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html lang="en">
<head>
<title>Title goes here</title>
</head>
<body>
<c:set var = "fruitVal" value = "${fruits[0].name}"/>
<h2>Value: ${fruitVal}</h2>
</body>
</html>
我希望它可以显示Apple,但它会继续打印Ball . 为什么?
1 回答
您应该添加2个对象列表(或删除
po.setName("Ball");
):还要考虑使用接受名称的构造函数