我想把后台查出来的数据传到jsp上没想到一直遍历出错,求大佬指点一二,把数据封装成json用js解析实在是麻烦
报错部分代码如下
19: <table border="1px">
20: <tr>
21: <c:forEach items="${page.rows}" var="item">
22: <td>${item.id}</td>
23: <%-- <c:forEach items="${item}" var="cost">
24: ${cost}
25: </c:forEach> --%>
Stacktrace:] with root cause
javax.el.PropertyNotFoundException: Property [id] not found on type [cn.craftsman18.dormitory.pojo.Cost]
at javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:260)
at javax.el.BeanELResolver$BeanProperties.access$300(BeanELResolver.java:212)
at javax.el.BeanELResolver.property(BeanELResolver.java:347)
controller代码如下
@RequestMapping("/query")
public String list(QueryVo vo, Model model) throws Exception{
//设置查询的起始记录条数
vo.setStart((vo.getPage() - 1) * vo.getSize());
//查询数据列表和数据总数
List<Cost> payList = payService.queryList();
int count = payService.queryListCount();
Page<Cost> page = new Page<Cost>();
page.setTotal(count); //数据总数
page.setSize(vo.getSize()); //每页显示条数
page.setPage(vo.getPage()); //当前页数
page.setRows(payList); //数据列表
model.addAttribute("page", page);
return "query";
}
jsp代码如下
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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=UTF-8">
<title>账单流水</title>
</head>
<body>
<br> ${page.rows}
<br> -------------分割线-------------
<table border="1px">
<tr>
<c:forEach items="${page.rows}" var="item">
<td>${item.id}</td>
</c:forEach>
</tr>
</table>
</body>
</html>
对了还有pojo
public class Cost {
private int id;
private double money;
private String type;
private String detail;
private Date time;
private String operator;
private double balance;
private boolean delFlag;
int getId() {
return id;
}
void setId(int id) {
this.id = id;
}
double getMoney() {
return money;
}
void setMoney(double money) {
this.money = money;
}
String getType() {
return type;
}
void setType(String type) {
this.type = type;
}
String getDetail() {
return detail;
}
void setDetail(String detail) {
this.detail = detail;
}
Date getTime() {
return time;
}
void setTime(Date time) {
this.time = time;
}
String getOperator() {
return operator;
}
void setOperator(String operator) {
this.operator = operator;
}
double getBalance() {
return balance;
}
void setBalance(double balance) {
this.balance = balance;
}
boolean isDelFlag() {
return delFlag;
}
void setDelFlag(boolean delFlag) {
this.delFlag = delFlag;
}
——————————极丑的分割线———————————
更新
折腾了半天还是换回老办法使用SpringMVC的ModelAndView,但是仍然报错
最后貌似在pojo里发现了什么
private int id;
int getId() {
return id;
}
方法没公开……
哎真的是学艺不精
来自stackoverflow的解答
You thus need to make sure that there is a getter method matching exactly the property name, and that the method is public (non-static) and that the method does not take any arguments and that it returns non-void. If you have one and it still doesn't work, then chances are that you was busy editing code forth and back without firmly cleaning the build, rebuilding the code and redeploying/restarting the application. You need to make sure that you have done so.
For boolean (not Boolean!) properties, the getter method name must start with is instead of get.
大意就是
需要确保有一个getter方法完全匹配属性名称,并且该方法是public(非 - static),并且该方法不接受任何参数,并且它返回非void。对于boolean(不Boolean!)属性,getter方法名称必须以开始is而不是get。
公开方法,成功显示
折腾了一下午
好好学习
好好Google