FacesConverter forClass不适用于复合组件 [英] FacesConverter forClass doesn't work with Composite Component

查看:86
本文介绍了FacesConverter forClass不适用于复合组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的复合组件,它必须呈现一个inputText.当放置一个值并按下commandButton时,将引发以下异常:

I've got a simple composite component which has to render a inputText. When a put the value and press commandButton the follow exception is throw:

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

当我使用h:inputText而不是d:myInputText时,效果很好.

When i use h:inputText instead d:myInputText it's work fine.

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

Is possible use a FacesConverter and attribute forClass for composite component? I do not like to use converter attribute or converterId of tag f:converter. Anybody help me?

页面代码:

<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. 最好的问候.

I use Mojarra 2.1.14, Glassfish 3.1 and CDI. Best regards.

推荐答案

我能够在Mojarra 2.1.14中重现它.这是Mojarra中的错误.在MyFaces 2.1.9中可以正常工作.我已将其作为 issue 2568 报告给Mojarra伙计.同时,除了在客户端中明确指定<f:converter for>或移动到MyFaces(虽然它也有其自己的一组特定的怪癖/问题)之外,没有其他选择.

I was able to reproduce it in Mojarra 2.1.14. This is a bug in Mojarra. It works fine in MyFaces 2.1.9. I've reported it to Mojarra guys as issue 2568. In the meanwhile, there's not really another option than explicitly specifying a <f:converter for> in the client or moving to MyFaces (which has its own set of specific quirks/issues as well though).

这篇关于FacesConverter forClass不适用于复合组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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