jsf converter getAsString() always have null as a parameter - jsf-2

I wanted to create my own date converter.I have input field with f:converter id="MyConverter". When I type "today" value I want show today date. Method getAsObject is working good. I've got value "today" from input field and return new LocalDate(), but method getAsObject() have always null as a value. What should I do to fix it?
#FacesConverter(forClass = Date.class, value = "MyConverter")
public class MyConverter implements Converter {
#Override
public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
if (value == null) {
return null;
}
if (value.equals("today")) {
return new LocalDate();
}
//do something else
}
#Override
public String getAsString(FacesContext context, UIComponent component, Object value) throws ConverterException {
// value is always null!
if (value instanceof LocalDate) {
LocalDate date = (LocalDate) value;
return date.toString();
} else {
return "";
}
}
EDIT:
jsf tag (it is a little bit complicated) it's actually 2 date input fields (Date interval component), and I want to use MyDateConverter on it:
<cc:interface componentType="DateIntervalComponent">
<cc:attribute name="value" required="true" />
<cc:attribute name="placeholder" />
<cc:attribute name="labelFromDate" default="From Date" />
<cc:attribute name="labelToDate" default="To Date" />
</cc:interface>
<cc:implementation>
<div id="#{cc.userId}">
<b:input id="fromDate" binding="#{cc.FromDateComponent}"
label="#{cc.attrs.labelFromDate}">
<f:converter converterId="MyConverter" />
<f:ajax render="#{cc.userId}" />
</b:input>
<b:input id="toDate" binding="#{cc.ToDateComponent}"
label="#{cc.attrs.labelToDate}">
<f:converter converterId="MyConverter" />
<f:ajax render="#{cc.userId}" />
</b:input>
</div>
</cc:implementation>
#FacesComponent("DateIntervalComponent")
public class DateIntervalComponent extends UIInput implements NamingContainer {
private UIInput fromDateComponent;
private UIInput toDateComponent;
public DateIntervalComponent() {
setRendererType(null);
}
#Override
public String getFamily() {
return UINamingContainer.COMPONENT_FAMILY;
}
#Override
public DateInterval getValue() {
return (DateInterval) super.getValue();
}
#Override
public void processEvent(ComponentSystemEvent event) throws AbortProcessingException {
super.processEvent(event);
}
#Override
public void encodeBegin(FacesContext context) throws IOException {
if (fromDateComponent != null && toDateComponent != null) {
DateInterval value = getValue();
if (!fromDateComponent.isLocalValueSet()) {
fromDateComponent.setValue(value != null ? value.getStart() : null);
}
if (!toDateComponent.isLocalValueSet()) {
toDateComponent.setValue(value != null ? value.getEnd() : null);
}
}
super.encodeBegin(context);
}
#Override
public DateInterval getSubmittedValue() {
Object startValue = fromDateComponent.getValue();
Object endValue = toDateComponent.getValue();
return new DateInterval((Date) startValue, (Date) endValue);
}
#Override
public void validate(FacesContext context) {
super.validate(context);
if (!isValid()) {
fromDateComponent.setValid(false);
toDateComponent.setValid(false);
return;
}
}
#Override
public void resetValue() {
super.resetValue();
fromDateComponent.resetValue();
toDateComponent.resetValue();
}
public UIInput getFromDateComponent() {
return fromDateComponent;
}
public UIInput getToDateComponent() {
return toDateComponent;
}
public void setToDateComponent(UIInput toDateComponent) {
this.toDateComponent = toDateComponent;
}
public void setFromDateComponent(UIInput fromDateComponent) {
this.fromDateComponent = fromDateComponent;
}
}
regards

You need to keep track of the state of your attributes as soon as the FacesComponent itself is STATELESS.
I use the following strategy to keep state of FacesComponent attibutes
First of all, create a generic class based on UINamingContainer (if this is your type of component).
public class MyUINamingContainer extends UINamingContainer {
#SuppressWarnings("unchecked")
/**
* Searches for an attribute and return it.
*/
protected <T> T getAttribute(String attName) {
return (T) getStateHelper().eval(attName);
}
#SuppressWarnings("unchecked")
/**
* Evaluate an attribute and return it if found.
*/
protected <T> T getAttribute(String localName, String attName) {
return (T) getStateHelper().eval(localName, getAttributes().get(attName));
}
/**
* Refresh the attributes state
*
* #param <T>
* #param attName attribute's name. This name will identify the attribute into the map.
It must be different from the name used to the attribute on the view, otherwise
it will cause a infinite loop.
* #param value attribute's value.
*/
protected <T> void setAttribute(String attName, T value) {
getStateHelper().put(attName, value);
}
}
Then, make my FacesComponent class inherit from it, so it would know the stateful getters and setters:
#FacesComponent("myComponent")
public class MyComponent extends MyUINamingContainer {
public SomeType getMyAttributeOnStateMap() {
return getAttribute("myAttributeOnStateMap","myAttribute");
}
public void setMyAttributeOnStateMap(SomeType myAttribute) {
setAttribute("myAttributeOnStateMap", myAttribute);
}
}
Into the component xhtml code:
<cc:interface componentType="myComponent">
<cc:attribute name="myAttribute"/>
</cc:interface>
<cc:implementation>
<h:input value="#{cc.myAttributeOnStateMap}" />
<cc:implementation>
PS.: Note that the attribute has a different name into the map. This is because it will get stuck in a loop it you use the same name for the map attribute and the view one. The method getAttribute(String,String) would always look for the same attribute and never find anything, leading you to a overflow.
PS2.: Note that the name used as the value into the h:input was the name used to store it into the map.

Related

Add attribute dynamically to component in converter?

I wonder if should be possible to add an attribute to a component inside a converter? So inside the getAsString I would use uiComponent.addAttribute(). This seems to work 50% for me, the initial value is set, but when the converter is called later setting a new value the initial value is still retrieved.
you should not do it this way since it breaks separation of duties. you should use a bean or a scope attribute instead.
but maybe this suits:
<h:inputText value="#{bean.someValue}" converter="#{bean}">
<f:attribute name="attrName" value="#{bean.attrValue}"/>
</h:inputText>
and
#ManagedBean
public class Bean implements Converter
{
private String someValue;
private String attrValue;
#Override
public String getAsString(FacesContext context, UIComponent component, Object value)
{
attrValue = "uppercase";
return someValue.toUpperCase();
}
#Override
public Object getAsObject(FacesContext context, UIComponent component, String value)
{
attrValue = "lowercase";
return value.toLowerCase();
}
public String getSomeValue()
{
return someValue;
}
public void setSomeValue(String someValue)
{
this.someValue = someValue;
}
public String getAttrValue()
{
return attrValue;
}
public void setAttrValue(String attrValue)
{
this.attrValue = attrValue;
}
}

Jsf custom converter life cyle

From one of my previous questions on this site I realized I don't know anything about custom converter life cycle. I searched a bit on internet and found nothing useful.
I would like to understand if custom converters are created once for all and recycled any time they are needed or if they are created on the fly and destroyed.
I suppose their main purpose is to perform some easy and light tasks, so it would make no difference if the a new instance of the converter is created each time it is found inside a jsf page sent to the user.
But I would like to use a custom converter to solve a common task in what it is an unpaved way. My custom convert would have an heavy initialization logic, so I have to be sure about its life-cycle. It must be created once for all and not every time it is needed. Is it possible ?
Depending on the answers I will receive I can abort the idea of using custom converter or decide to move the heavy initialization logic in a singletone.
Converters are created once for each time you reference them when using #FacesConverter annotation. That means if you execute slow code there it'll bring you into problems.
Alternatively, you can annotate them as #ManagedBean with the scope you want and use them with an EL reference instead of raw converter id. If you want to initialize them in some way, the solution for you would be setting them the scope for the whole application and making them eagerly initialized, so they'll be created when application starts up:
Converter:
#ManagedBean(eager = true)
#ApplicationScoped
public class WorkerConverter implements Converter {
public WorkerConverter() {
System.out.println("Building converter...");
}
#Override
public Object getAsObject(FacesContext context, UIComponent component,
String value) {
Integer id = Integer.parseInt(value);
if (id == 1) {
return new Worker(1, "John");
} else {
return new Worker(1, "Larry");
}
}
#Override
public String getAsString(FacesContext context, UIComponent component,
Object value) {
return ((Worker) value).getId().toString();
}
}
Managed bean:
#ManagedBean
#ViewScoped
public class SelectWorkerBean {
public static class Worker {
Integer id;
String name;
public Worker(Integer id, String name) {
this.id = id;
this.name = name;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Worker other = (Worker) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
#Override
public int hashCode() {
return id;
}
#Override
public String toString() {
return "Worker [name=" + name + "]";
}
}
private Worker selectedWorker;
private List<Worker> workers = Arrays.asList(new Worker(1, "John"),
new Worker(2, "Larry"));
public Worker getSelectedWorker() {
return selectedWorker;
}
public List<Worker> getWorkers() {
return workers;
}
public void send() {
System.out.println(selectedWorker + " selected");
}
public void setSelectedWorker(Worker selectedWorker) {
this.selectedWorker = selectedWorker;
}
}
Page:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Test page</title>
</h:head>
<h:body>
<h:form>
<h:selectOneMenu value="#{selectWorkerBean.selectedWorker}"
converter="#{workerConverter}">
<f:selectItems value="#{selectWorkerBean.workers}" var="worker"
itemLabel="#{worker.name}" />
</h:selectOneMenu>
<h:commandButton value="send" action="#{selectWorkerBean.send}" />
</h:form>
</h:body>
</html>

How to get the objects in the targetList of a rich:pickList?

I filled my rightList of the PickList with Objects from a web service, but when i select some elements, i want to get these elements in myManagedBean, because i'll affect them to an Object.
JSF :
<h:form>
<rich:panel>
<h:panelGrid columns="2" styleClass="criteresSaisie"
rowClasses="critereLigne" columnClasses="titreColonne,">
<h:outputLabel for="libelleCoplement" value=" "
size="20" />
<rich:pickList d="libelleCoplement" sourceCaption="Compléments"
targetCaption="Compléments sélectionnés"
value="#{listeCmpltDispoModel.listeCmpltSelect}" size="15"
addText=">" addAllText=">>" removeText="<"
removeAllText="<<" listWidth="270px" listHeight="110px"
orderable="true">
<f:converter converterId="cmpltsTitresConcerter" />
<f:selectItems value="#{listeCmpltDispoModel.listeCmpltDispo}"
var="liste" itemLabel="#{liste.libelleComplement}"
itemValue="#{liste.cdComplement}"/>
</rich:pickList>
</h:panelGrid>
<h:panelGroup>
<div align="right">
<h:panelGrid columns="8">
<h:commandButton value="Valider"
action="#{saisieCmpltsTitreCtrl.valider}" />
</h:panelGrid>
</div>
</h:panelGroup>
</rich:panel>
</h:form>
The bean :
#ManagedBean(name = "listeCmpltDispoModel")
#SessionScoped
public class ListeCmpltDispoModel implements Serializable {
private static final long serialVersionUID = 1L;
private Long cdComplement;
private String libelleComplement;
private int nbCompl;
private List<ComplementsDispoSortieDTO> listeCmpltDispo ;
private List<ComplementsDispoSortieDTO> listeCmpltSelect ;
public ListeCmpltDispoModel() {
}
public Long getCodeComplement() {
return cdComplement;
}
public void setCodeComplement(Long cdComplement) {
this.cdComplement = cdComplement;
}
public String getLibelleComplement1() {
return libelleComplement;
}
public void setLibelleComplement1(String libelleCoplement) {
this.libelleComplement = libelleCoplement;
}
public Long getCdComplement() {
return cdComplement;
}
public void setCdComplement(Long cdComplement) {
this.cdComplement = cdComplement;
}
public String getLibelleComplement() {
return libelleComplement;
}
public void setLibelleComplement(String libelleComplement) {
this.libelleComplement = libelleComplement;
}
public List<ComplementsDispoSortieDTO> getListeCmpltDispo() {
return listeCmpltDispo;
}
public void setListeCmpltDispo(List<ComplementsDispoSortieDTO> listeCmpltDispo) {
this.listeCmpltDispo = listeCmpltDispo;
}
public int getNbCompl() {
return nbCompl;
}
public void setNbCompl(int nbCompl) {
this.nbCompl = nbCompl;
}
public List<ComplementsDispoSortieDTO> getListeCmpltSelect() {
return listeCmpltSelect;
}
public void setListeCmpltSelect(List<ComplementsDispoSortieDTO> listeCmpltSelect) {
this.listeCmpltSelect = listeCmpltSelect;
}
}
Converter :
#FacesConverter(value="cmpltsTitresConcerter")
public class CmpltsTitresConcerter implements Converter {
#SuppressWarnings("null")
public Object getAsObject(FacesContext context, UIComponent component,
String value){
ComplementsDispoSortieDTO cmpltSelect = null;
cmpltSelect.setCdComplement(Long.parseLong(value));
return cmpltSelect;
}
public String getAsString(FacesContext arg0, UIComponent arg1, Object obj) {
return String.valueOf(((ComplementsDispoSortieDTO) obj).getCdComplement());
}
}
Any help is greatly apprectiated!
Roughtly you need 3 things :
Custom converter for your object (Object to String, String to Object)
Getter/Setter with the List of your Objects choices
Getter/Setter with the List of your Objects selected
Everything is perfectly described here : RichFaces Showcase - pickList
EDIT :
Adding this should fix your problem :
<rich:pickList ...>
<f:converter converterId="cmpltsTitresConcerter" />
</rich:pickList>
also the converter property in <f:selectItems /> is not valid : f:selectItems
EDIT :
You should modify your converter like that to remove converting exceptions :
#FacesConverter(value="cmpltsTitresConcerter")
public class CmpltsTitresConcerter implements Converter
{
public Object getAsObject(FacesContext context, UIComponent component, String value)
{
ComplementsDispoSortieDTO cmpltSelect = null;
if(value != null)
{
cmpltSelect = new ComplementsDispoSortieDTO();
cmpltSelect.setCdComplement(Long.parseLong(value));
}
return cmpltSelect;
}
public String getAsString(FacesContext arg0, UIComponent arg1, Object obj)
{
String result = null;
if(obj != null)
{
result = String.valueOf(((ComplementsDispoSortieDTO) obj).getCdComplement());
}
return result;
}
}
Your selected objects are bound to the value attribute which must have a getter and setter.

FacesConverter forClass doesn't work with Composite Component

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
When i use h:inputText instead d:myInputText it's work fine.
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?
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>
Composite 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 use Mojarra 2.1.14, Glassfish 3.1 and CDI.
Best regards.
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).

The value returned by a h:selectOneMenu / f:selectItems is always null for a bean

I'm still trying to program an app with jsf2, ejb3.1, cdi and glassfish.
I have a form which have a selectOneMenu
<h:form prependId="false">
...
<f:validateBean>
...
<h:selectOneMenu value="#{bottleManagedBean.selectProducer}" id="selectproducerb"
validatorMessage="#{messages.bottleaddinvalideproducer}" immediate="true">
<f:converter binding="#{producerConverter}"/>
<f:selectItems value="#{bottleManagedBean.producerItems}" />
</h:selectOneMenu>
...
The of the selectItems component are well showned but when I submit the form the value of bottleManagedBean.selectProducer is always null.
My formbean
#Named("bottleManagedBean")
#RequestScoped
public class BottleManagedBean {
....
private List<Producer> producers;
public List<Producer> getProducers() {
if (producers == null) {
setProducers(producerService.list(Producer.class));
}
return producers;
}
public void setProducers(List<Producer> producers) {
this.producers = producers;
}
private Producer selectProducer;
public Producer getSelectProducer() {
return selectProducer;
}
private List<SelectItem> producerItems;
public List<SelectItem> getProducerItems() {
if (producerItems == null) {
producerItems = new ArrayList<SelectItem>();
for (Producer current : getProducers()) {
producerItems.add(new SelectItem(current.getId(), current.getName()));
}
}
return producerItems;
}
public void setProducerItems(List<SelectItem> producerItems) {
this.producerItems = producerItems;
}
...
The converter
#Named("producerConverter")
public class ProducerConverter implements Converter {
#Inject
BusinessService<Producer> service;
private static Logger trace = Logger.getLogger(ProducerConverter.class.getCanonicalName());
#Override
public Object getAsObject(FacesContext fc, UIComponent uic, String id) {
try {
return service.findByID(Producer.class, Integer.parseInt(id));
} catch (NumberFormatException e) {
e.printStackTrace();
throw new ConverterException(e);
}
}
#Override
public String getAsString(FacesContext fc, UIComponent uic, Object o) {
String asString = null;
if (o != null) {
asString = String.valueOf(o);
}
return asString;
}
}
I tried to debug the app .When I submit the form , the application never goes to the setter.
I also tried to add a valuechangelistener and my app never called this method.
Finally, I tried to submit an integer (eg. the id of my bean) and the value is well filled when I submit.1
So,... what's wrong ?
Thanks in advance for your help
here is how you should use the valuechangelistener properly in jsf2
Add <f:ajax listener="#{bottleManagedBean.selectProducer}" /> to your <h:selectOneMenu
Like this:
<h:selectOneMenu value="#{bottleManagedBean.selectProducerValueChange}" id="selectproducerb"
validatorMessage="#{messages.bottleaddinvalideproducer}">
<f:ajax listener="#{bottleManagedBean.selectProducerValueChange}" />
<f:converter binding="#{producerConverter}"/>
<f:selectItems value="#{bottleManagedBean.producerItems}" />
</h:selectOneMenu>
and implement the selectProducerValueChange in your bean...

Resources