playframework,如何做一个组合框,在其中为我修改JPA bean? [英] playframework, how to do a combobox where it modifies the JPA bean for me?

查看:70
本文介绍了playframework,如何做一个组合框,在其中为我修改JPA bean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有:

public class TicketDbo {
   @Required
   @ManyToOne
   public ProjectDbo project;
}

然后我们有许多项目可供选择,因此在编辑故障单时我们会添加一个下拉菜单,您可以在其中选择一个项目.

We then have many projects to choose from, so we add a pulldown menu when you edit the Ticket where you can choose one of the projects.

我们要渲染这样的东西(这是简单的部分,我们知道如何使用#{select}来完成此部分:

We want to render something like this(this is the easy part and we know how to do this part with #{select}:

<select ... >
  <option value="" selected="selected">Please select...</option>
  <option value="1">Project1</option> 
  <option value="56">Project2</option>
</select>

注意:上面的值是数据库中的实体ID.

NOTE: The value above is the entity id in the database.

我们想要的是控制器外部的一些代码,该代码将在调用控制器之前运行(并且我们希望可以将这些代码用于所有发生在所有 ManyToOne 情况下的代码)时间):

What we want is some piece of code outside the controller where this code would run before the controller is invoked (AND we could hopefully re-use this code for any of these ManyToOne situations that happen all the time):

 ProjectDbo project = JPA.em().getReference(selectedOptionValueFromAbove); 
 //note, getReference doesn't hit DB!!!!
 ticketDbo.setProject(project);

然后我们的控制器将一如既往:

Then our controller would be the same as always:

public static void postTicket(TicketDbo ticket) {
    //at this point the ticket already has the ProjectDbo and validation of it being required has already been run from the @Required annotation
}

有人知道该怎么做吗?还是有一个插件可以帮助您解决这个问题?

Anyone knows how to do something like this? Or is there a plugin to help with this?

@ 2ND问题:枚举也如何? (希望这只是对上面的一项调整).

@2ND QUESTION: How do this with an enumeration as well? (Hopefully that is just a tweak to the above).

推荐答案

这似乎效果很好(由对另一个答案的@maartencls评论之一形成)

This seems to work well(formed from one of the comments @maartencls made on another answer)

@Global
public class ProjectRefBinder implements TypeBinder<ProjectDBO>{
    @Override
    public Object bind(String name, Annotation[] annotations, String value,
            Class actualClass, Type genericType) throws Exception {
        if(value == null)
            return null;
        EntityManager em = JPA.em();
        Long id = Long.valueOf(value);
        ProjectDBO ref = em.getReference(ProjectDBO.class, id);
        return ref;
    }
}

然后在页面中,我可以执行#{ticket.project},并在我的控制器方法中,我的TicketDbo最终得到一个ProjectDbo代理,该代理包装了所选的ID !!!我认为到目前为止效果很好.该类不仅适用于我的TicketDbo,也适用于其他实体以及其中包含ProjectDbo的实体.

Then in the page, I can do a #{ticket.project} and in my controller method, my TicketDbo ends up with a ProjectDbo proxy wrapping the id selected!!! works great so far I think. This class also works not just for my TicketDbo for other entities as well that have a ProjectDbo in them.

这篇关于playframework,如何做一个组合框,在其中为我修改JPA bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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