我的webapp中的个人资料版表格有点问题。我使用3个selectOneMenu(日,月,年)来修改生日。当新月有不同的日期范围时,我使用ajax监听器来呈现日期值。当我进入个人资料版本时,用户的生日从DB加载并且selectOneMenu被填充。如果我只更改日期 - 版本完成没有任何错误。每当我改变月份,日期变为空,我不知道为什么。这是.xhtml代码片段:
<h2>
#{messages.birthday}:
<h:selectOneMenu id="daySelect" value="#{profileEdition.dayOfBirth}" >
<f:selectItems value="#{profileEdition.dateValues.daysList}" var="day" itemLabel="#{day.valueLabel}" itemValue="#{day.valueValue}"/>
<f:ajax listener="#{profileEdition.dayChanged}" />
</h:selectOneMenu>
<h:selectOneMenu value="#{profileEdition.monthOfBirth}">
<f:selectItems value="#{profileEdition.dateValues.monthsList}" var="month" itemLabel="#{month.valueLabel}" itemValue="#{month.valueValue}"/>
<f:ajax listener="#{profileEdition.monthChanged}" render="daySelect" />
</h:selectOneMenu>
<h:selectOneMenu value="#{profileEdition.yearOfBirth}" >
<f:selectItems value="#{profileEdition.dateValues.yearsList}" var="year" itemLabel="#{year.valueLabel}" itemValue="#{year.valueValue}"/>
<f:ajax listener="#{profileEdition.yearChanged}" render="daySelect" />
</h:selectOneMenu>
</h2>
在我的@ManagedBean中,我得到了这些监听器,如果有这样的需要,他们正在更改daysList,并且当day超出新月的日期范围时,将dayOfBirth值更改为null。我插入了一些System.out.println();在这些听众中检查他们是否正在改变dayOfBirth值,但他们不是。有什么奇怪的 - 我已经在我的注册表单中使用了这个机制,它运行正常。
有什么问题?
DayChanged听众:
public void dayChanged(){
System.out.println("DAY HAS CHANGED! \n NEW DATE IS: " + dayOfBirth + "-" + monthOfBirth + "-" + yearOfBirth);
}
MonthChanged听众:
public void monthChanged(){
ResourceBundle bundle = ResourceBundle.getBundle("locale.messages",
FacesContext.getCurrentInstance().getViewRoot().getLocale());
String day = bundle.getString("day");
String month = bundle.getString("month");
if(monthOfBirth.equals(month)){
monthOfBirth = null;
} else {
int m = Integer.parseInt(monthOfBirth);
if(m==1 || m==3 || m==5 || m==7 || m==10 || m==12) {
dateValues.daysList = new DateValues.Values[32];
dateValues.daysList[0] = new DateValues.Values(day,day);
for(int i=1;i<10;i++){
dateValues.daysList[i] = new DateValues.Values("" + i,"0" + i);
}
for(int i=10;i<32;i++){
dateValues.daysList[i] = new DateValues.Values("" + i,"" + i);
}
}else if(m==2) {
dateValues.daysList = new DateValues.Values[30];
dateValues.daysList[0] = new DateValues.Values(day,day);
for(int i=1;i<10;i++){
dateValues.daysList[i] = new DateValues.Values("" + i,"0" + i);
}
for(int i=10;i<30;i++){
dateValues.daysList[i] = new DateValues.Values("" + i,"" + i);
}
if(dateValues.searchForDay(dayOfBirth) == false){
System.out.println("DAY IS OUT OF SELECTED MONTH RANGE!");
dayOfBirth = null;
}
}else {
dateValues.daysList = new DateValues.Values[31];
dateValues.daysList[0] = new DateValues.Values(day,day);
for(int i=1;i<10;i++){
dateValues.daysList[i] = new DateValues.Values("" + i,"0" + i);
}
for(int i=10;i<31;i++){
dateValues.daysList[i] = new DateValues.Values("" + i,"" + i);
}
if(dateValues.searchForDay(dayOfBirth) == false){
System.out.println("DAY IS OUT OF SELECTED MONTH RANGE!");
dayOfBirth = null;
}
}
}
System.out.println("MONTH HAS CHANGED! \n NEW DATE IS: "
+ dayOfBirth + "-" + monthOfBirth + "-" + yearOfBirth);
}
YearChanged听众:
public void yearChanged(){
ResourceBundle bundle = ResourceBundle.getBundle("locale.messages",
FacesContext.getCurrentInstance().getViewRoot().getLocale());
String day = bundle.getString("day");
String year = bundle.getString("year");
if(yearOfBirth.equals(year)){
yearOfBirth = null;
}
if(monthOfBirth !=null && yearOfBirth != null){
int y = Integer.parseInt(yearOfBirth);
int m = Integer.parseInt(monthOfBirth);
if(y%400==0 && m==2){
dateValues.daysList = new DateValues.Values[30];
dateValues.daysList[0] = new DateValues.Values(day,day);
for(int i=1;i<30;i++){
dateValues.daysList[i] = new DateValues.Values("" + i,"" + i);
}
} else if(y%100==0 && m==2){
dateValues.daysList = new DateValues.Values[29];
dateValues.daysList[0] = new DateValues.Values(day,day);
for(int i=1;i<29;i++){
dateValues.daysList[i] = new DateValues.Values("" + i,"" + i);
}
if(dateValues.searchForDay(dayOfBirth) == false){
System.out.println("DAY IS OUT OF SELECTED MONTH RANGE!");
dayOfBirth = null;
}
} else if(y%4==0 && m==2){
dateValues.daysList = new DateValues.Values[30];
dateValues.daysList[0] = new DateValues.Values(day,day);
for(int i=1;i<30;i++){
dateValues.daysList[i] = new DateValues.Values("" + i,"" + i);
}
} else if(m==2) {
dateValues.daysList = new DateValues.Values[29];
dateValues.daysList[0] = new DateValues.Values(day,day);
for(int i=1;i<29;i++){
dateValues.daysList[i] = new DateValues.Values("" + i,"" + i);
}
if(dateValues.searchForDay(dayOfBirth) == false){
System.out.println("DAY IS OUT OF SELECTED MONTH RANGE!");
dayOfBirth = null;
}
}
}
System.out.println("YEAR HAS CHANGED! \n NEW DATE IS: "
+ dayOfBirth + "-" + monthOfBirth + "-" + yearOfBirth);
}
所有这些if / else语句用于确定特定月份的闰年和正确的日期范围。由于System.out,我已经注意到的是dayOfBirth在我提交表单之后被改为null。
以防万一我通过提交表单来解决我的方法。
public String editProfile() throws ParseException{
System.out.println("Edition! You have chosen:");
System.out.println("Day: " + dayOfBirth);
System.out.println("Month: " + monthOfBirth);
System.out.println("Year: " + yearOfBirth);
birthday = dayOfBirth + "-" + monthOfBirth + "-" + yearOfBirth;
System.out.println("NEW BIRTHDAY IS: " + birthday);
if(DateValidator.isValid(birthday) == false){
ResourceBundle bundle = ResourceBundle.getBundle("locale.messages",
FacesContext.getCurrentInstance().getViewRoot().getLocale());
String text = bundle.getString("invalidDate");
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, text, text);
FacesContext.getCurrentInstance().addMessage("", msg);
return null;
}
Date convertedBirthday = sdf.parse(birthday);
myAccount.setUrodziny(convertedBirthday);
mokEndpoint.editAccount(myAccount);
return "editionSuccess";
}
@ManagedBean是@ViewScoped