简体   繁体   English

Play框架-删除对象后在会话中找不到用户ID

[英]Play Framework - Can't find user ID in session after deletion of object

I have a table listing registered activities with the possibility to delete each of them. 我有一张列出已注册活动的表格,可以删除每个活动。 The table looks like this: 该表如下所示:

<table class="table-striped table-condensed table-hover">
    <tr>
        <th>Date</th>
        <th>Activity</th>
        <th>Steps</th>
        <th></th>
    </tr>
    @for(a <- activities) {
    @helper.form(routes.DashboardController.deleteUA(a.id)) {
        <tr>
            <td style="width:30%">@a.date.format("dd MMM")</td>
            <td style="width:30%">@a.activity.name</td>
            <td style="width:30%">@a.steps</td>
            <td>
            <button type="submit" style="background-color:#FF6666"><i class="fa fa-trash-o"></i></button>
            </td>
            </tr>
    }}
</table>

The method to delete the UserActivity object looks like this: 删除UserActivity对象的方法如下所示:

public static Result deleteUA(Long id) {
    UserActivity.find.ref(id).delete();
    return dashboard();
}

When the object is deleted the method will render the page again with the dashboard() method: 删除对象后,该方法将使用dashboard()方法再次呈现页面:

@Security.Authenticated(Secured.class)
public static Result dashboard() {
    User user = User.find.byId(request().username());
    return ok(dashboard.render(user, Tips.all(), 0.0 , getGoals(user), updateLeaderboards(), getRecentUA()));
}

When I submit the form and call on the deleteUA() the object is deleted as expected, but when I'm going to render the page with dashboard() I get this error: 当我提交表单并调用deleteUA()时,该对象将按预期方式删除,但是当我使用dashboard()呈现页面时,出现此错误:

[NullPointerException: The id is null] [NullPointerException:ID为null]

from this line: 从这一行:

User user = User.find.byId(request().username()); 用户用户= User.find.byId(request()。username());

If I go back to the dashboard by calling on the url (calling on the dashboard() method that way) everything seems fine and the deleted activity is gone from the list. 如果我通过调用url(以这种方式调用dashboard()方法)返回到仪表板,那么一切似乎都很好,并且删除的活动也从列表中消失了。

I can't understand why the deletion of an UserActivity object has anything to do with the finding of the user id in the session though. 我不明白为什么删除UserActivity对象与在会话中找到用户ID有什么关系。 Any ideas? 有任何想法吗?

Update! 更新! UserActivity looks like this: UserActivity看起来像这样:

package models;

    import java.util.*;
    import play.db.ebean.*;
    import javax.persistence.*;

    @Entity
    public class UserActivity extends Model {

        @Id
        public Long id;
        @ManyToOne 
        public User belongsTo;
        @ManyToOne  
        public Activity activity;
        public int intensity;
        public double steps;
        public Date date;

        public static Finder<Long,UserActivity> find = new Finder<Long,UserActivity>(
            Long.class, UserActivity.class
        );
    }

How about trying to use the redirect(...) method instead of just calling the controller's action. 如何尝试使用redirect(...)方法而不是仅调用控制器的操作。 I would assume that your routes file has some entry like this 我认为您的routes文件中有一些像这样的条目

GET /dashboard controllers.Application.dashboard()

Then in your deleteUA method you can write 然后在您的deleteUA方法中,您可以编写

public static Result deleteUA(Long id) {
    UserActivity.find.ref(id).delete();
    return redirect(controllers.Application.dashboard());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM