禁用启用JSF f:convertDateTime

11

我有两个按钮,其中一个需要使用<f:convertDateTime>,而另一个需要在按钮点击时禁用<f:convertDateTime>

我尝试了rendereddisabled属性,但它们没有起作用,这是我的错误,因为根据API文档,它们不可用。

此外,是否有一种方法可以覆盖javax.faces.converter.DateTimeConverter类,以便每当触发f:convertDateTime时都会调用我的类?


你有XHTML的样例吗? - PDStat
2个回答

3
我尝试使用渲染和禁用属性,但它没有起作用,这是我的错误,因为根据API文档并不可用。
实际上,这种行为不受支持。然而,关于可能的解决方案,你基本上已经给出了答案:
此外,有没有一种方法可以覆盖javax.faces.converter.DateTimeConverter类,以便每当触发f:convertDateTime时就会调用我的类?
这是可能的,并且也将解决您最初的问题。只需在faces-config.xml中将其注册为,并与具有相同的
<converter>
    <converter-id>javax.faces.DateTime</converter-id>
    <converter-class>com.example.YourDateTimeConverter</converter-class>
</converter>

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

例如,在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);
        }
    }

    // ...
}

0

我猜测你是基于按钮点击来显示一些文本(如果我错了请纠正我)。那么应该是这样的:

<h:commandButton value="Convert" action="#{bean.doBtnConvert}" />

<h:commandButton value="Don't convert" action="#{bean.doBtnDontConvert}" />

<h:panelGroup id="pgText">
    <h:outputText value="#{bean.someDateTime}" rendered="#{bean.convert}">
        <f:convertDateTime pattern="dd.MM.yyyy HH:mm:ss" />
    </h:outputText>

    <h:outputText value="#{bean.someDateTime}" rendered="#{not bean.convert}"> />
</h:panelGroup>

在bean中,您有以下字段和方法:

private Date someDate;
private boolean convert;

public String doBtnConvert(){
    setConvert(true);
    String viewId = FacesContext.getCurrentInstance().getViewRoot().getViewId();
    return viewId + "?faces-redirect=true";
}

public String doBtnDontConvert(){
    setConvert(false);
    String viewId = FacesContext.getCurrentInstance().getViewRoot().getViewId();
    return viewId + "?faces-redirect=true";
}

// getter and setter for 'someDate' and 'convert' fields

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接