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()呢?
屬性,你的意思是在bean中聲明一個javax.faces.convert.FacesConverter與getter和setter? – Thufir
@Thufir:呃不,只是具體的轉換器實現。二傳手是順便說一句,不是強制性的。 – BalusC