JSF 2 : What scope should i use?
Im confused regarding which scope should i use.
Here i have a module, that consist of 4 level of pages like page browse - page header - page detail of that header - page sub detail of that detail. Im thinking that each page would have it's own managedbean's.
The deepest level, level 4 (the sub detail page), would be able to access every other managedbean's state in the outer level. The level 3 (the detail page) could access level 2 managedbean's state (header page) and level 1 managedbean's state (browse).
The outer managedbean's wont be able to access the deeper managedbean's state
Using request scope will not do, since the deep level pages need to access other state in outer levels.
Using session scope will do, but will memory consuming when the user switch to other unrelated program modules, and those unused beans will stay in the memory as long as the session. And moreover, i cant open these chains of pages in different tabs.
Using conversation scope is confusing for me also, since i dont have a clear begin() and end() like a wizard style pages, or quiz pages.开发者_开发问答 The user could switch back n forth with these pages and do actions in any page.
Using view scope seems possible, but i must use the parameter passing from one page to another. The deeper level beans wont be able to access the outer beans, but they could receive the string parameters from the previous page. This kinda ugly, because i might need to query many information from the database again that could have been done in the previous managed bean.
Am i thinking wrong ?
Please share your thoughts.
Thank you,
Albert KamThe new Flash scope in JSF 2 might help you. Think of it like a Session scope that is cleared after it's read once. It's a bit different than the other scopes in that it's not an annotation, but a Map. You could put all info from page1 that page 2 requires in the flash scope, read it a PostConstruct annoted method in page2, then put in all data required by page 3 and so forth. Slightly messy, but at least you won't need to use view parameters. The actual managed beans could be Requestscoped. It would look something like this:
Level1Bean
@public String toLevel2Button(){
ELFlash.getFlash().put("data1", object1);
ELFlash.getFlash().put("data2", object2);
return "level2";
Level2Bean
@PostConstruct
public void init(){
object1 = (MyType1)ELFlash.getFlash().get("data1");
object2 = (MyType2)ELFlash.getFlash().get("data2");
}
public String toLevel3(){
ELFlash.getFlash().put("data1", object1);
ELFlash.getFlash().put("data2", object2);
ELFlash.getFlash().put("data3", object3);
return "level3";
}
You can find a short example here. The flash scope can be read directly from the View aswell. Besides this, i can't think of any way to transfer data between views without using view parameters.
精彩评论