2009-12-18 57 views

回答

2

方式三:

  1. 後退或中間它通過java.util.Calendar三獲得者和三名制定者。
  2. 使用Converter,但這將是有點hacky。
  3. 使用第三方組件,如rich:calendar

編輯:根據評論,這裏是選項2的樣子。

page.jsp

<h:form> 
    <h:selectOneMenu value="#{myBean.date}"> 
     <f:converter converterId="datePartConverter" /> 
     <f:attribute name="part" value="day" /> 
     <f:selectItems value="#{myBean.days}" /> 
    </h:selectOneMenu> 
    <h:selectOneMenu value="#{myBean.date}"> 
     <f:converter converterId="datePartConverter" /> 
     <f:attribute name="part" value="month" /> 
     <f:selectItems value="#{myBean.months}" /> 
    </h:selectOneMenu> 
    <h:selectOneMenu value="#{myBean.date}"> 
     <f:converter converterId="datePartConverter" /> 
     <f:attribute name="part" value="year" /> 
     <f:selectItems value="#{myBean.years}" /> 
    </h:selectOneMenu> 

    <h:commandButton value="submit" action="#{myBean.submit}"/> 
    <h:messages /> 
</h:form> 

mypackage.MyBean

package mypackage; 

import java.util.ArrayList; 
import java.util.Date; 
import java.util.List; 

import javax.faces.model.SelectItem; 

public class MyBean { 

    private static List<SelectItem> days = new ArrayList<SelectItem>(); 
    private static List<SelectItem> months = new ArrayList<SelectItem>(); 
    private static List<SelectItem> years = new ArrayList<SelectItem>(); 

    static { 
     // Just do your thing to fill them. Only ensure that those are Strings, 
     // else you'll need to change the type in Converter accordingly. 
     for (int i = 1; i <= 31; i++) days.add(new SelectItem(String.valueOf(i))); 
     for (int i = 1; i <= 12; i++) months.add(new SelectItem(String.valueOf(i))); 
     for (int i = 2000; i <= 2020; i++) years.add(new SelectItem(String.valueOf(i))); 
    } 

    private Date date; 

    public void submit() { 
     // Print submitted date to stdout. 
     System.out.println("Submitted date: " + date); 
    } 

    public List<SelectItem> getDays() { 
     return days; 
    } 

    public List<SelectItem> getMonths() { 
     return months; 
    } 

    public List<SelectItem> getYears() { 
     return years; 
    } 

    public Date getDate() { 
     return date; 
    } 

    public void setDate(Date date) { 
     this.date = date; 
    } 

} 

mypackage.DatePartConverter

package mypackage; 

import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.Map; 

import javax.faces.application.FacesMessage; 
import javax.faces.component.UIComponent; 
import javax.faces.component.UIInput; 
import javax.faces.context.FacesContext; 
import javax.faces.convert.Converter; 
import javax.faces.convert.ConverterException; 

public class DatePartConverter implements Converter { 

    public Object getAsObject(FacesContext context, UIComponent component, String value) { 
     String part = (String) component.getAttributes().get("part"); 
     Date date = null; 

     if (context.getRenderResponse()) { 
      // Convert any default/selected date for display. 
      Date selectedDate = (Date) ((UIInput) component).getValue(); 
      if (selectedDate != null) { 
       if (("day".equals(part) && new SimpleDateFormat("d").format(selectedDate).equals(value)) 
        || ("month".equals(part) && new SimpleDateFormat("M").format(selectedDate).equals(value)) 
        || ("year".equals(part) && new SimpleDateFormat("yyyy").format(selectedDate).equals(value))) 
       { 
        date = selectedDate; 
       } 
      } 
     } else { 
      // Convert submitted date after submit. 
      Map<String, Object> map = context.getExternalContext().getRequestMap(); 
      if ("day".equals(part)) { 
       map.put("DatePartConverter.day", value); // Save until we have all parts. 
      } else if ("month".equals(part)) { 
       map.put("DatePartConverter.month", value); // Save until we have all parts. 
      } else if ("year".equals(part)) { 
       String day = (String) map.get("DatePartConverter.day"); 
       String month = (String) map.get("DatePartConverter.month"); 
       String dateString = String.format("%s-%s-%s", day, month, value); 

       try { 
        date = new SimpleDateFormat("d-M-yyyy").parse(dateString); 
       } catch (ParseException e) { 
        throw new ConverterException(new FacesMessage(e.getMessage()), e); 
       } 
      } 
     } 

     return date; 
    } 

} 

    public String getAsString(FacesContext context, UIComponent component, Object value) { 
     // Not relevant here. Just return SelectItem's value. 
     return (String) value; 
    } 

faces-config.xml

<converter> 
    <converter-id>datePartConverter</converter-id> 
    <converter-class>mypackage.DatePartConverter</converter-class> 
</converter> 

<managed-bean> 
    <managed-bean-name>myBean</managed-bean-name> 
    <managed-bean-class>mypackage.MyBean</managed-bean-class> 
    <managed-bean-scope>request</managed-bean-scope> 
</managed-bean> 

請注意,沒有Validator,並且SimpleDateFormat默認爲lenient。因此,選擇例如11月31日將產生1月1日。如果您想提醒您,可能需要自行實施DatePartValidator

+0

感謝您的回覆,BalusC(+1)你能告訴我如何讓我的目標bu使用方法2:利用轉換器,但是這將是有點hacky – 2009-12-19 17:53:23

+0

我編輯我的問題與工作樣本。 – BalusC 2009-12-20 04:01:36

+0

注意:您確實需要按照日 - 月 - 年的順序放置下拉菜單。例如,如果您想要年度 - 月 - 日,那麼您需要重新安排轉換器中的if塊,以便最後一部分最終根據輸入創建日期。談論哈克:) – BalusC 2009-12-20 04:05:48

0

時說:「結合」和「支持bean」,你應該參考以下:

<h:inputText binding="#{myBean.myTextField}" /> 

,並在bean有private UIInput

如果是這樣 - 不,你不能這樣綁定。那麼,我不確定你是否在技術上可以 - 但它肯定會產生意想不到的效果。

但是,如果你希望定位管Bean屬性,那麼你就可以,例如:

<h:inputText value="#{myBean.myProperty.day}" /> 
<h:inputText value="#{myBean.myProperty.year}" /> 
+0

感謝您的回覆。任何解決方法? – 2009-12-18 20:58:51

相關問題