William
William

Reputation: 163

Bean Scopes Managment in JSF + Spring

I am building a application using JSF2.0 + Spring 3.0 .My beans are managed by Spring in this application.
In this application I have a form which have 3 SelectOneMenuItems(JSF)

  1. When user select 1st dropdown box value it then it will populate 2nd dropdown box(As i have placed logic in EventChangeListener)
  2. And when user select value from 2nd drop down it will populate 3rd drop down according to the selection of 2nd drop down

Now When I submit the form, it will submit fine but it doesn't not remove the values from the box. I tried request scope in spring but it generate another problem that is when a value select by first drop down let say country and i send value to next drop down of province (to populate province according to country drop down selection) , spring consider it a new request and refresh the bean, So country's drop down value get removed and i get NullPointerException while populating provinces. What should I do now I really get stucked.Please help

Upvotes: 0

Views: 403

Answers (3)

user1640277
user1640277

Reputation:

You can create a new bean yourself and put it in the desired scope using FacesContext.

FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("myBean", new MyBean());

By using this in your code you can override the current instance of your bean and get rid of old bean.

Upvotes: 4

wemu
wemu

Reputation: 8170

I would say the session scope is fine here. I think you need to do a proper management of the selected values of the dropdowns. Once you change DropDown1 you need to reset DropDown2 and DropDown3. Once you change DropDown2 reset DropDown3. By reset I mean to load or filter only the valid data into the model of those components.

Your are using JSF2. Are you working with the f:ajax tag? do you have a code snippet available? There may be an issue there, that is a bit tricky here and there.

Upvotes: 0

Vrushank
Vrushank

Reputation: 2813

You can use the View Scope which would be optimal for your use case. But since Spring 3.0 doesn't support the view scope, you'll need to create your custom view scope implementation for Spring to identify. You may refer to this link for reference.

Upvotes: -1

Related Questions