JSF验证错误,价值损失 [英] JSF validation error, lost value

查看:68
本文介绍了JSF验证错误,价值损失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有复合键的更新表单,因为我每个复合键都有隐藏字段,所以所有复合键都显示在输出框中.验证错误后,这些输出框值为空.我该如何解决.我在同一页面上,所以它不必具有值.

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.

推荐答案

这确实是h:inputHidden的非直观行为(我曾经提交过

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.

第一种方法是在h:inputHidden上使用binding:

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

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

这样,该值将不会经历不必要的验证周期.但是,这需要更改获取/设置备用Bean代码中的值的方式.例如:

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

第二种方法(也是恕我直言的首选方法)是使用 Tomahawk t:saveState代替

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

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

主要优点是您不需要更改后备bean代码中的任何内容.它将在应用请求值阶段之前尽早恢复该值.您只需要添加额外的库(如果尚未完成的话),但是Tomahawk所提供的优势不仅仅限于t:saveState,例如在基本JSF实现中缺少组件/功能t:inputFileUploadt:dataListt:dataTable preserveDataModel="true"t:selectOneRadio layout="spread"等,值得付出努力.

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.

第三种方法是将其存储在会话范围的Bean中,但实际上您不想对请求范围的变量执行此操作.它只会给"wtf?"最终用户在同一会话中打开多个选项卡/窗口时会遇到这种情况.

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.

这篇关于JSF验证错误,价值损失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆