FacesConverter forClass does not work with Composite Componet

I have a simple composite component that should display an inputText. When putting a value and pressing commandButton, the following exception is throw:

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

When I use h: inputText instead of d: myInputText, it works fine.

Is it possible to use FacesConverter and the forClass attribute for a composite component? I do not like to use the converter attribute or convertId of the f: converter tag. Does anyone help me?

Page Code:

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

Compound code:

 <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 Code:

 @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); } } 

Converter Code:

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

Entity Code:

  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 + '}'; } } 

I am using Mojarra 2.1.14, Glassfish 3.1 and CDI. Best wishes.

+4
source share
1 answer

I was able to reproduce it in Mojarra 2.1.14. This is a mistake in Mojarra. It works fine in MyFaces 2.1.9. I reported this to the Mojarra guys as issue 2568 . Meanwhile, there is no other option than explicitly specifying <f:converter for> in the client or navigating to MyFaces (which also has its own set of specific features / problems).

+2
source

Source: https://habr.com/ru/post/1443442/


All Articles