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