本文介绍了获取RuntimeException:数据源用户是否为null?在Play Framework 2.2.1中更新用户模型时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试增强Play框架网站上的待办事项列表"教程.我添加了一个登录表单(当然还有一个User类充当模型),它运行得很好.我还制作了一个小的仪表板",使登录用户可以更改其密码和电子邮件地址.但是,当我提交表单时,我得到一个数据源用户为空?"的信息.错误(RuntimeException).

I am currently trying to enhance the To-Do List tutorial from Play framework's website. I've added a login form, (and of course a User class acting as a model), and it works perfectly well. I also made a small "dashboard" in which the logged user is able to change his password and his email address. But when I submit the form, I get a "Datasource user is null ?" error (RuntimeException).

当我想限制版本的可能性时,出现了整个问题(我首先使用了一个完整的User表单,该表单位于顶部(User不需要编辑其ID).因此,我在自己的类中做了一个小内部类就像我在登录系统中所做的一样,名为UpdateUser的应用程序文件将收集所需的信息.

The whole problem came when I wanted to restrict the edition possibilities (I first used a whole User form, which is quite over the top (User do not need to edit their ID). So I made a small inner class in my Application file called UpdateUser which gathers the required informations, just as I did for the login system.

搜索此错误给了我很多结果,但是人们看到了通过取消注释conf文件中的ebean.default行来解决他们的问题的方法,

Searching this error gave me many results but people saw their problem fixed by uncommenting the ebean.default line in the conf file, which I already did.

这是我用来更新用户信息的方法:

Here is the method I used to update user's informations :

首先,我在我的应用程序中做了一个小类来保存表单(就像我登录时所做的一样).然后,我在此处中找到了一个更新功能我的用户类别:

Firstly, I made a small class in my Application to hold the form (exactly like I did for the login thing).Then I made a update function as found here in my user class :

public static String update(String id, User newuser) {
    newuser.update(id);
    return("Your profile has been updated");    
}

返回将在我的Flash中存储的字符串,并且根据我的编译器,该字符串是问题函数.

which returns the String that will be in my flash and which is according to my compiler the problem function.

此函数在我的应用程序中的调用方式如下:

This function is called in my Application like this :

public static Result updateUser(String id)
{
    Form<UpdateUser> filledForm = updateUserForm.bindFromRequest();
    System.out.println("Updated User : "+filledForm.get().id);
    if(filledForm.hasErrors())
        {
            flash("success","Error while updating");
    }else{
        User user = new User(filledForm.get().id, filledForm.get().email, User.find.byId(filledForm.get().id).name, User.find.byId(filledForm.get().id).surname, filledForm.get().password);
        flash("success", User.update(id,user));
    }
    return redirect(routes.Application.dashboard());
}

我跟踪了Form中的数据,但它不是null(我可以从表单中获取所有信息).但是我想知道是否必须创建另一个ebean还是我的功能错了.我也想知道是否不是我的用户创建失败.还是我应该采用updateUser函数并将其放在我的内部UpdateUser类中?

I tracked the data in the Form and it is not null (I mean I can get everything from the form). But I wonder if I have to create another ebean or if it's my function which is wrong. I also wonder if it's not my User creation that fail. Or maybe I should take the updateUser function and put it in my inner UpdateUser class ?

我不得不承认我昨天(可能也是今天)都在工作,除了ebean.default之外,我在互联网上找不到任何东西.

I have to admit that I worked on that all of yesterday (and probably today too), and I can't find anything on the internet except the ebean.default thing.

------编辑

我继续搜索,所以这是我尝试过的:

I continued to search, so here's what I tried :

1)将表单结果获取到UpdateUser实例中以进行使用
2)使用此实例,而不是从表单中获取数据

1) Getting the form result into an instance of UpdateUser in order to use it
2) Use this instance instead of getting the data from the form

但是它也失败了.真正奇怪的是,我为User类添加了toString()方法,并在要插入的用户上调用它(作为更新)给了我全部的东西.我认为这一定是配置问题,但我看不到.

But it failed too. What's really weird is that I've added a toString() method for User class, and calling it on the user I want to insert (as an update) gives me the full stuff. I think it must be a configuration problem, but I can't see it.

另一件事:当我进入错误页面并且尝试通过修改URL返回到应用程序时,我断开了连接.是我的自我封闭的小豆吗?

Another thing : when I come to the error page and when I try to come back to the application by modifying the URL, I am disconnected. Is it my ebean that closes himself ?

当天的最后修改,我对此感到厌倦.我试图延迟操作(即在用户注销后使操作发生),新数据已正确保存,但是在调用更新函数时仍然出现错误.

Last edit for the day, I'm getting tired of this. I tried to delay the action (i.e. making it happen after the user has logged out), the new data are correctly saved but I still get the error when calling the update function.

推荐答案

好的,我终于找到了,但是完全是偶然的.

Alright, I finally found it, but totally by chance.

我只需要写这个:

public static String update(String id, User user) {
    user.update((Object)id);
    return ("Your profile has been updated");
}

由于某种原因,我不太了解,因此需要将id字符串强制转换为Object.其余代码是正确的.但是显然,当在这种情况下使用update()时(是因为我的id是String还是因为在将其用作模型之前我从另一个类获取了信息),所以该参数应该是String(即使在文档)必须投放.

Because for some reason that I don't really understand, The id String needs to be cast to Object. The rest of the code was correct. But apparently, when using update() on this particular case (is it because my id is a String or because I get the informations from another class before using it as my Model), the parameter which is supposed to be a String (even in the documentation) HAS to be cast.

就是这样.

这篇关于获取RuntimeException:数据源用户是否为null?在Play Framework 2.2.1中更新用户模型时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 14:24