HTTP와 HTML은 복잡한 Java 객체를 이해하지 못합니다. Java 퍼스펙티브에서는 문자열 만 이해합니다. 복잡한 자바 객체를 문자열 풍미의 고유 식별자 (일반적으로 기술 ID (예 : 자동 생성 된 데이터베이스 PK))로 변환 한 다음 해당 식별자를 HTML 링크의 HTTP 요청 매개 변수로 사용하는 것이 좋습니다.
다음과 같이 대상보기에 updateStudent.xhtml
사용 <f:viewParam>
이 Student
에 전달 된 학생 ID를 다시 변환 할 수 있습니다
<h:dataTable value="#{studentTable.students}" var="student">
<h:column>
<h:link value="Edit" outcome="updateStudent.xhtml">
<f:param name="id" value="#{student.id}" />
</h:link>
</h:column>
</h:dataTable>
다음과 같이 링크가있는 테이블로 표시되는
List<Student>
을 감안할 때
<f:metadata>
<f:viewParam name="id" value="#{updateStudent.student}" converter="#{studentConverter}" />
</f:metadata>
와
private Student student;
및
@ManagedBean
@ApplicationScoped
public class StudentConverter implements Converter {
@EJB
private StudentService studentService;
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (value == null || value.isEmpty()) {
return null;
}
if (!value.matches("[0-9]+")) {
throw new ConverterException("The value is not a valid Student ID: " + value);
}
long id = Long.valueOf(value);
return studentService.getById(id);
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (value == null) {
return "";
}
if (!(value instanceof Student)) {
throw new ConverterException("The value is not a valid Student instance: " + value);
}
Long id = ((Student)value).getId();
return (id != null) ? String.valueOf(id) : null;
}
}
저는 프로젝트에서'Jboss Seam'을 사용합니다. 이제'Jboss Seam '을 삭제하려고합니다. 그래서 '회화'처럼 연구하려고합니다. – CycDemo
@CycDemo 필자는 같은 위치에 있었고 아래에 나와있는 BalusC와 같은 변환기를 사용하여 ViewScoped 빈을 선택했습니다 (ConversationScoped는 아직 잘 모르는 JEE7 추가 항목 임). 모든 조회는 PK에 의한 것이므로 매우 캐시 친화적이며 JSF가 수행해야하는 다른 모든 작업과 비교하여 번개가 빠르다. – mabi