如何从控制器获取属性作为表达式值?

时间:2011-08-02 13:09:19

标签: jsp spring-mvc jstl el

我一直得到这个例外:

  

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>

1 个答案:

答案 0 :(得分:0)

很抱歉,这个回复已经过了一周,所以你可能已经想到了这一点,但我认为问题出在您的DAO实施中。这一行:

List<Message> messages=session.createCriteria(User.class)
        .setFetchMode("messages", FetchMode.JOIN)
        .add(Restrictions.eq("username", username)).list();

实际上会在初始化User集合的情况下返回messages个对象的列表。

将DAO方法更改为:

session=sessionFactory.getCurrentSession();
@SuppressWarnings("unchecked")
User userWithMessages=session.createCriteria(User.class)
        .setFetchMode("messages", FetchMode.JOIN)
        .add(Restrictions.eq("username", username)).uniqueResult();
return userWithMessages.getMessages();

应该给你(并且可能已经给你)你要找的东西。