如何从控制器获取属性作为表达式值?
我不断收到此异常:
javax.el.PropertyNotFoundException:找不到属性“消息” 输入 com.itworx.twitter.model.User
当我尝试从控制器运行此代码时,
@RequestMapping(value="/profile", method=RequestMethod.GET)
public ModelAndView goProfile(@ModelAttribute("user") User user){
if(user != null){
ModelAndView modelAndView = new ModelAndView("profile-layout");
List< Message> messages=service.getUserMessages(user.getUsername());
/*user.setMessages(messages);*/
/*modelAndView.addObject("user",user);*/
modelAndView.addObject("userMessages",messages);
return modelAndView;
}else{
return new ModelAndView("register-layout");
}
}
:用户 bean 是这样的:
@Entity
@Table(name="user")
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = -5689970869179555442L;
@Id
@Column(name="username")
/*@NotNull*/
private String username;
@Column(name="password")
private String password;
@Column(name="email")
/*@Email*/
private String email;
@ManyToMany
@JoinTable(name="user_follower",joinColumns={@JoinColumn(name="username")},
inverseJoinColumns={@JoinColumn(name="follower")})
private List<User> followers = new ArrayList<User>(0);
/*@LazyCollection(LazyCollectionOption.FALSE)*/
@OneToMany
@JoinTable(name="user_message",joinColumns={@JoinColumn(name="username")},
inverseJoinColumns={@JoinColumn(name="message_date")})
@OrderBy(value="messageDate")
private List<Message> messages = new ArrayList<Message>(0);
public User() {
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public void setUserFollowers(List<User> followers) {
this.followers = followers;
}
public List<User> getUserFollowers() {
return followers;
}
public void setMessages(List<Message> messages) {
this.messages = messages;
}
public void addMessage(Message message){
this.messages.add(message);
}
public List<Message> getMessages() {
return messages;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
dao 实现是这样的:
public List<Message> getUserMessage(String username) {
session=sessionFactory.getCurrentSession();
@SuppressWarnings("unchecked")
List<Message> messages=session.createCriteria(User.class).setFetchMode("messages", FetchMode.JOIN)
.add(Restrictions.eq("username", username)).list();
return messages;
}
页面是这样的:
<body>
<table id="feeds" style='width: 100%'>
<tr></tr>
</table>
<table style='width: 100%'>
<tr></tr>
<c:forEach var="msg" items="${userMessages}">
<tr>
<td>
<c:out value="${msg.message}" />
</td>
</tr>
<tr>
<td>
<c:out value='${msg.messageDate}' />
</td>
</tr>
</c:forEach>
</table>
</body>
I keep getting this exception:
javax.el.PropertyNotFoundException: Property 'message' not found on
type com.itworx.twitter.model.User
when I try to run this code from my controller:
@RequestMapping(value="/profile", method=RequestMethod.GET)
public ModelAndView goProfile(@ModelAttribute("user") User user){
if(user != null){
ModelAndView modelAndView = new ModelAndView("profile-layout");
List< Message> messages=service.getUserMessages(user.getUsername());
/*user.setMessages(messages);*/
/*modelAndView.addObject("user",user);*/
modelAndView.addObject("userMessages",messages);
return modelAndView;
}else{
return new ModelAndView("register-layout");
}
}
the user bean is like this:
@Entity
@Table(name="user")
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = -5689970869179555442L;
@Id
@Column(name="username")
/*@NotNull*/
private String username;
@Column(name="password")
private String password;
@Column(name="email")
/*@Email*/
private String email;
@ManyToMany
@JoinTable(name="user_follower",joinColumns={@JoinColumn(name="username")},
inverseJoinColumns={@JoinColumn(name="follower")})
private List<User> followers = new ArrayList<User>(0);
/*@LazyCollection(LazyCollectionOption.FALSE)*/
@OneToMany
@JoinTable(name="user_message",joinColumns={@JoinColumn(name="username")},
inverseJoinColumns={@JoinColumn(name="message_date")})
@OrderBy(value="messageDate")
private List<Message> messages = new ArrayList<Message>(0);
public User() {
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public void setUserFollowers(List<User> followers) {
this.followers = followers;
}
public List<User> getUserFollowers() {
return followers;
}
public void setMessages(List<Message> messages) {
this.messages = messages;
}
public void addMessage(Message message){
this.messages.add(message);
}
public List<Message> getMessages() {
return messages;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
and the dao implementation is like this:
public List<Message> getUserMessage(String username) {
session=sessionFactory.getCurrentSession();
@SuppressWarnings("unchecked")
List<Message> messages=session.createCriteria(User.class).setFetchMode("messages", FetchMode.JOIN)
.add(Restrictions.eq("username", username)).list();
return messages;
}
the page is like this:
<body>
<table id="feeds" style='width: 100%'>
<tr></tr>
</table>
<table style='width: 100%'>
<tr></tr>
<c:forEach var="msg" items="${userMessages}">
<tr>
<td>
<c:out value="${msg.message}" />
</td>
</tr>
<tr>
<td>
<c:out value='${msg.messageDate}' />
</td>
</tr>
</c:forEach>
</table>
</body>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉,这个回复已经一周了,所以您可能已经弄清楚了这一点,但我认为问题出在您的 DAO 实现中。这一行:
实际上会返回一个
User
对象列表,其中messages
集合已初始化。将 DAO 方法更改为:
应该为您提供(并且可能已经为您提供)您正在寻找的内容。
Sorry, this reply is a week old so you've probably figured this out, but I think the problem is in your DAO implementation. This line:
Is actually going to give you back a list of
User
objects with themessages
collection initialized.Changing the DAO method to this:
should give you (and probably gave you already) what you're looking for.