对象为<f:selectItems>中的itemValue

ehs*_*n7b 7 java jsf java-ee jsf-2

是否可以将对象作为itemValue标记?

例如,我有一个类Foo:

public class Foo {
  private int id;
  private String name;
  private Date date;
}
Run Code Online (Sandbox Code Playgroud)

而另一个班吧

public class Bar {
  private Foo foos;
}

public class BarBean {
  private Set<Foo> foos;
}
Run Code Online (Sandbox Code Playgroud)

现在在一个名为BarBean的Bean中,我需要有一个从用户那里得到当前Bar的Foo,如下所示:

<h:selectOneMenu value="#{barBean.bar.foo}" required="true">
 <f:selectItems value="#{barBean.foos}"  var="foo" itemLabel="#{foo.name}" itemValue="#{foo}" />
</h:selectOneMenu>
Run Code Online (Sandbox Code Playgroud)

---------------编辑:

my converter:

package ir.khorasancustoms.g2g.converters;

import ir.khorasancustoms.g2g.persistance.CatalogValue;
import java.util.ResourceBundle;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

@FacesConverter("ir.khorasancustoms.CatalogValueConverter")
public class CatalogValueConverter implements Converter {

  @Override
  public Object getAsObject(FacesContext context, UIComponent component, String value) {
    SessionFactory factory = new Configuration().configure().buildSessionFactory();
    Session session = factory.openSession();

    try {
      int id = Integer.parseInt(value);
      CatalogValue catalogValue = (CatalogValue) session.load(CatalogValue .class, id);
      return catalogValue;
    } catch (Exception ex) {
      Transaction tx = session.getTransaction();
      if (tx.isActive()) {
        tx.rollback();
      }
      ResourceBundle rb = ResourceBundle.getBundle("application");
      String message = rb.getString("databaseConnectionFailed");
      FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, message, message));
    } finally {
      session.close();
    }

    return null;
  }

  @Override
  public String getAsString(FacesContext context, UIComponent component, Object value) {
    return ((CatalogValue) value).getId() + "";
  }

}
Run Code Online (Sandbox Code Playgroud)

和我的脸:

    <h:outputText value="#{lbls.paymentUnit}:"/>
    <h:selectOneMenu id="paymentUnit" label="#{lbls.paymentUnit}" value="#{price.price.ctvUnit}" required="true">
      <f:selectItems value="#{price.paymentUnits}"/>
      <f:converter converterId="ir.khorasancustoms.CatalogValueConverter"/>
    </h:selectOneMenu>
    <h:message for="paymentUnit" infoClass="info" errorClass="error" warnClass="warning" fatalClass="fatal"/>
Run Code Online (Sandbox Code Playgroud)

Jig*_*shi 5

是的,这是可能的.

您需要编写一个将Foo转换为的转换器 SelectItem

在这里检查实施和非常好的文章

  • 你指出的文章是现货,但你自己的总结并不完全正确.转换器不会转换为``SelectItem``,而是将模型对象(此处为Foo)转换为String表示形式.对于此String表示,将获取模型对象的键或ID,并将此ID转换回对象,使用DAO. (3认同)