gpt4 book ai didi

jsf - 将 java.util.Date 拆分为两个 h :inputText fields representing hour and minute with f:convertDateTime

转载 作者:行者123 更新时间:2023-12-04 05:31:33 24 4
gpt4 key购买 nike

我想像这样在我的页面中设置一个日期字段

|hours| h |minutes|

其中小时和分钟在单独的 inputText 中。

bean 有这个日期
import java.util.Date;
...
private Date myDate;
...

页面是
<h:form>
...
<h:inputText id="myDateHours" maxlength="2" value="#{myBean.myDate}"
<f:convertDateTime pattern="HH" />
</h:inputText>

<h:outputText value=" h " />

<h:inputText id="myDateMinutes" maxlength="2" value="#{myBean.myDate}"
<f:convertDateTime pattern="mm" />
</h:inputText>
...
</h:form>

但问题是,当我提交表单时,只保存最后一个元素。
例如,如果我输入小时和分钟,小时将被覆盖,结果是
| 00 | h | minutes |

我试图设置
<h:inputText id="myDateHours" value="#{myBean.myDate.hours}></h:inputText>

<h:inputText id="myDateMinutes" value="#{myBean.myDate.minutes}></h:inputText>

但我得到一个
Cannot convert 01/01/70 01:00 of type class java.util.Date to int

我不想用两个 int 字段(小时和分钟......)替换我的日期字段
你有想法吗?

谢谢

最佳答案

如果您想使用单个模型值,则这种特殊情况是不可能的。

然而,这是 composite component 的完美候选者。 .它允许您将单个模型值绑定(bind)到一组密切相关的现有组件,并在支持组件中执行处理/转换,与 View 和支持 bean 完全分离。可以在本文中找到其中一个示例:composite component with multiple input fields .此示例可以针对您的具体情况进行如下更改:
/resources/components/inputTime.xhtml :

<ui:component
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:cc="http://java.sun.com/jsf/composite"
>
<cc:interface componentType="inputTime">
<cc:attribute name="value" type="java.util.Date" shortDescription="The selected time. Defaults to now." />
</cc:interface>
<cc:implementation>
<span id="#{cc.clientId}" style="white-space:nowrap">
<h:inputText id="hour" binding="#{cc.hour}" maxlength="2" converter="javax.faces.Integer" />h
<h:inputText id="minute" binding="#{cc.minute}" maxlength="2" converter="javax.faces.Integer" />
</span>
</cc:implementation>
</ui:component>
com.example.InputTime
@FacesComponent("inputTime")
public class InputTime extends UIInput implements NamingContainer {

private UIInput hour;
private UIInput minute;

/**
* As required by <cc:interface>.
*/
@Override
public String getFamily() {
return UINamingContainer.COMPONENT_FAMILY;
}

/**
* Set initial hour and minute based on model.
*/
@Override
public void encodeBegin(FacesContext context) throws IOException {
Calendar calendar = Calendar.getInstance();
Date date = (Date) getValue();

if (date != null) {
calendar.setTime(date);
}

hour.setValue(calendar.get(Calendar.HOUR_OF_DAY));
minute.setValue(calendar.get(Calendar.MINUTE));
super.encodeBegin(context);
}

/**
* Returns the submitted value in HH-mm format.
*/
@Override
public Object getSubmittedValue() {
return hour.getSubmittedValue() + "-" + minute.getSubmittedValue();
}

/**
* Converts the submitted value to concrete {@link Date} instance.
*/
@Override
protected Object getConvertedValue(FacesContext context, Object submittedValue) {
try {
return new SimpleDateFormat("HH-mm").parse((String) submittedValue);
}
catch (ParseException e) {
throw new ConverterException(e);
}
}

public UIInput getHour() {
return hour;
}

public void setHour(UIInput hour) {
this.hour = hour;
}

public UIInput getMinute() {
return minute;
}

public void setMinute(UIInput minute) {
this.minute = minute;
}

}

用法:
<html ... xmlns:my="http://java.sun.com/jsf/composite/components">
...
<my:inputTime value="#{bean.date}" />

也可以看看:
  • When to use <ui:include>, tag files, composite components and/or custom components?
  • 关于jsf - 将 java.util.Date 拆分为两个 h :inputText fields representing hour and minute with f:convertDateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17235721/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com