gpt4 book ai didi

java - JSP无法访问对象属性

转载 作者:行者123 更新时间:2023-11-30 03:20:05 25 4
gpt4 key购买 nike

我有对象项目:

public class Project {
public static int projectid = 0;

private int id;

private String name;
private String description;

private Category category;

public DonationLogic donation;

private String link;
private String questionsAndAnswers;

//methods
}

和 DonationLogic(是项目的属性(property)):

public class DonationLogic {
private int totalAmount;
private int daysLeft;
private double collectAmount;
private int id;
private String donatorname;
private int daonatorcardid;
private static int donationid = 0;

//methods
}

我尝试访问 JSP 中的项目属性:

<c:forEach var="project" items="${projects}">
<c:out value="${project.name}"/></a></br>
Name: <c:out value="${project.name}"/></br>
Description: <c:out value="${project.description}"/></br>
TotalAmount/CollectAmount: <c:out value="${project.donation.totalAmount}/${project.donation.collectAmount}"/></br>
DaysLeft: <c:out value="${project.donation.daysLeft}"/></br>
Category: <c:out value="${project.category.name}"/></br>
</c:forEach>

除了带有 project.donation.XXX 的行之外,所有代码都可以工作。错误是:

javax.el.PropertyNotFoundException: Property 'donation' not found on type data.Project

我能做什么?与 project.category.name 一起工作我注意到属性(property)捐赠是公开的,而其他则是私有(private)的。也许问题就出在这里?

最佳答案

添加 getter 方法:

public DonationLogic getDonation {
return this.donation;
}

您还可以在 jsp 中添加类似的内容来查看对象的外观:

PROJECTS=${projects}<br/>
<c:forEach var="project" items="${projects}">
PROJECT=${project}<br/>
<c:out value="${project.name}"/></a></br>
Name: <c:out value="${project.name}"/></br>
Description: <c:out value="${project.description}"/></br>
TotalAmount/CollectAmount: <c:out value="${project.donation.totalAmount}/${project.donation.collectAmount}"/></br>
DaysLeft: <c:out value="${project.donation.daysLeft}"/></br>
Category: <c:out value="${project.category.name}"/></br>
</c:forEach>

关于java - JSP无法访问对象属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31545485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com