getAsObject()
안에 항목 값 대신 항목 레이블이있는 이유가 확실하지 않습니다. 아마도 귀하의 getAsString()
은 잘못하고 있으며 학생 ID를 기반으로 학생 이름을 반환 할 것입니다.
어쨌든 귀하의 itemValue
이 옳지 않다는 것을 알 수 있습니다.
<h:selectOneMenu id="studlist" value="#{studBean.selectedStudent}">
<f:selectItems value="#{studBean.student}" var="s"
itemValue="#{s.studid}" itemLabel="#{s.name}" />
<f:converter converterId="studentconverter" />
</h:selectOneMenu>
는 A 컨버터는 복잡한 자바 객체와이 HTTP 요청 매개 변수로 주위에 전달 될 수 있도록 String 표현 사이의 변환 사용할 수들이 의도된다. 그러나 학생 ID를 전체 학생 개체 대신 항목 값으로 지정합니다. 대신 전체 학생 개체를 지정해야합니다. #{studBean.selectedStudent}
은 학생 번호를 나타내는 Long
속성이 아닌 Student
속성을 참조해야합니다. 다음과 같이이 itemValue
를 해결하면
: 다음
public String getAsString(FacesContext context, UIComponent component, Object value) {
// This method is called when item value is to be converted to HTTP request parameter.
// Normal practice is to return an unique identifier here, such as student ID.
Student student = (Student) value;
Long id = student.getStudid();
return String.valueOf(id);
}
public Object getAsObject(FacesContext context, UIComponent component, String value) {
// This method is called when HTTP request parameter is to be converted to item value.
// You need to convert the student ID back to Student.
Long id = Long.valueOf(value);
Student student = someStudentService.find(id);
return student;
}
그것을 작동한다고 :
<h:selectOneMenu id="studlist" value="#{studBean.selectedStudent}">
<f:selectItems value="#{studBean.student}" var="s"
itemValue="#{s}" itemLabel="#{s.name}" />
<f:converter converterId="studentconverter" />
</h:selectOneMenu>
하고 다음과 같이 컨버터 (사소한 nullchecks 생략). 처음했다으로
또는, 당신은 당신의 itemValue
을 유지할 수 있고, 전부 <f:converter>
을 제거 할 수 있지만 당신은 학생 ID를 나타내는 Long
속성을 가리 키도록 #{studBean.selectedStudent}
을 변경해야합니다.
BalusC처럼 항상 똑똑! 감사합니다 – Mariah