tags:

views:

67

answers:

1

I want to implement something like this using JSF.(part of search screen)alt text

More and more new rows will be added dynamically onclick of "+" button and the row will be removed on click of "-" button. The question is about binding the values (user input in those dynamically created rows) to the backing model.

I have created a session scoped backing bean. Some portions of the code,

public class BackingBean{
  //other code

  private List<Criteria> searchFilters; // Each criteria is bound to a row

  public void init(){
   //init code
  }
  public void addEmptyCriteria(){
    searchFilters.add(MyFactory.createNewCriteria());
  }

}

Action of + button triggers addEmtpyCriteria method and rerenders(using a4j) the entire section(with new rows).

This is just a boilerplate and everything works fine as long as the bean is session scoped. But its more intuitive to have it in request scope. A single search is a single request made by the user and in no way is tied to the user session. Also having it in session,forces the developer to clear/remove the backingbean from session inorder to display a fresh search screen.

Is there a better way of doing this? Is it right to have it in session scope ?

+1  A: 

When you're on JSF 2.0, then you can just put the bean in the view scope.

@ManagedBean
@ViewScoped
public class BackingBean {
    // ...
}

When you're still on JSF 1.x, then your best bet is using a4j:keepAlive. Make your bean request scoped and add the following to the JSF page:

<a4j:keepAlive beanName="managedBeanName" />

If you wasn't using Ajax4jsf, then I would have suggested Tomahawk's t:saveState. It does basically the same.

<t:saveState value="#{managedBeanName}" />
BalusC
That's correct. The bean will live as long as you interact with the same view (i.e. submit to bean and navigate back to same view). This is not been shared among other tabs/windows in the same session (when the bean was session scoped, it would be). Have you in any instance tested it?
BalusC
Apologies. Deleted the comment accidentally. Reposting it."The entire code is in say page1.jsp. If i have to use keepalive,I will have to put it in page1.jsp. Meaning,the bean will not be instantiated again.Am i missing any point here?"
chedine
Cannot test it now. I am on a thin client with just internet access. Will let you know.
chedine
So,does it mean that,If I navigate to a different view and come back to page1.jsf,a new instance will be created (even the though the page has keepalive tag)?
chedine
That's correct. That's exactly what you (and everyone else) want with regard to this kind of issues :) See also [Benefits and pitfalls of @ViewScoped](http://balusc.blogspot.com/2010/06/benefits-and-pitfalls-of-viewscoped.html) and [Disadvantages of JSF](http://stackoverflow.com/questions/3623911/what-are-the-main-disadvantages-of-java-server-faces-2-0) for more detailed background information.
BalusC
Works like a charm.Thanks
chedine
You're welcome.
BalusC