JSF 2 Default DateTime Converter Pattern

試著忘記壹切 提交于 2019-12-23 12:53:10

问题


My JSF pages display DateTime from managed beans in this format: "MM/dd/yyyy h:mm a"

I want avoid duplicate converter declaration in different pages: <f:convertDateTime type="both" pattern="MM/dd/yyyy h:mm a" dateStyle="short" timeStyle="medium" />

Is there a way to make the above converter default for all DateTime fields?

(Experience with JSF 2: 2 months.)


回答1:


Just extend the DateTimeConverter class behind <f:convertDateTime> and set the defaults in the constructor.

@FacesConverter("defaultDateConverter")
public class DefaultDateConverter extends DateTimeConverter {

    public DefaultDateConverter() {
        setPattern("MM/dd/yyyy h:mm a");
    }

}

Use it as <f:converter converterId="defaultDateConverter" />

Please note that I omitted other attributes as they are ignored anyway when pattern is specified.



来源:https://stackoverflow.com/questions/14990960/jsf-2-default-datetime-converter-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!