Use Bean inside Managed Bean - jsf-2

how can i use a bean inside other bean ?
I use this solution, what's the best way ?
#ManagedBean(name="firstBean")
public class FirstBean implements Serializable{
#ManagedProperty(value="#{myBean}")
private MyBean myBean;
...
//(get/set method)
}
I'm new in JSF and JSP so probably it's a bad practies.

Your solution is the right one to inject another bean.
You only need the setter for the injected bean.
Are you having any issues\errors with it?

Related

JSF + Spring Using #Autowired in a ManagedBean ; Do we need setters and getters

I am using Spring and JSF 2.0.
This is how my class look like
#ManagedBean(name = "userLogin", eager = true)
#SessionScoped
public class UserLogin
in this class I am using the following property
#Autowired
#ManagedProperty(value = "#{userService}")
private UserService userService;
And this is how my userService looks like
#Service ("userService")
In a framework like struts , I can use the userService without setters and getters , as its been named as a service.
Please tell how to use this without setters and getters , as I feel like its kind of a overhead.
Basically I just want to get rid of getters and setters for the userService as its a Spring bean.
Regards
Rashen
#ManagedProperty(value = "#{userService}") probably does nothing here, since UserService is not a JSF managed bean (judging from the code in your comment). You are combining two dependency injection strategies, where you only need one (setter is required for #ManagedProperty).
If you remove #ManagedProperty and leave only #Autowired, I think it should work.

Cannot access inner class in bean

I'm using JSF 2.0. I have a managed bean which I can access through my xhtml page. Inside the bean I declared an inner class. I can access ArrayList<String> of managed bean but not ArrayList<InnerClass> and I get the error that the InnerClass does not have a readable property. Anyone know what's wrong?
That can happen if the inner class is not public. It will then be invisible to other classes outside the package (like as JSF/EL itself!). Make sure that the inner class is public whenever you need to access it by JSF/EL.
public class Bean {
public class InnerClass {
// ...
}
}
Otherwise it will be interpreted as String and you'll get confusing exceptions like
javax.el.ELException: /test.xhtml: Property 'someProperty' not readable on type java.lang.String
when you want to access #{innerClass.someProperty}.

Why does the #PostConstruct method not get called using BeanManager.getReference() to obtain an instance?

We are currently moving from JSF-ManagedBeans to CDI. Unfortunately we have made excessive use of the EL-Resolver in the past in order to gain static access to session scoped beans managed by JSF.
Since CDI dependency injection is not available everywhere I rewrote the existing static lookup to make use of the BeanManager (Using SEAM-Solder extending BeanManagerAware).
Iterator<Bean<?>> iterator = beans.iterator();
Bean<T> bean = (Bean<T>) iterator.next(); // possible NPE, I know :)
CreationalContext<T> creationalContext = beanManager.createCreationalContext(bean);
T contextual = (T) beanManager.getReference(bean, type, creationalContext);
return contextual;
The code works and returns a container managed instance of the desired bean. BUT: the methods annotated with #PostConstruct do not get called using getReference(). Perhaps you guys know how to do it. Couldn't find anything googling the issue :-/
Best regards!
You should be using Application#evaluateExpressionGet() for this. Not only for CDI beans, but actually also for JSF beans you previously had.
FacesContext context = FacesContext.getCurrentInstance();
Bean bean = (Bean) context.getApplication().evaluateExpressionGet(context, "#{beanName}", Bean.class);
// ...
Much cleaner, however, is to just use CDI's #Inject or JSF's #ManagedProperty instead.
See also:
Get JSF managed bean by name in any Servlet related class

Howto access JSF2 #ViewScoped beans via ExternalContext?

In JSF1 you can access the bean instances of your current FacesContext by
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext()
ev.getApplicationMap().get(beanName) // get an #ApplicationScoped bean instance
ev.getSessionMap().get(beanName) // get a #SessionScoped bean instance
ev.getRequestMap().get(beanName) // get a #RequestScoped bean instance
In JSF2 #ViewScoped has been introduced, but I can't find a corresponding getViewMap() method on ExternalContext? I am using latest JSF 2.1.1-b04.
Am i misunderstanding some aspects of a #ViewScoped bean? Is there another good-practice to get a #ViewScoped bean instance on the server side?
Thanks,
Steve
Try to eval expression (evaluateExpressionGet):
context.getApplication().evaluateExpressionGet(context, "#{beanName}", BeanClass.class)
View scoped data is stored in the view root. You can get this from the context.

Injecting entity manager into managed bean

It is possible to inject entity manager (or its factory) into jsf managed bean using #PersistenceContext (or #PersistenceUnit)?
I Tried it but nothing, I obtain a NullPointerException.
Yes it is possible. This is the syntax.
#PersistenceContext
EntityManager em;
You need to have a persistence.xml in your project. Btw: I'm running Glassfish 3.
After this you can then use methods like em.createNamedQuery.
Also remember the injection takes place after the constructor so if your trying to do database functions in the constructor this will not work. You will have to add the #PostConstruct annotation to a method. This is probably the problem your having.

Resources