gpt4 book ai didi

java - 支柱2 : Displays table of Songs - null gets printed

转载 作者:太空宇宙 更新时间:2023-11-04 15:22:24 25 4
gpt4 key购买 nike

我正在尝试使用以下字段填充 html 歌曲表。

歌曲名称专辑名称

为了做到这一点,我已经将 Song 对象的 List 从我的 struts 2 操作类发送到 JSP。以下是歌曲和专辑类的结构。

  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 之前检查了该列表,其中的值已正确填充。

Action 类

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
}

最佳答案

我解决了这个问题。 Song 类没有专辑属性的 getter 方法。正如 JB Nizet 所说,我检查了所有类的 getter 和 setter,这解决了问题。谢谢你们。

为了确定问题,我使用了 jsp 内的 scriptlet block 并编写了 java 代码来找出问题。当我尝试编写song.getAlbum()行时,我发现歌曲类中没有定义getAlbum方法。

    List<com.myprojects.myfavmusic.domain.Song> allSongs =(List<com.myprojects.myfavmusic.domain.Song>)pageContext.findAttribute("allSongs");
for (com.myprojects.myfavmusic.domain.Song song : allSongs) {
System.out.println(song.getAlbum().getTitle());
}

关于java - 支柱2 : Displays table of Songs - null gets printed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20279296/

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