禁用启用jsf f:convertDateTime

时间:2016-08-18 05:59:44

标签: jsf converter

我有两个按钮,其中一个我需要<f:convertDateTime>才能工作,但在另一个按钮上我需要在点击按钮时禁用<f:convertDateTime>

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

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

2 个答案:

答案 0 :(得分:3)

  

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

确实,不支持此行为。但是,对于可能的解决方案,您基本上已经自己给出了答案:

  

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

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

<converter>
    <converter-id>javax.faces.DateTime</converter-id>
    <converter-class>com.example.YourDateTimeConverter</converter-class>
</converter>

其中您可以执行其他条件检查,例如检查是否按下了某个按钮,或者是否存在某个请求参数。如果您想继续执行默认的<f:convertDateTime>工作,只需委托super,前提是您的转换器extends来自DateTimeConverter

E.g。在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);
        }
    }

    // ...
}

答案 1 :(得分: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
相关问题