1

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

1 回答 1

1

您是否确保放入<p:selectOneMenu>a <h:form>?当没有调用转换器时,这是我的错误。

于 2014-10-10T08:41:28.633 回答