2013-05-26 3 views
0

데이터 테이블의 데이터를 requestscope와 동일한 백킹 빈을 사용하여 다른 페이지로 전달하려면 datamodel을 사용하지 않고이 작업을 수행 할 수 있습니까? 여기 서블릿 3.0jsf 다른 페이지로의 데이터 전달이 작동하지 않습니다.

를 사용하고있어 여기 내 데이터 테이블 페이지

<p:dataTable var="entity" value="#{collegeController.allCollege}"> 
     <p:column headerText="Code"> 
      #{entity.collegeCode} 
     </p:column> 
     <p:column headerText="Description"> 
      #{entity.collegeDesc} 
     </p:column> 
     <p:column headerText=""> 
      <p:commandLink value="Edit" action="#{collegeController.prepareEdit(entity)}"/> 
     </p:column> 
    </p:dataTable> 

이고 것은 내 백업 콩

@ManagedBean 
@RequestScoped 
public class CollegeController implements Serializable { 

    private String redirect = ".jsf?faces-redirect=true"; 
    private CollegeCatalog entity; 

    public CollegeController() { 
    } 

    public String prepareEdit(CollegeCatalog selectedEntity) { 
     Session s = NewHibernateUtil.getSessionFactory().getCurrentSession(); 
     s.beginTransaction(); 

     entity = (CollegeCatalog) s.load(CollegeCatalog.class, selectedEntity.getCollegeKey()); 

     return "update" + redirect; 
    } 

    public List getAllCollege() { 
     Session s = NewHibernateUtil.getSessionFactory().getCurrentSession(); 
     Transaction tx = s.beginTransaction(); 

     String query = "" 
       + "FROM CollegeCatalog entity " 
       + "WHERE entity.deleted = FALSE"; 

     Query q = s.createQuery(query); 

     List l = q.list(); 

     tx.commit(); 

     return l; 
    } 

    /** 
    * @return the entity 
    */ 
    public CollegeCatalog getEntity() { 
     if (entity == null) { 
      entity = new CollegeCatalog(); 
     } 
     return entity; 
    } 

    /** 
    * @param entity the entity to set 
    */ 
    public void setEntity(CollegeCatalog entity) { 
     this.entity = entity; 
    } 
} 

이며,이 내 업데이트 페이지입니다 (이것은 내가 보여주고 싶은 곳이다 선택된 데이터)

<h:form> 
    <p:outputLabel value="Code:" for="txtCode"/> 
    <p:inputText id="txtCode" value="#{collegeController.entity.collegeCode}"/> 
    <br/> 
    <p:outputLabel value="Description:" for="txtDesc"/> 
    <p:inputText id="txtDesc" value="#{collegeController.entity.collegeDesc}"/> 
    <br/><br/> 
    <p:commandButton value="Update" action="#{collegeController.update()}"/> 
    <p:commandButton value="Back" action="index.jsf?faces-redirect=true"/> 
</h:form> 

항상 null을 반환합니다.

답변

0

<p:column headerText=""> 
    <p:commandLink value="Edit" action="#{collegeController.action" > 
     <f:setPropertyActionListener value="#{collegeController.entity}" target="#{collegeController.targetEntity}"> 
    </p:commandLink> 
</p:column> 
+0

작동하지 않습니다. – borj

+0

Look at http://stackoverflow.com/questions/4994458/how-can-i-pass-a-parameter-to-a-commandlink-inside-a-datatable –

+0

나에게 준 링크에서 # 3을 사용하고 있습니다. 당신이 내 datatable 페이지에서 볼 수 있듯이, unforunately 나를 위해 일하지 않습니다. 명확한 설명을 위해 – borj

3
당신이 @RequestScoped 관리 빈을 가지고 있기 때문에 당신이 당신의 collegeController.action 방법으로 전송 매개 변수가 손실됩니다

전체 관리 Bean가 생성됩니다 같은 f:setPropertyActionListener@ViewScoped

뭔가 시도 모든 요청. 또한 실제 디자인에서는 collegeController.allCollege 메서드 호출마다 목록이 다시 작성됩니다.

이 문제를 해결하려면

  1. 더 넓은 범위로 관리 빈 범위를 변경

    . 이 경우 @ViewScoped이 문제가되지 않습니다. 관리 빈 범위에 대한 추가 정보 : Communication in JSF 2 - Managed Bean Scopes

  2. 모든 비즈니스 로직을 getter 외부로 이동하십시오. JSF 2로 작업하고 있고 빈을 생성 할 때리스트가로드되기를 원한다면 @PostConstruct 메소드를 사용하고 목록을로드 할 수있다. JSF에서 관리 빈을 사용할 때 은 getters/setter에 비즈니스 로직을 넣지 않는다는 것을 기억하십시오. 더 많은 정보 : Why JSF calls getters multiple times

이 조언을 촬영하고 코드에 적용이 관리 Bean은 다음과 유사하게 표시됩니다

@ManagedBean 
@ViewScoped 
public class CollegeController implements Serializable { 

    private String redirect = ".jsf?faces-redirect=true"; 
    private CollegeCatalog entity; 
    private List<CollegeCatalog> collegeCatalogList; 

    public CollegeController() { 
    } 

    @PostConstruct 
    public void init() { 
     Session s = NewHibernateUtil.getSessionFactory().getCurrentSession(); 
     Transaction tx = s.beginTransaction(); 
     String query = "" 
       + "FROM CollegeCatalog entity " 
       + "WHERE entity.deleted = FALSE"; 
     Query q = s.createQuery(query); 
     collegeCatalogList = (List<CollegeCatalog>)q.list(); 
     tx.commit(); 

     entity = new CollegeCatalog(); 
    } 

    public String prepareEdit(CollegeCatalog selectedEntity) { 
     Session s = NewHibernateUtil.getSessionFactory().getCurrentSession(); 
     s.beginTransaction(); 

     entity = (CollegeCatalog) 
      s.load(CollegeCatalog.class, selectedEntity.getCollegeKey()); 

     return "update" + redirect; 
    } 

    public List<CollegeCatalog> getAllCollege() { 
     return collegeCatalogList; 
    } 

    /** 
    * @return the entity 
    */ 
    public CollegeCatalog getEntity() { 
     return entity; 
    } 

    /** 
    * @param entity the entity to set 
    */ 
    public void setEntity(CollegeCatalog entity) { 
     this.entity = entity; 
    } 
} 

그러나이 방법은 당신이 entity를 보내도록하는 데 도움이된다는 점에 유의 매개 변수는 관리 Bean에 <p:commandLink action="#{collegeController.prepareEdit(entity)}"/>에 있습니다. 페이지를 재전송하는 대신 방금 전달하는 것이 좋습니다.

리디렉션을 사용하여이 작업을 수행하는 것을 선호하는 경우이 방법은 다른 페이지로 데이터를 보내는 데 도움이되지 않습니다. 다른 페이지로 리디렉션되는 <h:button> (또는 더 좋게는 <h:link>)을 대신 사용하면 매개 변수를 처리 할 수 ​​있습니다. 여기에 더 잘 설명되어 있습니다 : Communication in JSF 2 - Processing GET Request Parameters.

+0

tnx를 보았습니다. 대상이 읽을 수 있도록 데이터를 전달하지 않고 관리 대상 비대칭을 업데이트했습니다. – borj

+0

@borj 전체 답변을 읽으셨습니까? 귀하의 경우에는 마지막 단락 모음집이됩니다. –

관련 문제