java - 对象作为 <f :selectItems> 中的 itemValue

标签 java jsf jakarta-ee jsf-2

<分区>

是否可以在标签中将对象作为itemValue

例如我有一个 Foo 类:

public class Foo {
  private int id;
  private String name;
  private Date date;
}

还有一个类Bar

public class Bar {
  private Foo foos;
}

public class BarBean {
  private Set<Foo> foos;
}

现在在一个名为 BarBean 的 Bean 中,我需要像这样从 User 获取当前 Bar 的 Foo:

<h:selectOneMenu value="#{barBean.bar.foo}" required="true">
 <f:selectItems value="#{barBean.foos}"  var="foo" itemLabel="#{foo.name}" itemValue="#{foo}" />
</h:selectOneMenu>

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

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() + "";
  }

}

还有我的小脸:

    <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"/>

最佳答案

这是可能的。

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

Check implementation and very good article here

关于java - 对象作为 <f :selectItems> 中的 itemValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4635302/

相关文章:

java - 如何将嵌套 yaml 结构映射到 Java ConfigurationProvider Builder?

java - 替换字符串中的多个单词 ln java like php str_replace

Java instanceof 在我的情况下不起作用

css - 如何在 <p :commandLink>? 中显示整个背景图像

java - hibernate 无法重新连接到 mysql 数据库

java - 我在 Java 中的 do while 循环有什么问题

java - 是否有从 inputText 到 URL 的任何标准 JSF 转换器?

java - JSF 真的准备好迎接高性能社交网络项目了吗?

java - GWT 中的 Hibernate 查询

java - 在 EJB 中使用 "private static"辅助方法