【发布时间】:2013-05-02 16:52:59
【问题描述】:
我有一个<h:selectOneMenu>。
<h:selectOneMenu id="LoanType" style="width: 180px; font-size: 12px;" value ="#{editLoan.currentLoanType}" converter="#{convertToLoanType}">
<f:selectItems value="#{editLoan.loanTypeList}" var="parLoanType" itemValue="#{parLoanType}" itemLabel="#{parLoanType.loanTypename}"/>
<f:ajax listener="#{editLoan.loanTypeChanged}" execute="@this"/>
</h:selectOneMenu>
如果用户改变了选择,那么我想调用一个监听器方法。但是,我的转换器的getAsObject() 方法没有被调用。只有getAsString() 方法是,所以我在监听器方法中得到null 变量。
这是我的转换器类:
@ManagedBean
@RequestScoped
public class ConvertToLoanType implements Converter {
@PersistenceContext(unitName = "CrasmonClientPU")
private EntityManager em;
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
try{
int id = Integer.parseInt(value);
System.out.println("getting as object");
return em.find(ParLoantype.class, id);
}catch(Exception e){
System.out.println(e.getMessage());
}
return null;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
try{
ParLoantype pa = (ParLoantype) value;
return String.valueOf(pa.getLoanTypeid());
}catch(Exception e){
System.out.println(e.getMessage());
}
return null;
}
}
这是我的支持 bean 类:
@Named(value = "editLoan")
@SessionScoped
public class EditLoan implements Serializable {
@Inject
private LoanmainController lmainController;
ParLoantype currentLoanType;
LoanMain loanMain;
List<LoanMain> allLoans;
List<ParLoantype> loanTypeList;
public EditLoan() {
}
@PostConstruct
public void init(){
currentLoanType = lmainController.findLoantype(1);
this.allLoans = findLoansByParLoantype(currentLoanType);
this.loanTypeList = lmainController.prepareAllParLoantype();
}
public void loanTypeChanged(){
System.out.println(this.currentLoanType.getLoanTypename());
//this.allLoans = findLoansByParLoantype(currentLoanType);
}
public List<LoanMain> findLoansByParLoantype(ParLoantype type){
return lmainController.findLoansByParLoanType(type.getLoanTypeid());
}
}
【问题讨论】:
-
你在转换器的方法中设置了断点吗?或者这只是您对缺少
System.out.println("getting as object");的天真观察?你真的排除了Integer#parseInt()抛出异常的可能性吗? -
是的,我做到了。它根本没有经过方法。但我通过在 selectOneMenu 组件中加入 'required = "true"' 解决了这个问题。我认为转换器在验证后可以工作。所以需要意味着有验证然后转换器完成它的工作。
-
嗯,这是出乎意料的。您使用的是哪个 JSF impl/version?
web.xml中有哪些javax.faces.*上下文参数?顺便说一句,转换器在验证器之前运行。
标签: ajax jsf-2 converter selectonemenu