playframework随机CRUD错误 [英] playframework random CRUD error

查看:93
本文介绍了playframework随机CRUD错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我执行以下控制器操作时,在此问题结尾处出现错误. 出现此错误时,如果我在浏览器中刷新页面,则显示的控制器视图页面没有错误. 我不确定是什么原因导致在首次请求beleow控制器操作时出现此错误?

when i execute the below controller action I get the error attached at end of this question. when getting this error and if i refresh the page in browser the controller view page displays with no error. i'm not sure what causes this error at first request of beleow controller action?

/**
     * controller to register new user.
     *  Shows registration screen.
     */
    public static void registration() throws Exception {

        ObjectType type = ObjectType.forClass("models.User");

        Constructor<?> constructor = type.entityClass.getDeclaredConstructor();
        constructor.setAccessible(true);

        Model object = (Model) constructor.newInstance();
        /*System.out.print("type=");
        System.out.println(type);
        System.out.print("object=");
        System.out.println(object);*/
        render(type, object);
    }

----异常错误跟踪--------------

----Exception error trace--------------

23:12:14,229 ERROR ~ 

@69bf92hlc
Internal Server Error (500) for request GET /registration

Template execution error (In {module:crud}/app/views/tags/crud/types.tag around line 3)
Execution error occured in template {module:crud}/app/views/tags/crud/types.tag. Exception raised was NullPointerException : null.

play.exceptions.TemplateExecutionException
    at play.templates.BaseTemplate.throwException(BaseTemplate.java:86)
    at play.templates.GroovyTemplate.internalRender(GroovyTemplate.java:257)
    at play.templates.GroovyTemplate$ExecutableTemplate.invokeTag(GroovyTemplate.java:379)
    at {module:crud}/conf/routes.(line:4)
    at play.templates.GroovyTemplate.internalRender(GroovyTemplate.java:232)
    at play.templates.Template.render(Template.java:26)
    at play.templates.GroovyTemplate.render(GroovyTemplate.java:187)
    at play.mvc.Router.parse(Router.java:162)
    at play.mvc.Router.parse(Router.java:190)
    at play.mvc.Router.parse(Router.java:164)
    at play.mvc.Router.load(Router.java:48)
    at play.mvc.Router.detectChanges(Router.java:219)
    at Invocation.HTTP Request(Play!)
Caused by: java.lang.NullPointerException
    at play.classloading.ApplicationCompiler$2.acceptResult(ApplicationCompiler.java:266)
    at org.eclipse.jdt.internal.compiler.Compiler.compile(Compiler.java:478)
    at play.classloading.ApplicationCompiler.compile(ApplicationCompiler.java:282)
    at play.classloading.ApplicationClassloader.getAllClasses(ApplicationClassloader.java:424)
    at play.classloading.ApplicationClassloader.getAssignableClasses(ApplicationClassloader.java:453)
    at play.classloading.ApplicationClassloader$getAssignableClasses.call(Unknown Source)
    at {module:crud}/app/views/tags/crud/types.tag.(line:3)
    at play.templates.GroovyTemplate.internalRender(GroovyTemplate.java:232)
    ... 11 more

推荐答案

我认为出现错误的原因是,当您第一次尝试显示表单时,还没有创建对象,所以Exception raised was NullPointerException : null.

I think the reason for you error is that when you first try display the form there's no object created yet, so the Exception raised was NullPointerException : null.

不幸的是,我对Play 2. *不熟悉,也没有计划在它更稳定之前启动,但我想我知道CRUD生成

Unluckily I'm not familiar with Play 2.* and do not plan on starting before it's more stable, but I think I understood that CRUD generation is not included nor fully supported there, so you are probably using code from play1 crud?

我认为您的解决方案是更好地覆盖New(空白)/View/Save模式;看到您的路线也可能有助于了解这个确切的问题.

I think the solution in your case is to better cover the New(blank) / View / Save pattern; seeing your Routes might also help to understand this precise issue.

无论如何,考虑到您不是在生成未知的模型类型,而是总是在生成User的模型类型,经历这种复杂模式的真正优势是什么?

In any case, considering you are not generating an unknown model type, rather always a User one, what is the real advantage of going through this complicated pattern?

您应该做类似的事情

User user = null
render()

查看/编辑

User user = User.findById(id);

保存

public static void save(@Valid User object) {

    if(validation.hasErrors()) {
        params.flash(); // add http parameters to the flash scope
        User user = object;
        render("User/show.html", user);
    }
}

这篇关于playframework随机CRUD错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆