我目前正在开发一个小型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.Name}
替换为${element}
时,我得到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;}
}
最后,错误堆栈:
答案 0 :(得分:1)
根据Java命名约定,字段和方法的名称应该在camelCase中,如
private String name;
public String getName(){ return name; }
public void setName(String name){ this.name=name; }
JavaBeans也遵循此约定,因此将字段和名称的第一个字母更改为小写。
哦,更改之后不要忘记更新您的EL查询以使用${element.name}
,而不是${element.Name}
通过这些更改,下面的示例对我来说很好:
Car.java
JavaBean
package whatever;
public class Car {
private String name;
private String brand;
// Getters and Setters
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 Car(String name, String brand) {
this.name = name;
this.brand = brand;
}
@Override
public String toString() {
return "Car [Name=" + name + ", Brand=" + brand + "]";
}
}
CarTest
servlet
@WebServlet("/CarTest")
public class CarTest extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Car> list = new ArrayList<Car>();
list.add(new Car("car1", "brand1"));
list.add(new Car("car2", "brand2"));
list.add(new Car("car3", "brand3"));
list.add(new Car("car4", "brand4"));
request.setAttribute("cars", list);
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
}
index.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" %>
<%@ page import="java.util.List" %>
<body>
<h2>Car List :</h2>
<ul>
<c:forEach items="${requestScope.cars}" var="element">
<li>${element.name }</li>
</c:forEach>
</ul>
</body>
</html>