开发者

JSF validation error, lost value

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 e开发者_开发问答rror. How do I resolve this. I am on the same page so doesn't it has to have the values.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜