【问题标题】:FacesConverter forClass doesn't work with Composite ComponentFacesConverter forClass 不适用于复合组件
【发布时间】:2012-10-22 02:09:14
【问题描述】:

我有一个简单的复合组件,它必须呈现一个 inputText。当输入值并按下 commandButton 时,会抛出以下异常:

java.lang.IllegalArgumentException: Cannot convert 1 of type class java.lang.String to class sample.entity.Product

当我使用 h:inputText 代替 d:myInputText 时,它工作正常。

复合组件是否可以使用 FacesConverter 和属性 forClass?我不喜欢使用标签 f:converter 的转换器属性或转换器 ID。 有人帮帮我吗?

页面代码:

<h:form>
  <h:messages />
  Product Id: <h:myInputText value="#{productController.product}"/>
  <h:commandButton value="Submit" action="#{productController.someAction()}" />
  Product Description: <h:outputText value="#{productController.product.description}"/>
</h:form>

复合代码:

<composite:interface>
   <composite:attribute name="value"/>
   <composite:editableValueHolder name="value" targets="#{cc.clientId}:value"/>
</composite:interface>

<composite:implementation>
   <div id="#{cc.clientId}">
      <h:inputText id="value" value="#{cc.attrs.value}"/>
      <h:message for="#{cc.clientId}:value" />
    </div>
</composite:implementation>

ManagedBean 代码:

@Named("productController")
@RequestScoped
public class ProductController {

  private Product product;

  public Product getProduct() {
    if (product == null) {
        product = new Product();
    }
    return product;
  }

  public void setProduct(Product product) {
    this.product = product;
  }

  public void someAction() {
    System.out.println("Product " + product);
  }
}

转换器代码:

@FacesConverter(forClass = Product.class)
public class ProductConverter implements Converter {

  @Override
  public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
    System.out.println("[DEBUG] getAsObject: " + value);
    if (value == null || "".equals(value)) {
        return null;
    }
    //TODO: some logic to get entity from database.
    return new Product(new Long(value));
  }

  @Override
  public String getAsString(FacesContext fc, UIComponent uic, Object o) {
    System.out.println("[DEBUG] getAsString: " + o);
    if (o == null) {
        return null;
    }
    return String.valueOf(((Product) o).getId());
  }
}

实体代码:

 public class Product {

      private Long id;
      private String description;

      public Product() {
      }

  public Product(Long id) {
    this.id = id;
  }

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    this.description = description;
  }

  @Override
  public int hashCode() {
    int hash = 7;
    hash = 29 * hash + (this.id != null ? this.id.hashCode() : 0);
    return hash;
  }

  @Override
  public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final Product other = (Product) obj;
    if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
        return false;
    }
    return true;
  }

  @Override
  public String toString() {
    return "Product{" + "id=" + id + '}';
  }
}

我使用 Mojarra 2.1.14、Glassfish 3.1 和 CDI。 最好的问候。

【问题讨论】:

    标签: jsf-2 converters composite-component


    【解决方案1】:

    我能够在 Mojarra 2.1.14 中重现它。这是 Mojarra 中的一个错误。它在 MyFaces 2.1.9 中运行良好。我已经以issue 2568 的身份向 Mojarra 人报告了它。与此同时,除了在客户端中明确指定 &lt;f:converter for&gt; 或转移到 MyFaces(尽管它也有自己的一组特定怪癖/问题)之外,没有其他选择。

    【讨论】:

    • 我尝试了您的解决方案,但还没有奏效。顺便说一句,如果我指定类型我的复合不能用于像客户端这样的其他实体。
    • 你是对的。请参阅更新的答案。我会检查原因,然后将其报告给 Mojarra 人。
    猜你喜欢
    • 2015-10-14
    • 1970-01-01
    • 2011-11-18
    • 2012-04-30
    • 1970-01-01
    • 2013-09-30
    • 1970-01-01
    • 2021-02-04
    • 2020-04-27
    相关资源
    最近更新 更多