-1

I have some problems with session scoped bean working with f:viewParam. So I have two pages, test_first.xhtml and test_second.xhtml, and a TestBean.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);
}
    /...
}

Start running from first.xhtml, if I open link1 in a new tab and then open link2 in another new tab. Now I got two pages.

If I click the "Print page id" button in link1, 1 will be printed in the console. In link2 the printed value will be 2.

But if I click the Submit button in link1, 2 will be printed and the rendered text will change from 1 to 2. (because link2 is opened later and the bean is session scoped?)

(Update: Why is this case? How can I still print "1" if I click "Submit"?)

I want to keep the bean as session scoped for other properties basically. So any thoughts on this or any alternative methods? Many thanks!

ethanjyx
  • 1,970
  • 7
  • 28
  • 50
  • 1
    A small remark: on first the h:link does not need to be in a form. – Mike Braun Aug 21 '13 at 08:31
  • Related: http://stackoverflow.com/questions/7031885/how-to-choose-the-right-bean-scope/ – BalusC Aug 21 '13 at 12:44
  • Hi @BalusC, any ideas on my updated question? – ethanjyx Aug 21 '13 at 16:54
  • You seem to not have read or not have understood the link in my previous comment. I suggest to read it carefully. Basically, you're trying to store a view scoped property in a session scoped bean and then you're complaining that the view scoped property is held in session scope instead of in view scope. How does that complaint ever make sense? – BalusC Aug 21 '13 at 17:04
  • @BalusC I don't understand why things are different on the two buttons... – ethanjyx Aug 21 '13 at 17:07

1 Answers1

1

If you want it working in different tabs or windows, you need to put those tab specific properties on a ViewScoped or RequestScoped Bean. For properties that are session specific, you could create another Bean and make it SessionScoped.

fischermatte
  • 3,327
  • 4
  • 42
  • 52