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

发布于 2024-11-27 06:52:07 字数 3773 浏览 0 评论 0原文

我不断收到此异常:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方的韩爷 2024-12-04 06:52:07

抱歉,这个回复已经一周了,所以您可能已经弄清楚了这一点,但我认为问题出在您的 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();

应该为您提供(并且可能已经为您提供)您正在寻找的内容。

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:

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

Is actually going to give you back a list of User objects with the messages collection initialized.

Changing the DAO method to this:

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

should give you (and probably gave you already) what you're looking for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文