【问题标题】:Disable enable jsf f:convertDateTime禁用启用 jsf f:convertDateTime
【发布时间】:2016-12-24 22:51:31
【问题描述】:

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

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

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

【问题讨论】:

  • 你有示例 XHTML 吗?

标签: jsf converter


【解决方案1】:

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

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

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

这是可能的,也将解决您最初的问题。只需在 faces-config.xml 中将其注册为 <converter> 即可,恰好在 same <converter-id> 上注册为 <f:convertDateTime>

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

您可以在其中进行额外的条件检查,例如检查某个按钮是否被按下,或者某个请求参数是否存在。如果您想继续默认的 &lt;f:convertDateTime&gt; 作业,只需委托给 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);
        }
    }

    // ...
}

【讨论】:

    【解决方案2】:

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

    <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
    

    【讨论】:

      猜你喜欢
      • 2014-03-24
      • 2013-02-20
      • 2014-05-26
      • 1970-01-01
      • 2013-09-04
      • 1970-01-01
      • 2012-12-09
      • 1970-01-01
      • 2011-04-03
      相关资源
      最近更新 更多