Lor*_*ic- 8 jsf date input converter
我想在我的页面中设置一个Date字段
|hours| h |minutes|
Run Code Online (Sandbox Code Playgroud)
小时和分钟在分开的inputText中.
豆有这个日期
import java.util.Date;
...
private Date myDate;
...
Run Code Online (Sandbox Code Playgroud)
而页面是
<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>
Run Code Online (Sandbox Code Playgroud)
但问题是,当我提交表单时,只保存最后一个元素.例如,如果我键入小时数,然后输入分钟数,则会覆盖小时数,结果为
| 00 | h | minutes |
Run Code Online (Sandbox Code Playgroud)
我试着设定
<h:inputText id="myDateHours" value="#{myBean.myDate.hours}></h:inputText>
<h:inputText id="myDateMinutes" value="#{myBean.myDate.minutes}></h:inputText>
Run Code Online (Sandbox Code Playgroud)
但我得到了
Cannot convert 01/01/70 01:00 of type class java.util.Date to int
Run Code Online (Sandbox Code Playgroud)
我不想用两个int字段替换我的日期字段(小时和分钟......)你有什么想法吗?
谢谢
Bal*_*usC 14
如果要使用单个模型值,则无法使用此特定情况.
然而,这是复合材料部件的理想选择.它允许您将单个模型值绑定到一组密切相关的现有组件,并在后备组件中执行处理/转换,与视图和辅助bean完全分离.其中一个示例可以在本文中找到:具有多个输入字段的复合组件.此示例可以针对您的具体情况进行更改,如下所示:
/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>
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
<html ... xmlns:my="http://java.sun.com/jsf/composite/components">
...
<my:inputTime value="#{bean.date}" />
Run Code Online (Sandbox Code Playgroud)