禁用启用jsf f:convertDateTime

Anu*_*pam 11 jsf converter

我有两个按钮,在一个我需要<f:convertDateTime>工作但在另一个我需要禁用<f:convertDateTime>按钮单击.

我试过的属性rendered和disabled,但它没有工作,这是我的错误,因为它不能作为每个API文档.

此外,有没有办法覆盖类javax.faces.converter.DateTimeConverter,以便每当f:convertDateTime被触发我的类将被调用?

Bal*_*usC 3

我尝试了渲染和禁用的属性,但它不起作用,这是我的错误,因为根据 API 文档它不可用。

事实上,这种行为不受支持。不过,对于可能的解决方案,你自己基本上已经给出了答案:

另外,有没有办法覆盖该类javax.faces.converter.DateTimeConverter,以便每当f:convertDateTime触发时我的类都会被调用?

这是可能的,也将解决您最初的问题。只需将其注册为<converter>与faces-config.xmlon完全相同<converter-id>即可<f:convertDateTime>。

<converter>
    <converter-id>javax.faces.DateTime</converter-id>
    <converter-class>com.example.YourDateTimeConverter</converter-class>
</converter>
Run Code Online (Sandbox Code Playgroud)

其中您可以进行额外的条件检查,例如检查是否按下某个按钮,或者某个请求参数是否存在。如果您想继续默认<f:convertDateTime>作业,只需委托super给您的转换器extends即可DateTimeConverter。

例如在getAsObject():

public class YourDateTimeConverter extends DateTimeConverter {

    @Override
    public void getAsObject(FacesContext context, UIComponent component, String submittedValue) {
        // ...

        if (yourCondition) {
            // Do your preferred way of conversion here.
            // ...
            return yourConvertedDateTime;
        } else {
            // Do nothing. Just let default f:convertDateTime do its job.
            return super.getAsObject(context, component, submittedValue);
        }
    }

    // ...
}
Run Code Online (Sandbox Code Playgroud)