显示下拉框时出错?

时间:2013-05-03 20:19:31

标签: jsf-2 primefaces

我正在研究最新版本的主要面孔3.5。 问题:每当ajax更新发生时,都会启用所需的true。

<p:column>
    <h:outputText value="Your Birth Date " style="width:50%"/>
</p:column>
<p:column>
<p:selectOneMenu required="true" id="inputMonth"
value="#{createProfile.dateUtils.inputMonth}"
requiredMessage="Invalid Month."        
style="width:28%;text-align:left;" 
tabindex="4" 
label="Month" 
height="355">
    <f:selectItem itemLabel="MM" itemValue="" />
    <f:selectItems value="#{createProfile.dateUtils.monthList}" />
    <p:ajax update="inputDay" listener="#{createProfile.dateUtils.loadDays}">
</p:selectOneMenu>

<p:selectOneMenu  required="true" id="inputDay"
value="#createProfile.dateUtils.inputDay}"
requiredMessage="Invalid Date."
style="width:28%;text-align:left;"
tabindex="5"
label="Day"
height="355">
    <f:selectItem itemLabel="DD" itemValue="" />
    <f:selectItems value="#{createProfile.dateUtils.dayList}" />
    <p:ajax />
</p:selectOneMenu>

<p:selectOneMenu required="true" id="inputYear"
value="#{createProfile.dateUtils.inputYear}"
requiredMessage="Invalid Year."
style="width:34%;text-align:left;" tabindex="6"
label="Year" height="455">
    <f:selectItem itemLabel="YYYY" itemValue="" />
    <f:selectItems value="#{createProfile.dateUtils.yearList}" />
    <p:ajax update="inputDay" listener="#{createProfile.dateUtils.loadDays}" />
</p:selectOneMenu>
</p:column>

当从下拉列表中选择一个月时,它会使用ajax调用填充day字段并根据所选月份加载日期,因为在day字段中设置了required =“true”的文本框一天从默认颜色变为红色,当我们选择年份时也会发生类似的事情。验证应该仅在我尝试提交表单时发生。

Date Utils .java

public class DateUtils {

private Map<String,String> monthList;
private Map<String,String> dayList;
private Map<String,String> yearList;
private String inputYear="";
private String inputMonth="";
private String inputDay="";

/**
 * Method to get the Month List.
 * @return monthList
 */
public Map<String, String> getMonthList() {
    this.monthList  =new TreeMap<String, String>();
    //this.monthList = new HashMap<String, String>();
    for(int i=1;i<=12;i++){
        String monthIndex = i<=9?"0"+i:""+i;
        this.monthList.put(monthIndex, monthIndex);
    }
    //monthList = sortByComparator(monthList);
    return monthList;
}
/**
 * Method to set the Month List.
 * @param monthList
 */
public void setMonthList(Map<String, String> monthList) {
    this.monthList = monthList;
}
/**
 * method to get the day list.
 * @return dayList
 */
public Map<String, String> getDayList() {
    return dayList;
}
/**
 * method to set the Day List.
 * @param dayList
 */
public void setDayList(Map<String, String> dayList) {
    this.dayList = dayList;
}
/**
 * Method to get the yearList.
 * @return yearList
 */
public Map<String, String> getYearList() {
    //this.yearList = new HashMap<String, String>();
    this.yearList =new TreeMap<String, String>(Collections.reverseOrder());  
    Calendar cal = Calendar.getInstance();
    int yyyy = cal.get(Calendar.YEAR);
    for(int i=yyyy-18; i>=(yyyy-112); i--){
        this.yearList.put(""+i, ""+i);
    }
    return yearList;
}
/**
 * Method to setYearList
 * @param yearList
 */
public void setYearList(Map<String, String> yearList) {
    this.yearList = yearList;
}

/**
 * Ajax Method is called when the month value is selected , 
 * based on the selected month the days are populated.
 */
public void loadDays(){
    this.dayList = new TreeMap<String, String>();
    Calendar cal = Calendar.getInstance();
    int yyyy = (this.inputYear!=null && this.inputYear.trim().length()>0 )?Integer.parseInt(this.inputYear):cal.get(Calendar.YEAR);
    String selDate = this.inputDay;
    String selMonth = this.inputMonth;
    int lastDay = 1;
    if(yyyy%4==0 && selMonth!=null && selMonth.equals("02")){
        lastDay = 29;
    }else if(selMonth!=null && selMonth.equals("02")){
        lastDay = 28;
    }else if(selMonth!=null && ((selMonth.equals("01") || selMonth.equals("03") || selMonth.equals("05") || selMonth.equals("07") || selMonth.equals("08") || selMonth.equals("10") || selMonth.equals("12")))) {
        lastDay = 31;
    }else{
        lastDay = 30;
    }
    for(int i=1; i<=lastDay;i++){
        String days = i<=9?"0"+i:""+i;
        this.dayList.put(days, days);
    }

    if(selDate!=null && selDate.trim().length()>0 && Integer.parseInt(selDate)<=lastDay){
        this.inputDay = selDate;
    }
}
/**
 * Method to get the Input day.
 * @return inputDay
 */
public String getInputDay() {
    return inputDay;
}
/**
 * Method to set the Input Day.
 * @param inputDay
 */
public void setInputDay(String inputDay) {
    this.inputDay = inputDay;
}
/**
 * Method to get the Input year.
 * @return inputYear
 */
public String getInputYear() {
    return inputYear;
}
/**
 * Method to set the Input year.
 * @param inputYear
 */
public void setInputYear(String inputYear) {
    this.inputYear = inputYear;
}
/**
 * Method to getInputMonth 
 * @return inputMonth
 */
public String getInputMonth() {
    return inputMonth;
}
/**
 * Method to setInputMonth 
 * @param inputMonth
 */
public void setInputMonth(String inputMonth) {
    this.inputMonth = inputMonth;
}

}

谢谢!非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您有两种可能性:

  1. 使用属性“立即”

    <p:ajax update="inputDay" listener="#{createProfile.dateUtils.loadDays}" immediate="true" />

    使用immediate =“true”,您可以跳过流程验证阶段

  2. 使用属性“进程”

    <p:ajax update="inputDay" listener="#{createProfile.dateUtils.loadDays}" process="@this" />

    使用process =“@ this”,您不会使用ajax请求处理整个表单,而只会处理此字段。