1
저는 <h:selectOneMenu>
입니다.변환기 getAsObject 메서드가 호출되지 않았습니다.
<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;
}
}
내 백업 콩 빈 클래스는 다음과 같습니다 :
@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 ("객체로 가져 오기")가 없다는 것을 순진하게 관찰 한 것입니까? 'Integer # parseInt()'가 예외를 던졌을 가능성을 정말로 배제 했습니까? – BalusC
예. 그것은 방법을 전혀 통하지 않습니다. 하지만 selectOneMenu 구성 요소에 'required = "true"'를 넣어서 해결했습니다. 나는 변환기가 유효성 검사 후에 작동한다고 생각했다. 그래서 필요한 것은 유효성 검사가 있고 변환기가 그 일을한다는 것을 의미합니다. – Odgiiv
흠, 이것은 예상치 못한 일입니다. 어떤 JSF impl/version을 사용하고 있습니까? 어떤 javax.faces. * 컨텍스트 매개 변수가'web.xml '에 있습니까? 그런데 변환기는 유효성 검사기보다 먼저 실행됩니다. – BalusC