【发布时间】:2013-12-15 06:26:57
【问题描述】:
我正在尝试使用以下字段填充 html 歌曲表。
歌曲名称、专辑名称
为了做到这一点,我已经将 Song 对象的 List 从我的 struts 2 动作类发送到 JSP。以下是 Song 和 Album 类的结构。
Song
----
Title:String
album: Album
Album
-----
title : String
releasedYear:int
这里的问题是,当我尝试遍历 allSongs 列表时,我可以打印歌曲名称,但它不会打印专辑名称。下面是我用struts标签写的JSP代码。
<table width="500px">
<s:iterator value="allSongs">
<tr>
<td><s:property value="Title" /></td> // this is getting printed
<td><s:property value="album.title" /></td> // this doesnt get printed
</tr>
</s:iterator>
</table>
我尝试在 JSP 中使用以下代码来查找问题。但它显示 null 作为输出。
System.out.println(pageContext.findAttribute("album")
你能请任何人帮助我吗?如果提供的信息不充分,请告诉我,我会更新问题。提前致谢。
注意:我在将列表传递给 JSP 之前检查了该列表,其中的值已正确填充。
动作类
public class HomeAction {
private List<Song> allSongs;
public List<Song> getAllSongs() {
return allSongs;
}
public void setAllSongs(List<Song> allSongs) {
this.allSongs = allSongs;
}
private MusicManager musicManager;
public MusicManager getMusicManager() {
return musicManager;
}
public void setMusicManager(MusicManager musicManager) {
this.musicManager = musicManager;
}
// all struts logic here
public String displayAllSongs() {
allSongs = musicManager.listAllSongs();
return "SUCCESS";
}
}
歌曲课
@Entity
@Table(name = "Song", catalog = "myFavMusic")
public class Song implements Serializable {
@Id
@GeneratedValue(strategy = IDENTITY)
private Integer id;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Album album;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Singer singer;
private Integer rating;
//getter setters
}
专辑类
@Entity
public class Album implements Serializable {
public Album() {
super();
}
@OneToMany(fetch = FetchType.EAGER, mappedBy = "album")
private List<Song> songs = null;
@Id
@GeneratedValue
@Column(name = "ALBUM_ID", unique = true, nullable = false, length = 20)
private Integer id;
@Column(name = "TITLE", unique = true, nullable = false)
private String title;
// album or song , this could be replaced with enum later
@Column(name = "TYPE", nullable = false)
private String type;
@Column(name = "RELEASED_YEAR", nullable = false)
private Integer releasedYear;
//getter setters
}
【问题讨论】:
-
请分享您正在使用的表格和动作类
-
这应该可以正常工作,尝试在迭代器中执行
System.out.println(pageContext.findAttribute("album.title")看看结果如何。 -
我按照你说的 Dan 试过了,它也打印出 null
-
你能发布 Action 类以及 Song 和 Album 类吗?
-
将您的代码发布为文本,而不是图像。并张贴。您发布的代码不完整。重要的是 bean 属性,即 getter。
标签: java list jsp struts2 iterator