javax.el.PropertyNotFoundException:Property 'name' not found on type java.lang.Strng
问题:jsp用el表达式遍历获取对象的属性时,老是报获取不到属性的错误
下面是代码:
Servlet代码:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
DeviceService service = new DeviceService();
List<Device> deviceList = null;
try {
deviceList = service.findAllDevice();
} catch (SQLException e) {
e.printStackTrace();
}
request.setAttribute("deviceList", deviceList);
request.getRequestDispatcher("/readblocksadd.jsp").forward(request, response);
}
前端代码:
<td class="ta_01" bgColor="#ffffff">
<select name="device_index">
<c:forEach items="${deviceList } " var="device">
<option value="${device.name}">${device.name}</option>
</c:forEach>
</select>
</td>
bean代码
private Integer name;
public Integer getName() {
return name;
}
public void setName(Integer name) {
this.name= name;
}
解决方法
弄了好一会儿终于找到问题所在,因为我使用的是eclipse,在写出${ }时,我就没注意他中间会自动有一个空格,正常情况下是不会报错的,但是当你使用forEach然后获取它的属性的时候就会报错,去掉空格就可以了。