是否有必要在JSF定制组件中的encode()中调用setValid(true)? [英] Is it necessary to invoke setValid(true) in decode() in a JSF Custom Component?
本文介绍了是否有必要在JSF定制组件中的encode()中调用setValid(true)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在使用自定义组件时,我遇到了问题,即UIInput#getValid()
从不重置自身.问题是:在申请请求值"阶段是否必须自己调用setValid(true)
?
While working with a custom component I have the problem, that UIInput#getValid()
never resets itself. Question is: do I have to invoke setValid(true)
myself during Apply Request Values Phase?
这是我已完成的步骤:
- 创建一个自定义组件并从
UIInput
继承
- 该组件在构造函数中调用
setConverter
- 转换器仅是为此组件设计的,并且如果不能将其从String转换为模型对象,则会抛出
ConverterException
- 没有验证人
-
decode()
被覆盖,并且如果FacesContext#isPostback
返回true,我们将使用请求值调用setSubmittedValue
- 在渲染响应期间,我检查了
isValid()
,如果抛出了ConverterException
,则返回true,我可以相应地渲染响应 - 但是:如果我提交另一个正确的值,则永远不会为自定义组件调用
setValid(true)
- Create a custom component and inherit from
UIInput
- The component invokes
setConverter
in the constructor - The converter was designed for this component alone and throws a
ConverterException
if it can't convert from String to the model object - There are no validators
decode()
is overriden, and ifFacesContext#isPostback
returns true we invokesetSubmittedValue
with the request value- During render response I check
isValid()
, it returns true if aConverterException
was thrown, and I can render the response accordingly - But: If I submit another correct value the
setValid(true)
is never called for the custom component
推荐答案
UIInput#decode()
的默认实现确实使用true
调用EditableValueHolder#setValid()
.
The default implementation of UIInput#decode()
indeed calls EditableValueHolder#setValid()
with true
.
因此,您有2个选择:
- 只需在
UIInput#decode()
中调用super.decode(context)
,而不是手动获取提交的值并使用它调用setSubmittedValue()
(因为这已经是UIInput#decode()
的默认行为). - 或者,在调用
setSubimttedValue()
之前在UIInput#decode()
中手动调用setValid(true)
.
- Simply invoke
super.decode(context)
in yourUIInput#decode()
instead of manually grabbing submitted value and invokingsetSubmittedValue()
with it (because that's already the default behavior ofUIInput#decode()
). - Or, manually invoke
setValid(true)
in yourUIInput#decode()
before invokingsetSubimttedValue()
.
这篇关于是否有必要在JSF定制组件中的encode()中调用setValid(true)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文