Ove*_*eer 5 jsf composite-component
我试图让一个复合组件使用它自己的支持bean,使用来自Core JSF 3书的p375上的示例,但只是得到一个NPE.问题似乎是在encodeBegin()的开头,Date date =(Date)getValue()返回null.如果我老实说我真的不明白组件的值应该存储在哪里,我使用cc:attribute type =将其指定为java.util.Date,但我真的不明白这是怎么回事:public Object getSubmittedValue(){return this; - 将返回InputDateBean类的实例 - 导致Date.我一般都很好并且对这应该如何工作感到困惑.
与书籍示例不同,我正在尝试使用支持组件进行临时存储,因此当输入日期时,我会尝试将其存储在#{cc.day}中,因为他们出于某种原因使用应用程序范围的bean.
谢谢你的帮助.我正在使用Mojarra 2.1.
inputDate.xhtml
<cc:interface componentType="uk.co.myco.jsfbeans.sqcc.InputDateBean">
<cc:attribute name="value" type="java.util.Date"/>
</cc:interface>
<cc:implementation>
<h:panelGrid columns="3">
<h:inputText id="day" value="#{cc.day}"
converter="javax.faces.Integer"/>
<h:inputText id="month" value="#{cc.month}"
converter="javax.faces.Integer"/>
<h:inputText id="year" value="#{cc.year}"
converter="javax.faces.Integer"/>
</h:panelGrid>
</cc:implementation>
Run Code Online (Sandbox Code Playgroud)
InputDateBean.java
package uk.co.myco.jsfbeans.sqcc;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import javax.faces.component.FacesComponent;
import java.util.GregorianCalendar;
import javax.faces.application.FacesMessage;
import javax.faces.component.NamingContainer;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.convert.ConverterException;
import uk.co.myco.general.SQLog;
import uk.co.myco.jsfbeans.helper.Messages;
@FacesComponent(value = "uk.co.myco.jsfbeans.sqcc.InputDateBean")
public class InputDateBean extends UIInput implements NamingContainer {
private int day = 0, month = 0, year = 0;
public InputDateBean() {
}
@Override
public String getFamily() {
return "javax.faces.NamingContainer";
}
@Override
public void encodeBegin(FacesContext context) throws IOException {
Date date = (Date) getValue();
Calendar cal = new GregorianCalendar();
cal.setTime(date);
UIInput dayComponent = (UIInput) findComponent("day");
UIInput monthComponent = (UIInput) findComponent("month");
UIInput yearComponent = (UIInput) findComponent("year");
dayComponent.setValue(cal.get(Calendar.DATE));
monthComponent.setValue(cal.get(Calendar.MONTH) + 1);
yearComponent.setValue(cal.get(Calendar.YEAR));
super.encodeBegin(context);
}
@Override
public Object getSubmittedValue() {
return this;
}
@Override
protected Object getConvertedValue(FacesContext context, Object newSubmittedValue)
throws ConverterException {
UIInput dayComponent = (UIInput) findComponent("day");
UIInput monthComponent = (UIInput) findComponent("month");
UIInput yearComponent = (UIInput) findComponent("year");
int lday = (Integer) dayComponent.getValue();
int lmonth = (Integer) monthComponent.getValue();
int lyear = (Integer) yearComponent.getValue();
if (isValidDate(lday, lmonth, lyear)) {
return new GregorianCalendar(lyear, lmonth - 1, lday).getTime();
} else {
FacesMessage message = Messages.getMessage("util.messages", "invalidDate", null);
message.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ConverterException(message);
}
}
// getters & setters & isValidDate() removed
}
Run Code Online (Sandbox Code Playgroud)
我现在看到了我的错误。问题是必须使用 Date 对象调用复合组件,即<cclib:inputDate value="#{bean.date}"/>.按照代码,需要实例化日期,但事实并非如此。更可靠的方法是在 getValue() 为 null 的情况下在encodeBegin() 中执行 new Date() 。这与 ah:inputText/f:convertDateTime 的工作原理相同,不需要实例化该值。