2013-08-21 3 views
-1

f:viewParam으로 작업하는 세션 범위 Bean에 대한 몇 가지 문제점이 있습니다. 그래서 두 페이지, test_first.xhtmltest_second.xhtmlTestBean.java이 있습니다.세션 범위의 뷰 매개 변수

first.xhtml :

<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:p="http://primefaces.org/ui"> 

<h:head/> 
<h:body> 
<h:form> 
    <h:link value="link1" outcome="test_second" > 
     <f:param name="id" value="1"/> 
    </h:link> 
    <br/><br/> 
    <h:link value="link2" outcome="test_second" > 
     <f:param name="id" value="2"/> 
    </h:link> 
</h:form> 
</h:body> 
</html> 

second.xhtml :

<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:p="http://primefaces.org/ui"> 
<f:metadata> 
    <f:viewParam name="id" value="#{testBean.userId}" /> 
</f:metadata> 
<h:head/> 
<h:body> 
<h:form> 
    This is the second page. 
    <h:outputText value="Selected id is #{testBean.userId}" /> 
    <h:commandButton value="Print page id" action="#{testBean.print()}" /> 

    <h:commandButton styleClass="submitButton" value="Submit" action="#{testBean.submit}"> 
     <f:ajax execute="@form" render="@form"/> 
    </h:commandButton> 
</h:form> 
</h:body> 
</html> 

TestBean.java :

@ManagedBean 
@SessionScoped 
public class TestBean implements Serializable{ 
    private Integer userId; 

public void print() { 
    System.out.println(userId); 
} 

public void submit() { 
    System.out.println(userId); 
} 
    /... 
} 

시작 나는에 link1을 열 경우, first.xhtml에서 실행 새 탭을 열고 다른 새 탭에서 link2을 엽니 다. 이제 두 페이지가 있습니다.

링크 1에서 "페이지 ID 인쇄"버튼을 클릭하면 1이 콘솔에 인쇄됩니다. link2에서 인쇄 된 값은 2입니다. 하지만 링크 1에 Submit 버튼을 클릭하면, 2가 인쇄됩니다 렌더링 된 텍스트를 1에서 2로 변경됩니다 (링크 2 나중에 열리고 빈은 세션 범위이기 때문에?)

(업데이트

: 왜이 경우입니까? "제출"을 클릭하면 "1"을 계속 인쇄 할 수 있습니까?)

기본적으로 다른 속성의 세션 범위로 bean을 유지하려고합니다. 그래서이 방법이나 다른 방법에 대한 생각은 없습니까? 많은 감사합니다!

+1

작은 말 : 처음에는 h : 링크가 양식 일 필요는 없습니다. –

+0

관련 항목 : http://stackoverflow.com/questions/7031885/how-to-choose-the-right-bean-scope/ – BalusC

+0

안녕하세요 @BalusC, 내 업데이트 된 질문에 대한 아이디어가 있습니까? – ethanjyx

답변

1

다른 탭이나 창에서 작업하려면 해당 탭 특정 등록 정보를 ViewScoped 또는 RequestScoped Bean에 넣어야합니다. 세션 특정 속성의 경우 다른 Bean을 만들어 SessionScoped로 만들 수 있습니다.