Lost Heaven 0809
Lost Heaven 0809

Reputation: 456

Properties of bean with many tabs of browser

I'm using JSF,... I thought this is a stupid question but really i don't understand. My question: I have a jsf page: /product.xhtml

<f:metadata>
    <f:viewParam name="p" value="#{singleProduct.p}" />
</f:metadata>
...
<h:form>
        <h:inputTextarea value="#{singleProduct.content}" />
        <h:commandLink value="Comment" action="#{singleProduct.postComment}" />
</h:form>

And my bean: SingleProduct

@Named
@SessionScoped
@Inject
private ProductService productService; //EJB
private int p;
private String content;
//Getters and setters;

public void postComment(){
  productService.addComment(p,content); //addComment(int productId, String commentContent)
}

Now, i open a tab of browser with URL: /product.xhtml?p=1. Then open another tab with URL:/product.xhtml?p=2. Then return to tab with URL: /product.xhtml?p=1 to type and submit form. I want to ask: The comment that is saved to database with how many productId values: 1 or 2 ? I thought my question is related to state of JSF, but i don't understand about it clearly. Thanks

Upvotes: 0

Views: 93

Answers (1)

skuntsel
skuntsel

Reputation: 11742

You need to understand the lifetime of differently scoped beans to answer your question on your own. Session scoped data lives until the session has ben invalidated or it has timed out due to a period of inactivity. It can be created implicitly by JSF when it's first needed, i.e. when you access session bean in your view via EL. You can of course create session beforehand on your own, or remove session attributes, which beans (or proxies) are, but I think that it's not your scenario.

HTTP session is unique for some period of time per one browser that may have different tabs opened. So, as there is one bean whose values keep on being overwritten there are clear scoping problems. you ultimately don't want your message to be written to a different id, do you? Essentially you want unique data (bean) to be on a per-view basis, thus you need @ViewScoped bean.

Upvotes: 1

Related Questions