0

I have managed bean with session as managedbean-scope, now in constructor of the backing bean, am doing some validation, but since the scope of bean is session when user first tries to hit upload page, am calling constructor of managed bean and doing some validation to see if user has access to upload page or no.

So on first try, am calling constructor and i get validation error message saying that upload page is not available for user, but now if i go to any other tab and click back to upload page menu tab, that page shows up, how can i change this behavior so that validation is checked on every pageLoad, also I cannot use managed-scoped as request for the page, coz i need to maintain some information between different requests in same session.

Here is the code:

Backing Bean Constructor:

public Upload()
{
    ValidationStatus authorizeBean = validateUSER(user);                
}

Faces-config.xml

<managed-bean>
        <managed-bean-name>fileUpload</managed-bean-name>
        <managed-bean-class>Upload</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
</managed-bean>

Any thoughts?

Rachel
  • 100,387
  • 116
  • 269
  • 365
  • do add you comments if question is not clear and needs more clarifications. – Rachel Oct 16 '12 at 18:58
  • Why don't you just use a request scoped bean and from it call a validation method in your session bean? You just will have to call the validateUSER from the request bean. – ElderMael Oct 16 '12 at 19:10
  • @mael: to understand you correctly, are you suggesting of creating one more backing bean and have it's scope as request but then am creating object of session bean, how would having another request bean help in this scenario? – Rachel Oct 16 '12 at 19:22
  • Create a Request Backing Bean (or View scoped), then annotate a method with @PostConstruct and using a reference (injected) of your session bean to call the authorization method. – ElderMael Oct 16 '12 at 19:26
  • Are you using JSF 1.x or 2.x? In JSF 2.x there's an easy way using ``, but the verbose XML registration of managed bean and using session instead of view scope indicates JSF 1.x. – BalusC Oct 16 '12 at 19:30
  • After all, if you're already using JSF 2.x, why don't you put the bean in the view scope instead? – BalusC Oct 16 '12 at 19:32
  • My understanding could be wrong here, but I i put bean in view scoped then will i not lose all information that i have stored in bean if i go to another view and comeback to upload page ? – Rachel Oct 16 '12 at 19:33
  • @BalusC: View scope seems to have been working for this particular use case, am able to see values in the bean, if i go to another view and comeback to upload page. – Rachel Oct 16 '12 at 19:39

2 Answers2

1

I think the bean is the wrong place to do this. I would use either a servlet filter or a JSF Phase listener to handle page access.

An example of using a servlet filter to control page access is here.

Community
  • 1
  • 1
Steve Atkinson
  • 1,219
  • 2
  • 12
  • 30
-2

Use @PostConstruct

Reference is here.

In your JSF backing bean

@PostConstruct
public void initIt(){
}

@PreDestroy
public void cleanUp(){
}
Zaw Than oo
  • 9,651
  • 13
  • 83
  • 131
  • OP needs to invoke the method on every request. The `@PostConstruct` is invoked only once directly after bean's construction and all dependency injection. A session scoped bean is constructed only once per session, not on every request. – BalusC Oct 17 '12 at 01:06
  • @BalusC, I missed the backing bean is `SessionBean`. :) – Zaw Than oo Oct 17 '12 at 03:06