chedine
chedine

Reputation: 2374

JSF - Binding values of components added dynamically

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 ?

Upvotes: 1

Views: 1069

Answers (1)

BalusC
BalusC

Reputation: 1109570

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}" />

Upvotes: 1

Related Questions