如何在 JSTL 中的对象列表上迭代对象?
我有一个名为 Language
的类,其中有 1 个名为 name
的字段。我有另一个名为 Speech
的类,它有一个 language
成员。我需要迭代 Speech
对象列表。在 JSTL 中:
<c:forEach items="${requestScope.Speech}" var="speech">
<tr>
<td> ${speech.id}</td>
<td> ${speech.language.name}</td>
</tr>
</c:forEach>
我的第二条语句 ${speech.language.name}
不起作用。我怎样才能让它发挥作用?
言语和语言课程:
public class Speech {
private int id;
private Language language=null;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Language getLanguage() {
return language;
}
public void setLanguage(Language language) {
this.language = language;
}
}
public class Language {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
I have a class named Language
with 1 field named name
. I have another class named Speech
which has a language
member. I need to iterate over a list of Speech
objects. In JSTL:
<c:forEach items="${requestScope.Speech}" var="speech">
<tr>
<td> ${speech.id}</td>
<td> ${speech.language.name}</td>
</tr>
</c:forEach>
My second statement ${speech.language.name}
doesn't work. How can I make it work?
Speech and Language classes:
public class Speech {
private int id;
private Language language=null;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Language getLanguage() {
return language;
}
public void setLanguage(Language language) {
this.language = language;
}
}
public class Language {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确保您的
Speech
类定义了public Language getLanguage()
方法。Make sure that your
Speech
class has apublic Language getLanguage()
method defined.JSP EL 不使用字段。它使用属性。为了使该语句起作用,您需要在
Speech
中有一个公共方法getLanguage()
,在语言
。编辑:
如果 getter 在那里,并且没有打印任何内容,则可能有四个原因:
Speech
属性 语音如果为语音 ID 打印某些内容,则可以消除前两个原因。
The JSP EL doesn't use fields. It uses properties. In order for this statement to work, you need to have a public method
getLanguage()
inSpeech
, and a public methodgetName()
inLanguage
.EDIT:
If the getters are there, and nothing is being printed, it can have four reasons:
Speech
attribute in the requestIf something is printed for the speech ID, it eliminates the first two reasons.