0
我在我的JSP頁面中遇到了類屬性爲public enum
的問題。我可以訪問除invite.status
屬性以外的所有其他邀請類屬性。我做錯了什麼,但我不明白爲什麼它是錯誤的,我怎樣才能訪問我的返回列表的enum
值。感謝您的幫助解決我的問題!JSP - 獲取類枚舉類型屬性時出錯
錯誤我得到類...domain.jpa.meeting.Invite
'沒有屬性status
。
這是JPA類,我有
@Entity
public class Invite implements PersistentEntity {
@Id @GeneratedValue
private Long id;
@ManyToOne
private User inviter;
@ManyToOne
private User invitee;
@ManyToOne
private Meeting meeting;
/**
* Date when this invite has been created
*/
private Date created;
// @Column(nullable=false, length=1)
// @Enumerated(EnumType.STRING)
private InviteStatus status;
public Long getId() {
return id;
}
public User getInviter() {
return inviter;
}
public void setInviter(User inviter) {
this.inviter = inviter;
}
public User getInvitee() {
return invitee;
}
public void setInvitee(User invitee) {
this.invitee = invitee;
}
public Meeting getMeeting() {
return meeting;
}
public void setMeeting(Meeting meeting) {
this.meeting = meeting;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public InviteStatus getInviteStatus() {
return status;
}
public void setInviteStatus(InviteStatus status) {
this.status = status;
}
}
這是枚舉類型的公共類,我有
/**
* Values for tracking invite status
*/
public enum InviteStatus {
// invite is created and invitee have not responded yet
CREATED,
// invitee accepted this invite
ACCEPTED,
// invitee declined this invite
DECLINED
}
我在數據庫這樣的方式保存(成功爲EnumType.ORDINAL)
// adding invites (table invite)
Invite invite = new Invite();
invite.setInviter(creator);
User invitee = jpaDAO.getById(User.class, Long.parseLong(friends[i]));
invite.setInvitee(invitee);
invite.setInviteStatus(InviteStatus.CREATED); //CREATED = 0, ACCEPTED = 1, ..
Date dateNow = new Date(); //yyyy-MM-dd HH:mm:ss
invite.setCreated(dateNow);
Meeting meeting = jpaDAO.getById(Meeting.class, meetingId);
invite.setMeeting(meeting);
jpaDAO.save(invite);
而我有一個方法,從數據庫檢索邀請。
@SuppressWarnings({ "unchecked", "null" })
public List<Invite> getUserInvites(Long userId){
// getting invites for current user with status CREATED
Query query = jpaDAO.getEntityManager().createNamedQuery(JPQConst.InviteJpq.QUERY_GET_BY_USER);
query.setParameter("userId", userId);
List<Invite> invites = (List<Invite>) query.getResultList();
return invites;
}
內部控制器,它看起來像這樣:
/**
* Display Notifications tab
*/
@RequestMapping(value = "/home/notifications", method = RequestMethod.GET)
public String displayNotifications(ModelMap model, HttpSession session) {
User user = getCurrentUser(session);
List<Invite> invites = notificationService.getUserInvites(user.getId());
model.addAttribute("invites", invites);
return "home";
}
方法本身的工作,我能夠訪問所有邀請的屬性字段內除外JSP狀態屬性。
<c:forEach var="meeting" items="${inviteeMeetings}">
<c:forEach var="invite" items="${invites}">
<c:choose>
<c:when test="${meeting.id == invite.meeting.id}">
<tr>
<td width="150px" align="center">
<c:out value="${meeting.startTime}" />
</td>
<td width="396px">
<a href="../meeting/${meeting.id}"><c:out value="${meeting.title}" /></a>
</td>
<td width="96px">
<c:out value="${invite.status}" />
<form action="../meeting/attendance" method="post">
<input type="hidden" name="venue" value="${meeting.id}" />
<input type="submit" name="attendance" value="attend" />
<input type="submit" name="attendance" value="reject" />
</form>
</td>
</tr>
</c:when>
</c:choose>
</c:forEach>
</c:forEach>
此外,限制您的問題的範圍。如果它是一個JSP問題,那麼bean是否由JPA或Hibernate提供或在代碼中進行硬編碼並不重要。你可能想檢查http://sscce.org – SJuan76