Tag Archives: jsf

Creating JSF beans in Java. Best practice.

jsf_isolation
Commonly, the post is related to task when we deal with JSF managed beans.
TO – transfer object for DAO. It presents a big temptation to use TO as a field of JSF Bean.

For ex. in this case we have a TO:

public class SomeTO {
  private String id;
  private String name;
  private String value;
}

TO is used by DAO:

public class SomeDAO {
  public SomeTO get() {
    return new SomeTO();
  }
}

A JSF Bean (wrong way!):

public class SomeBean implements Serializable {
  private SomeTO to;

  public SomeTO getTo() {
    if (to == null) {
      to = new SomeTO();
    }
    return to;
  }
}

And so, on page:

 <p:inputText id="name" value="#{SomeBean.to.name}" />

BUT! A little problem occurs here. We lost the most important thing – loose coupling
So, the right way is to use next:

public class SomeBean implements Serializable {
  private String id;
  private String name;
  private String value;
}

And then, on page:

 <p:inputText id="name" value="#{SomeBean.name}" />

Yes, we duplicate the code. But save it free if we decide to change TO anytime.
Be sure for the mistake!
JSF beans are the part of presentation and they must be independent out of core.

Find form component in JSF

ICEfaces-2.0-JSF-Ajax
To find form component in FacesContext use next pattern:

private UIForm findFormComponent(FacesContext context) {
    UIViewRoot view = context.getViewRoot();
    Map<String, String> params = context.getExternalContext().getRequestParameterMap();

    for (String clientId : params.keySet()) {
        UIComponent component = view.findComponent(clientId);
        if (component instanceof UIForm) {
            return (UIForm) component;
        }
    }

    return null;
}

Resource loading order in JSF 2 and PrimeFaces 3.x

dister-mechanic
To override primefaces style use next in h:head tag:


  
    
    
    
  
  
    
  
  
    
    
  

And it works fine, instead of using this:


  
  

See also, http://blog.primefaces.org/?p=1433

Input text readonly attribute in JSF


JSF rocks!
If you want to use readonly property in h:inputText, using readonly=”true” attribute brings you in confusing, because value of this element is unrestorable after request will performed.

Javascript way works in Firefox only! Skip it, here just for ex.

/**
 * Add readonly attribute to element
 */
function addReadonly(obj){
	$(obj).attr('readonly', 'readonly');
	return false;
}

/**
 * Remove readonly attribute to element
 */
function removeReadonly(obj){
	$(obj).removeAttr('readonly');
	return false;
}

// in element, add attrs
onkeydown="addReadonly(this);" onkeyup="removeReadonly(this);"

And to help us, use next JSF workaround.
Add property to bean:

    /**
     * Need for readonly properties processing
* Allows to restore value of input. */ public boolean isReadonly() { return FacesContext.getCurrentInstance().getRenderResponse(); }

In page add readonly attr:

readonly="#{MyBean.readonly}"

Thanx to http://stackoverflow.com/questions/10384032/data-in-hinputtext-readonly-true-disappears-when-command-button-is-clicked