aspose file tools*
The moose likes JSF and the fly likes 1 managedBean 2 views Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "1 managedBean 2 views" Watch "1 managedBean 2 views" New topic
Author

1 managedBean 2 views

Marco Noronha
Ranch Hand

Joined: Oct 30, 2012
Posts: 50
How can I keep a managedBean alive between two views (xhtml) ?

@ViewScoped didn´t work for me..
Its because all my views have:
listEntity
formEntity
Where Entity is the name of the entity like



The both views uses the same managedBean !
How can I bypass that? Or should I put the form on the same view as the list
[bad idea for me, because of the security access for each user and stuff.. I would have to put it on a container and them rendered it]
Well.. I would like to have separate files/views for this.
Is there any workaround?

Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

Use Session scope when you need to hold data that appears in more than one View (web page).

Use View Scope when you need to hold data that is only processed on a single View (web page).

Request Scope is almost totally useless in JSF because Request Scope objects are destroyed on every postback and therefore cannot retain state.


True person is moral, false is right!
Marco Noronha
Ranch Hand

Joined: Oct 30, 2012
Posts: 50
Volodymyr Levytskyi wrote:
Use Session scope when you need to hold data that appears in more than one View (web page).

Use View Scope when you need to hold data that is only processed on a single View (web page).

Request Scope is almost totally useless in JSF because Request Scope objects are destroyed on every postback and therefore cannot retain state.


.... But I don´t need the MB to be on the session. If he is, the mb will only be destroyed after the session expired.
I cant do that to the server!!
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

Hello!

I think it is impossible to retain state between two views using ViewScoped bean. However, if you can, provide on one page two different views using 'rendered'
<h:panelGroup rendered=#{condition1} >
//first view
</h:panelGroup>
<h:panelGroup rendered=#{! condition1} >
// second view
</h:panelGroup>

You can put managed bean into session scope and then remove it when needed!
Marco Noronha
Ranch Hand

Joined: Oct 30, 2012
Posts: 50
Volodymyr Levytskyi wrote:Hello!

I think it is impossible to retain state between two views using ViewScoped bean. However, if you can, provide on one page two different views using 'rendered'
<h:panelGroup rendered=#{condition1} >
//first view
</h:panelGroup>
<h:panelGroup rendered=#{! condition1} >
// second view
</h:panelGroup>

You can put managed bean into session scope and then remove it when needed!


how can I remove it latter?
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

The first two links in google for 'how to remove managed bean from session':
http://mail-archives.apache.org/mod_mbox/myfaces-users/200506.mbox/%3C5d6f88a0506171209c254994@mail.gmail.com%3E
http://stackoverflow.com/questions/9741778/how-and-when-to-remove-a-session-scoped-bean-in-jsf-2-0
Marco Noronha
Ranch Hand

Joined: Oct 30, 2012
Posts: 50


I already saw those
the problem is,
i don´t want to do it programaticly...
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

JSF uses MVC pattern.
Logic is done programmatically in beans.
View is only to render ui components and bind them to properties and action methods.
I don't think it is possible to remove from session scope something in javascript.
Maybe you can apply f:ajax for event types it supports like focus, click, change, blur
Marco Noronha
Ranch Hand

Joined: Oct 30, 2012
Posts: 50
Well, let me put it in another way...

I´m using JSF2 , Spring and Spring Data.

Before I used the MB this way


The dependencies:


AND today I noticed that by default the managedBeans are sessionScope ...
Because Spring doesn´t have viewScope.
Is there a way to use the standard JSF viewscope in my case, and use spring for the other stuff?


Another question... if I use viewScope AND pass a parameter to another view... Like the ID of my entity,
or something like that.. Would it work ?
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

Unfortunately I am not familiar with spring I use EJB.

Definetely you can path parameters from one page to another just like here:

Then to access it in manaed bean I type:

FacesContext facesContext = FacesContext.getCurrentInstance();
String currentPage = facesContext.getExternalContext().getRequestParameterMap().get("currentPage");
Marco Noronha
Ranch Hand

Joined: Oct 30, 2012
Posts: 50
Guys, can you help me out?
This is the sequence of this post: click here!

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16134
    
  21

Volodymyr Levytskyi wrote:Unfortunately I am not familiar with spring I use EJB.

Definetely you can path parameters from one page to another just like here:

Then to access it in manaed bean I type:

FacesContext facesContext = FacesContext.getCurrentInstance();
String currentPage = facesContext.getExternalContext().getRequestParameterMap().get("currentPage");


Passing parameters on the View is something that should be avoided. So is using an object locator.

In JSF, the preferred way to pass objects around is to keep them in Managed Beans and inject them as Managed Properties into the beans that use them. It makes for less network traffic (objects don't have to be serialized out and back), more security (nothing exposed to the client side where it can be hacked), and simpler coding.


Customer surveys are for companies who didn't pay proper attention to begin with.
Marco Noronha
Ranch Hand

Joined: Oct 30, 2012
Posts: 50
Tim Holloway wrote:In JSF, the preferred way to pass objects around is to keep them in Managed Beans and inject them as Managed Properties into the beans that use them. It makes for less network traffic (objects don't have to be serialized out and back), more security (nothing exposed to the client side where it can be hacked), and simpler coding.


But that doesn´t depend on the bean's scope?


And, can you please give your opinion on the other post i refereced here?
It will be important to me!

Thanks
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16134
    
  21

Yes, scope is important. If you're going to pass objects around from one view to another, you need Session, Application or Custom scope.

I think I responded to your other post in the post itself. We prefer to keep things self-contained here, as it's less confusing.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: 1 managedBean 2 views