转换器getAsObject方法未被调用

时间:2013-05-08 12:42:23

标签: ajax jsf-2 converter selectonemenu

我有<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 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());
    }
}

1 个答案:

答案 0 :(得分:1)

您确定将<p:selectOneMenu>放入<h:form>吗?这是我在调用转换器时的错误。