tags:

views:

294

answers:

1

I have a update form, with composite keys All composite keys are displayed in outputbox as I have hidden field for each composite keys. These outputbox values are empty after validation error. How do I resolve this. I am on the same page so doesn't it has to have the values.

+2  A: 

This is indeed a non-intuitive behaviour of the h:inputHidden (I've ever filed a issue against it at the Mojarra issue list, but they didn't seem to do anything with it). The whole problem is that the component's value unnecessarily is also taken into the entire validation cycle while there's no means of user-controlled input. It will get lost when the validation fails. There are at least three ways to fix this non-intuitive behaviour.

First way is to use the binding on the h:inputHidden instead:

<h:inputHidden binding="#{bean.hidden}" />

This way the value won't undergo the unnecessary validation cycle. This however requires changes in the way you get/set the values in the backing bean code. For example:

private HtmlInputHidden hidden = new HtmlInputHidden(); // +getter +setter.

public void setHiddenValue(Object hiddenValue) {
    hidden.setValue(hiddenValue);
}

public Object getHiddenValue() {
    return hidden.getValue();
}

Second (and IMHO the preferred way) is to use Tomahawk's t:saveState instead.

<t:saveState value="#{bean.property}" />

The major advantage is that you don't need to change anything in the backing bean code. It will restore the value early before the apply request values phase. You only need to add extra libraries if not done yet, but as Tomahawk provides much more advantages than only the t:saveState, such as the in basic JSF implementation missing components/features t:inputFileUpload, t:dataList, t:dataTable preserveDataModel="true", t:selectOneRadio layout="spread" and so on, it is worth the effort.

The third way is to store it in a session scoped bean, but you actually don't want to do that for request scoped variables. It would only give "wtf?" experiences when the enduser has multiple tabs/windows open in the same session.

BalusC