selectItem的itemLabel

selectItem的itemLabel

本文介绍了在& lt; f:selectItem的itemLabel& gt;中设置日期格式使用DateTimeConverter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个< h:selectOneMenu> ,其中已加载了< f:selectItems> CategoryHistory 对象.我只将 Date date 字段显示为 itemLabel .那可行,但是我想格式化日期:我创建了一个扩展了 javax.faces.convert.DateTimeConverter 的转换器,并更改了构造函数中的字段.但是我的日期仅以默认格式显示:(

DateAndTimeConverter.java

  import javax.faces.bean.ManagedBean;导入javax.faces.convert.Converter;导入javax.faces.convert.DateTimeConverter;导入javax.faces.convert.FacesConverter;@FacesConverter(value ="dateAndTimeconverter")@ManagedBean公共类DateAndTimeConverter扩展了DateTimeConverter实现Converter {公共DateAndTimeConverter(){this.setDateStyle("short");} 

xhtml

 < h:selectOneMenu valueChangeListener =#{admin.categoryHistoryListener}"onchange ="submit()" value =#{admin.categoryHistory.id}" converter =#{dateAndTimeconverter}">< f:selectItems value =#{admin.categoryHistories}" var ="n"itemValue =#{n.id}" itemLabel =#{n.date}"></f:selectItems></h:selectOneMenu> 

当我尝试时它也不起作用:

 < h:selectOneMenu valueChangeListener =#{admin.categoryHistoryListener}"onchange ="submit()" value =#{admin.categoryHistory.id}">< f:converter converterId ="dateAndTimeconverter"/>< f:selectItems value =#{admin.categoryHistories}" var ="n"itemValue =#{n.id}" itemLabel =#{n.date}"></f:selectItems></h:selectOneMenu> 

CategoryHistory (日期历史记录),并且具有长日期+ ...

谢谢

解决方案

不幸的是,JSF转换器仅适用于输入,而不适用于输入标签./p>

您需要用其他方法解决.例如.使用 SimpleDateFormat 的吸气剂设置日期格式.或者,如果您的环境支持EL 2.2,只需直接调用converter方法(您已经将其作为托管bean):

 < f:selectItems value =#{admin.categoryHistories}" var ="n" itemValue =#{n.id}"itemLabel =#{dateAndTimeconverter.getAsString(facesContext,component,n.date)}"> 

如果您碰巧使用了JSF实用程序库 OmniFaces ,那么您也可以使用其 of:formatDate() 函数.例如:

 < f:selectItems value =#{admin.categoryHistories}" var ="n" itemValue =#{n.id}"itemLabel =#{of:formatDate(n.date,'d MMM yyyy')}"> 

I have a <h:selectOneMenu> that has <f:selectItems> with CategoryHistory objects loaded in it. I only show the Date date field as itemLabel.That works but I want to format the date:I created a converter that extends javax.faces.convert.DateTimeConverter and change the fields in the constructor. But my dates only show in default format :(

DateAndTimeConverter.java

import javax.faces.bean.ManagedBean;
import javax.faces.convert.Converter;
import javax.faces.convert.DateTimeConverter;
import javax.faces.convert.FacesConverter;

@FacesConverter(value = "dateAndTimeconverter")
@ManagedBean
public class DateAndTimeConverter extends DateTimeConverter implements Converter {

 public DateAndTimeConverter(){
  this.setDateStyle("short");
 }

xhtml

 <h:selectOneMenu valueChangeListener="#{admin.categoryHistoryListener}"
    onchange="submit()" value="#{admin.categoryHistory.id}" converter="#{dateAndTimeconverter}">
  <f:selectItems value="#{admin.categoryHistories}" var="n"
     itemValue="#{n.id}" itemLabel="#{n.date}">
  </f:selectItems>
 </h:selectOneMenu>

It also doesn't work when I try:

<h:selectOneMenu valueChangeListener="#{admin.categoryHistoryListener}"
    onchange="submit()" value="#{admin.categoryHistory.id}">
  <f:converter converterId="dateAndTimeconverter"/>
  <f:selectItems value="#{admin.categoryHistories}" var="n"
     itemValue="#{n.id}" itemLabel="#{n.date}">
  </f:selectItems>
</h:selectOneMenu>

CategoryHistory Has a Date date, and Long id +...

Thank you

解决方案

Unfortunately, the JSF converters only applies on the input value, not on the input label.

You'll need to solve this other ways. E.g. a getter which uses SimpleDateFormat to format the date. Or if your environment supports EL 2.2, simply invoke the converter method directly (you've it as managed bean already):

<f:selectItems value="#{admin.categoryHistories}" var="n" itemValue="#{n.id}"
    itemLabel="#{dateAndTimeconverter.getAsString(facesContext, component, n.date)}">

If you happen to use JSF utility library OmniFaces, then you can also use its of:formatDate() function. E.g.:

<f:selectItems value="#{admin.categoryHistories}" var="n" itemValue="#{n.id}"
    itemLabel="#{of:formatDate(n.date, 'd MMM yyyy')}">

这篇关于在&amp; lt; f:selectItem的itemLabel&amp; gt;中设置日期格式使用DateTimeConverter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 12:55