JSF 2.0 Access bean application area from another Bean

I am using jsf 2.0, and I have two bean navigation (application area) and module (request area). I want to use bean navigation methods in a Bean module. I do this in the Bean module

@ManagedProperty(value = "#{navigationBean}") private NavigationBean navigationBean; 

But when I try to get navigationBean.SomeMethod , it does not work, since the navigation bean is null. How to do it?

+6
source share
3 answers

Both beans must be full @ManagedBean . The acceptor must have a public configuration method for the bean entered. The introduced bean is available only in @PostConstruct and beyond (i.e., in all normal event methods, but not in the acceptor constructor).

So this should work:

 @ManagedBean @ApplicationScoped public class Navigation { // ... } 

 @ManagedBean @RequestScoped public class Module { @ManagedProperty(value="#{navigation}") private Navigation navigation; @PostConstruct public void init() { navigation.doSomething(); } public void setNavigation(Navigation navigation) { this.navigation = navigation; } } 
+19
source

I think @ManagedProperty requires the use of a public installation method.

+4
source

I got a solution

I have a method in the boolean application signature getReadAccess (String role, String module). If I want to use in another bean, then I have to follow these steps

  `javax.el.MethodExpression readAccess; javax.el.ELContext elContext = null; javax.faces.context.FacesContext context = FacesContext.getCurrentInstance(); elContext = ((FacesContext) context).getELContext(); javax.faces.application.Application application = FacesContext.getCurrentInstance().getApplication(); javax.el.ExpressionFactory expressionFactory = application.getExpressionFactory(); readAccess = expressionFactory.createMethodExpression(elContext, "#{navigationBean.getReadAccess}", Void.class, new Class[] { String.class, String.class }); //--------Call---------------------------- return (Boolean) readAccess.invoke(elContext, new Object[] { "roleName", "moduleName" }); 

`

+1
source

Source: https://habr.com/ru/post/888798/


All Articles