简体   繁体   English

Java j2ee JSTL无法打印对象列表

[英]Java j2ee JSTL cannot print a list of objects

I'm currently working on a small Java J2E project. 我目前正在研究一个小型Java J2E项目。

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. 我想在* .jsp页面中打印对象列表,但是有一个错误似乎无法消除。

My .jsp page : 我的.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>

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. 当我尝试用${element}替换${element.Name} ,我得到beans.Car@3a7488b4 ,所以我得到了我的对象,只是无法探索。

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 根据Java命名约定,字段和方法的名称应使用驼峰形式,例如

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. JavaBeans也遵循此约定,因此请将字段和名称的首字母更改为小写。

Oh, and after you change it don't forget to also update your EL query to use ${element.name} , not ${element.Name} 哦,更改之后,请不要忘记也将EL查询更新为使用${element.name} ,而不是${element.Name}


With these changes following example works fine for me: 通过这些更改,以下示例对我而言效果很好:

Car.java JavaBean 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 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 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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