简体   繁体   English

JSF / primefaces POJO自动完成如何将itemValue发送到转换器

[英]JSF/primefaces POJO Autocomplete how to send itemValue to converter

Need to use JSF/primefaces autocomplete with a POJO (say of name,id) to display data and save the information about selected object in DB. 需要将JSF / primefaces自动完成功能与POJO(例如name,id)一起使用,以显示数据并将有关所选对象的信息保存在DB中。

Bean 豆角,扁豆

class BackingBean{
  POJOClass obj;

  getters and setters
}

POJO class POJO课

class POJOClass{
  String name;
  int id;

  getters and setters
}

While displaying need to show name in autocomplete suggestion list but while saving need to save both id and name. 在显示时需要在自动完成建议列表中显示名称,但是在保存时需要同时存储id和名称。

Primefaces code Primefaces代码

<p:autocomplete value="#{backingbean.obj}" var="object" itemValue="#{object}" itemLabel="#{object.name}"/>

Converter 变流器

Class MyConverter ...
{
 public Object getAsObject(FacesContext fc, UIComponent uic, String value){
    String name=value;
    POJO myPOJO=new POJO();
    myPOJO.setName(name);
    myPOJO.setId(???)//need id information as well over here
    return myPOJO;
 }
  public String getAsString(...){
    //returns name to display
  }
}

Able to get only name in getAsObject method of the converter. 只能在转换器的getAsObject方法中获取名称。 Also, cannot get object from DB using only name but through id I can. 另外,不能仅使用名称而是通过ID从数据库获取对象。

Note: Don't want to make DB call to get information. 注意:不想进行数据库调用来获取信息。

In short aim is to either get id in converter's getAsObject or whole POJO object in converter.Not sure if there is some-other way to achieve this. 简而言之,目的是在转换器的getAsObject中获取id或在转换器中获取整个POJO对象。不确定是否还有其他方法可以实现此目的。

Any clues how we achieve that? 有什么线索可以实现吗?

Note: I am newbie to JSF/Primefaces. 注意:我是JSF / Primefaces的新手。

In short, you want values to persist. 简而言之,您希望价值观保持不变。 For that you can either use a persistent api like jpa or simply save them more globally in global objects like one used for cache whose scope is application level. 为此,您可以使用诸如jpa之类的持久性api,也可以将它们更全局地保存在全局对象中,例如用于范围为应用程序级的缓存。 I think what you are searching is the second option. 我认为您要搜索的是第二个选择。 This object may be initialised when the application starts. 应用程序启动时可以初始化此对象。

if key found in cache, get the values else execute a db call. 如果在缓存中找到键,则获取值,否则执行db调用。

If you have only just one resource like this then you can get know which one POJOClass instance is needed. 如果只有这样的一种资源,则可以知道需要哪个POJOClass实例。 You can use EL expressions in a managed environment (converters managed by the container). 您可以在受管环境(由容器管理的转换器)中使用EL表达式。 In this case you need just to convert the name field by the converter and access the only one ID via an EL expression: 在这种情况下,您只需要通过转换器转换名称字段并通过EL表达式访问唯一的ID:

Class MyConverter ...
{
  public Object getAsObject(FacesContext fc, UIComponent uic, String value)
  {
    String name=value;
    POJO myPOJO=new POJO();
    myPOJO.setName(name);
    myPOJO.setId( getPOJOId() );
    return myPOJO;
 }

 public String getAsString(...)
 {
    //returns name to display
  }

  protected int getPOJOId()
  {
    FacesContext context = FacesContext.getCurrentInstance();
    POJOClass pojo = context.getApplication().evaluateExpressionGet(context, "#{backingBean.obj}", POJOClass.class);
    return pojo.getId();
  }
}

If you have many POJOClass instances, store the ID in a hidden component and access its value to get the ID. 如果您有许多POJOClass实例,请将ID存储在一个隐藏的组件中并访问其值以获取ID。

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

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