【发布时间】:2015-03-28 21:44:40
【问题描述】:
我目前正在从事一个小型 Java J2E 项目。
我想在 *.jsp 页面中打印一个对象列表,但是有一个我似乎无法摆脱的错误。
我的 .jsp 页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Cars</title>
<link type="text/css" rel="stylesheet" href="Styles/style.css" />
</head>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ include file="/WEB-INF/header.html" %>
<%@ include file="/WEB-INF/Menus/menu-logged_in.jsp" %>
<%@ page import="java.util.List" %>
<%@ page import="com.SupTracking.beans.Car" %>
<body>
<h1>Cars</h1>
<p>
You currently have <c:out value="${requestScope.nbcar }"></c:out> car(s) registered.<br/>
<a href="/SupTracking/cars/newcar">Register a new car</a>
</p>
<c:if test="${requestScope.cars != null }">
<h2>Car List :</h2>
<ul>
<c:forEach items="${requestScope.cars}" var="element">
<li>${element.Name }</li>
</c:forEach>
</ul>
</c:if>
</body>
</html>
当我尝试用${element} 替换${element.Name} 时,我得到beans.Car@3a7488b4,所以我确实得到了我的对象,只是无法探索它。
我的班级:
public class Car {
private int CarID;
private int UserID;
private String Name;
private String Brand;
private int DateRegistration;
private Float CarLatitude;
private Float CarLongitude;
private Timestamp CarTimestamp;
//Getters and Setters
public void SetCarID(int id) {this.CarID = id;}
public int GetCarID() {return this.CarID;}
public void SetUserID(int id) {this.UserID = id;}
public int GetUserID() {return this.UserID;}
public void SetName(String n) {this.Name = n;}
public String GetName() {return this.Name;}
public void SetBrand(String b) {this.Brand = b;}
public String GetBrand() {return this.Brand;}
public void SetDateRegistration(int d) {this.DateRegistration = d;}
public int GetDateRegistration() {return this.DateRegistration;}
public void SetCarLatitude(Float lat) {this.CarLatitude = lat;}
public Float GetCarLatitude() {return this.CarLatitude;}
public void SetCarLongitude(Float lon) {this.CarLongitude = lon;}
public Float GetCarLongitude() {return this.CarLongitude;}
public void SetCarTimeStamp (Timestamp t) {this.CarTimestamp = t;}
public Timestamp GetCarTimestamp() {return this.CarTimestamp;}
}
最后,错误堆栈:
【问题讨论】:
-
考虑更改字段名称和方法以遵循 bean 约定,因此它们应该以小写开头,例如
CarID->carID。SetCarID->setCarID.... -
哎呀,我的错,我在写这篇文章的时候已经改变了。也就是说,即使使用 getName(),错误仍然存在
-
我刚刚用这些更改测试了你的代码,它似乎解决了你的问题。
标签: java jsp jakarta-ee jstl