简体   繁体   中英

Java j2ee JSTL cannot print a list of objects

I'm currently working on a small Java J2E project.

I want to print a list of objects in a *.jsp page, but there's an error I can't seem to get rid of.

My .jsp page :

<%@ 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>

When I try to replace the ${element.Name} per ${element} , I get beans.Car@3a7488b4 , so I do get my object, just can't explore it.

My class :

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;}
}

And finally, the error stack :
1

According to Java naming conventions names of fields and methods should be in camelCase like

private String name;

public String getName(){ return name; }
public void setName(String name){ this.name=name; }

JavaBeans also follow this convention so change first letter of your fields and names to lowercase.

Oh, and after you change it don't forget to also update your EL query to use ${element.name} , not ${element.Name}


With these changes following example works fine for me:

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 view

<%@ 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM