【问题标题】:Primefaces input mask for date converter日期转换器的 Primefaces 输入掩码
【发布时间】:2016-03-31 03:09:27
【问题描述】:

嗯。我在所有地方都找到了,但我仍然没有解决方案。我需要用 Primefaces 做一个输入掩码,并将值绑定到我的 bean 中的 Date 对象。

问题是:我使用带有所有验证、转换和格式的转换器以及我自己的代码。我的回答是:还有其他性能更好的解决方案。但愿如此。请帮忙。

P/D:我不需要选择日期。我需要输入掩码来执行此操作

【问题讨论】:

  • 您可以将 inputMask 与转换器、验证器和所有格式一起使用
  • 是的,我知道。我认为在一些“主要”解决方案中。当我编写我的解决方案时,我有很多代码。
  • “Prime”解决方案祝你好运
  • 谢谢,现在我会用最简单的方法来寻找更有效的解决方案。也谢谢你。

标签: jsf jsf-2 primefaces


【解决方案1】:

我用这个:

<div style="margin-bottom:1em;font-size: 1.2em;"> 
    <p:inputMask id="dob" mask="99/99/9999" value="#{viewMB.request.dateOfBirth}" style="width:8em;" >
        <f:convertDateTime pattern="MM/dd/yyyy" />
    </p:inputMask>
    <p:watermark  value="MM/DD/YYYY" for="dob" />
</div>

如果您愿意,您仍然可以添加自定义验证器。

【讨论】:

    【解决方案2】:

    嗯,这就是我的解决方案

    <h:form>
          <p:inputMask mask="99-99-9999" value="#{mask.date}" converterMessage="Invalid Date!" converter="dateconverter" />
          <h:commandButton actionListener="#{mask.submit()}" value="Submit" />
    </h:form>
    

    还有转换器

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        System.out.println(value);
        if (value != null) {
            try {
                if (validateDate(value)) {
                    return convertDate(value).toDate();
                }
                else{
                    throw new ConverterException("Invalid date!");
                }
            } catch (Exception e) {
                throw new ConverterException("Invalid date!");
            }
        }
        throw new ConverterException("Null String!");
    }
    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        System.out.println(value);
        if (value != null) {
            try {
                Date now = (Date)value;
                DateTime d = new DateTime(now);
                return d.getDayOfMonth() + "-" + d.monthOfYear() + "-" + d.getYear();
            } catch (Exception e) {
                throw new ConverterException("Convertion failure!");
            }
        }
        throw new ConverterException("Null object!");
    }
    private boolean validateDate(String param) throws ParseException{
        //Param is a date format from input mask
        String[] values = param.split("-");
        int day = Integer.valueOf(values[0]);
        int month = Integer.valueOf(values[1]);
        int year = Integer.valueOf(values[2]);
        DateTime converted = convertDate(param);
        if (converted.getDayOfMonth() != day || converted.getMonthOfYear() != month || converted.getYear() != year) {
            return false;
        }
        else{
            return true;
        }
    }
    private DateTime convertDate(String param) throws ParseException{
       SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
       Date convertedCurrentDate = sdf.parse(param);
       return new DateTime(convertedCurrentDate);
    }
    

    托管豆

    @ManagedBean(name="mask")
    public class MaskBean {
    
    private Date date;
    
    public Date getDate() {
        return date;
    }
    
    public void setDate(Date date) {
        this.date = date;
    }
    
    public void submit(){
        RequestContext context = RequestContext.getCurrentInstance();
        context.execute("alert('date submited: value recibed " + date.toString() + "')");
    }
    

    }

    它对我有用。

    【讨论】:

    • 这个 DateTime 是什么数据类型??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-23
    • 2017-08-25
    • 2010-11-14
    • 1970-01-01
    • 2014-02-19
    • 2020-02-09
    • 1970-01-01
    相关资源
    最近更新 更多