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