내 문제는 내 ViewScoped
bean 중 하나가 동일한보기 내에서 여러 번 생성된다는 것입니다. ViewScopedBean
의 생성자는 트리에서 노드를 선택할 때마다 만들어집니다. I이 부분 (복합 구성 요소를 참조)를 제거하면JSF 2.0 ViewScoped 라이프 사이클
<h:form>
<p:tree value="#{treeBean.root}" var="node"
selectionMode="single" selection="#{viewScopedBean.selectedNode}">
<p:ajax event="select" update="selectedNode, treeBeanUpdate, otherBeanUpdate, panel" listener="#{treeBean.onNodeSelect}" />
<p:treeNode>
<h:outputText value="#{node}" />
</p:treeNode>
</p:tree>
Selected Node: <h:outputText value="#{viewScopedBean.selectedNode}" id="selectedNode"/><br/>
Current TreeBean: <h:outputText value="#{treeBean}" id="treeBeanUpdate"/><br/>
Current OtherBean: <h:outputText value="#{viewScopedBean}" id="otherBeanUpdate"/><br/>
<p:outputPanel id="panel">
<ag:profileComponent managedBean="#{viewScopedBean.profileBean}"/>
</p:outputPanel>
</h:form>
는 ViewScopedBean
생성자가 호출되지 않는다 : 중고
<p:outputPanel id="panel">
<ag:profileComponent managedBean="#{viewScopedBean.profileBean}"/>
</p:outputPanel>
모든 콩 @ViewScoped
로 설정된다.
@ManagedBean
@ViewScoped
public class ViewScopedBean implements Serializable {
private TreeNode selectedNode;
private ProfileBean profileBean;
public ViewScopedBean() {
System.out.println("Constructor of ViewScopedBean " + this);
}
@PostConstruct
public void init() {
System.out.println("ViewScoped init" + this);
profileBean = new ProfileBean();
}
}
올바른 동작입니까? 그렇지 않으면 무엇이 그것을 일으킬 수 있습니까?
업데이트 : 빈 합성물을 사용하려고했는데 동일한 문제가 있습니다.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:composite="http://java.sun.com/jsf/composite">
<composite:interface>
<composite:attribute name="managedBean" required="true"/>
</composite:interface>
<composite:implementation>
</composite:implementation>
</html>
그러나 내가 managedBean
을 필수로 만들지 않았다면 괜찮습니다.
생성자가 호출 될 때 얻을 수없는 또 다른 사항은 생성 된 개체가 사용되지 않는 것입니다.
뷰를 초기화 (콘솔 출력) : 나무에
Constructor of ViewScopedBean [email protected]
2 클릭 : 다음
Constructor of ViewScopedBean [email protected]
Constructor of ViewScopedBean [email protected]
내가 디버그 창 <ui:debug/>
를 열고 viewScopedBean
는 [email protected]
흠. 이것은 매우 흥미 롭습니다. 두 번째 (또는 세 번째) 인스턴스화 중에 예외를 시도하고 throw 할 수 있습니까? stacktrace를보고 WHO가 ViewScopedBean을 만들고 어디서 볼 수 있는지 보는 것이 좋습니다. – fdreger
부분 상태 저장을 사용하지 않으면 실제로 생성자가 한 번만 호출됩니다. – Sydney
[이 질문에 대한 답변 (https://stackoverflow.com/questions/15265433/how-and-when-is-a-view-scope-bean-destroyed-in-jsf/15391453#15391453) 질문 * "어떻게 뷰 범위 Bean이 JSF에서 언제 파기됩니까? "* 관련성이 있습니다. – Lii