2011-11-22 83 views
1

嗨,我已經創建了一個自定義的轉換器在JSF的組合框中使用H:selectOneMenu用於,什麼ELContext做我的JSF自定義轉換器

在如下

@ManagedBean(name="studentMgBean") 
public class StudentMBean { 
.............. 
............ 
..... 
     public StudentVO getMyStudent(Integer studentId) { 
      return this.myStudents.get(studentId); 
     } 
     private List<SelectItem> studentList; 
     // getter setter of studentList 
     private Map<Integer,StudentVO> myStudents; 
     private StudentVO selectedStudent; 
     // getter setter of selectedStudent 

    @PostConstruct 
    public void loadStudents(){ 
     .......... 
     ........ 
     if(this.getStudentList() == null){ 
      this.setStudentList(new ArrayList<SelectItem>()); 
     }else{ 
      this.getStudentList().clear(); 
     } 
     this.myStudents = new HashMap<Integer, StudentVO>(); 
     while(rs.next()){ 
       vo = new StudentVO(String.valueOf(rs.getInt("studentId")), 
        rs.getString("studentName"), rs.getString("contactNo")); 
       selectItem = new SelectItem(vo.getStudentId(), vo.getStudentName()); 
       this.getStudentList().add(selectItem); 
       this.myStudents.put(Integer.parseInt(vo.getStudentId()),vo); 
     } 
    } 
} 

我支持bean的代碼,這是我的轉換器,

@FacesConverter(value="studentComboConv") 
public class StudentComboBoxConverter implements Converter{ 
    @Override 
    public Object getAsObject(FacesContext context, UIComponent component, String value) { 
    FacesContext ctx = null; 
    ValueExpression vex = null; 
    StudentMBean studentMgmtBean = null; 
    StudentVO studentVO = null; 
    ......... 
    ........ 
    ...... 
    vex = ctx.getApplication().getExpressionFactory() 
        .createValueExpression(ctx.getELContext(),"#{studentMgBean}", StudentMBean.class); 
    studentMgmtBean = (StudentMBean) vex.getValue(ctx.getELContext()); 
    studentVO = studentMgmtBean.getMyStudent(Integer.parseInt(value)); 
    ........... 
    ........ 
    ..... 
    return studentVO; 
    } 

,這是我的jsp我在哪裏我的應用轉換到組合框

<td align="left">SELECT STUDENT</td> 
<td align="right"> 
    <h:selectOneMenu value="#{studentMgBean.selectedStudent}" id="cmbo" converter="studentComboConv"> 
     <f:selectItems value="#{studentMgBean.studentList}" /> 
    </h:selectOneMenu> 
    ..... 
    .... 
    .. 

現在的問題是,這是什麼線做我的轉換器

vex = ctx.getApplication().getExpressionFactory() 
     .createValueExpression(ctx.getELContext(),"#{studentMgBean}", StudentMBean.class); 
    studentMgmtBean = (StudentMBean) vex.getValue(ctx.getELContext()); 

是什麼ctx.getElContext()呢?

回答

2

它獲得ELContext(< - 單擊鏈接以查看javadoc),以便您可以在Java代碼中以編程方式評估EL表達式#{}。在你的特定情況下,你基本上是通過編程來評估EL表達式#{studentMgBean}以獲得StudentMBean的當前實例。

在JSF 2.0存在的方式是通過Application#evaluateExpressionGet()一個快捷方式,其確實基本上是相同的,隱藏了ELContext細節罩下:

StudentMBean studentMgBean = (StudentMBean) ctx.getApplication().evaluateExpressionGet(ctx, "#{studentMgBean}", StudentMBean.class); 

這就是說,你的做法是相當笨拙。如果轉換器是緊耦合到後臺bean,你可能會更好,使其支持bean,而不是一個屬性:與轉換器作爲一個內部類

converter="#{studentMgBean.studentConverter}" 

+0

屬性,你的意思是在bean中聲明一個javax.faces.convert.FacesConverter與getter和setter? – Thufir

+0

@Thufir:呃不,只是具體的轉換器實現。二傳手是順便說一句,不是強制性的。 – BalusC