2014-01-20 2 views
1

일부 참조 JSF Parameter Passing이 발견되었습니다. JSF 2.X Passing parameter between two xhtml pages JSF 2.0에 전달되는 매개 변수

  • 하지만, 그것은 내 요구 사항에 대한 확인을하지 jsf passing parameters in a method expression failes in ViewScoped bean
  • Passing parameters between managed beans with request scope

    • . 하나의 백업 빈을 object(Student) 다른 빈에 아래와 같이 전달합니다.

      studentTable.xhtml        updateStudent.xhtml 
               pass student object 
               (not `StudentID` string) 
               ==================> 
      StudentTableBean.java       UpdateStudnetBean.java 
      

      백업 빈의 모두 RequestScope 또는 ViewScope하지 Session 수 있습니다. studentTable.xhtml에서 링크 (데이터 테이블 행)를 클릭하면 학생 개체를 updateStudent.xhtml으로 전달하고 싶습니다.

      가능합니까? 참고 자료를 제공하거나 제공 할 수 있습니까?

  • 답변

    0

    어떻게 가능하다고 생각하십니까? Viewscope은 페이지를 떠날 때 끝나고 그 범위에있는 모든 빈은 파괴되고 - 보유하고있는 객체도 파괴됩니다.

    updateStudent.xhtml은 새보기를 만들고 자체 뷰 세트 빈을 가져옵니다. 페이지간에 개체를 유지하려면 새로운 장기 실행 대화를 시작하거나 개체를 세션 범위로 밀어 넣으십시오.

    대화 범위를 사용하는 경우 this tutorial을 참조하십시오.

    +0

    저는 프로젝트에서'Jboss Seam'을 사용합니다. 이제'Jboss Seam '을 삭제하려고합니다. 그래서 '회화'처럼 연구하려고합니다. – CycDemo

    +0

    @CycDemo 필자는 같은 위치에 있었고 아래에 나와있는 BalusC와 같은 변환기를 사용하여 ViewScoped 빈을 선택했습니다 (ConversationScoped는 아직 잘 모르는 JEE7 추가 항목 임). 모든 조회는 PK에 의한 것이므로 매우 캐시 친화적이며 JSF가 수행해야하는 다른 모든 작업과 비교하여 번개가 빠르다. – mabi

    0

    요청의 현재 인스턴스의 세션 맵에 학생의 개체를 넣으면됩니다. studentTable.xhtml에서 해당 백킹 빈 UpdateStudnetBean.java으로 해당 학생의 ID를 전달한 다음 개체 인스턴스를 검색합니다 리소스 (목록, DB 등)에서 가져온 다음 위의 세션 맵 객체에 저장합니다. 이 방법은 암시 적 객체 sessionScope에 의해 updateStudent.xhtml보기에서 가져올 수 있습니다.

    2

    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; 
        } 
    
    } 
    
    +0

    DB에서 다시 검색하여 'ID 문자열'을 전달합니다. 그렇지 않으면'SessionScope' 또는'ApplicationScope'을 사용하십시오. 내가 맞습니까? 제공 해주셔서 감사합니다. – CycDemo

    +0

    범위를 남용하지 마십시오. 스레드 안전성과 사용자 경험이 저하 될 수 있습니다. 추가 정보 : http://stackoverflow.com/q/7031885 및 http://stackoverflow.com/a/15523045 유용한 링크 : https://jsf.zeef.com/bauke.scholtz – BalusC