Managed Bean annotation not working - jsf-2

Version :
RichFaces 4.3.5
Apache MyFaces 2.1
Tomcat 6
Issue :
We are using JSF2 .
I am not able to invoke a managed bean action method using annotation.
Below is the code for managed bean.
#ManagedBean(name="controller")
#RequestScoped
public class Controller {
public String getUserName(){
}
}
The bean has been invoked like below :
sample.xhtml :
#{controller.userName}
I have checked that all the faces-config.xml in my web application adhere to JSF2 namespace.
I am using tomcat 6 to deploy the web application.
When same bean is added in faces-config.xml,the bean method gets executed successfully.
Is there anything else I need to take care for this to work ?
Please help .

Related

Grails spring bean in not loaded (custom UserDetailsService)

I use custom implementation for Spring Security Plugin to override a default loadUser function. I read this manual https://grails-plugins.github.io/grails-spring-security-core/guide/userDetailsService.html and this is my resources.groovy file:
package spring
import security.ExtendedUserDetails
beans = {
UserDetailsService(ExtendedUserDetails)
}
but it was didn't loaded. App still use a default puligin implementation GormUserDetailsService. I use debugger and I see that ExtendedUserDetails never run.
So what's wrong?
This question is unanswered yet
Custom UserDetailsService Not Being Called - Grails & Spring Security Core
Looks like there is a typo in your bean name in resources.groovy. The name must be userDetailsService not UserDetailsService (capital U) and ExtendedUserDetails must implement GrailsUserDetailsService.

NotSerializableException on ViewScoped ManagedBean with CDI Injected properties

I'm trying to create a scalable JSF application.
I would like to save view states on client side but I have troubles with ViewScoped ManagedBean with CDI Injected attributes.
SomeService.java :
#Singleton
public class SomeService {
// ...
}
SomeBean.java
#ManagedBean
#ViewScoped
public class SomeBean implements Serializable {
#Inject
private SomeService someService;
}
Unfortunately glassfish fails to serialize someService which I don't want to be serializabled but re-injected.
I tried to make it transient which ends up to a NullPointerException when accessing someService after de-serialization.
What shall I do?
I'm aware that I could use CDI ViewScoped with Seam Faces or CODI but I want to minimize at most dependencies.
I could also wait for JEE7 which will provide #ViewScoped for CDI but we won't be using JEE7 before months.
UPDATE :
I just wanted to add that I was using embedded EJB bundled in a jar which is itself linked to my war.
NotSerializableException's stack trace has the following message :
com.company.core.service.__EJB31_Generated__SomeService__Intf____Bean__
I don't like to self respond to my own questions but after some more research I found that it was a bug in Mojarra 2.1.6 (I'm using Glassfish 3.1.2.2) which is now solved in Mojarra 2.1.20.
To update Mojarra you just need to download a fresher version (eg: https://maven.java.net/content/repositories/releases/org/glassfish/javax.faces/2.1.20/javax.faces-2.1.20.jar) and place it in the $GLASSFISH/modules directory as javax.faces.jar.

How to access a local interfaces from one project into another within the same ear project

I have a web project that has FacesValidator, this validator needs to access an EJB service to verify if a record exists. Unfortunately, I cannot inject my enterprise beans since the validator is not a managed-bean, so I'm trying to access it via InitialContext. I've tried different combination from http://docs.oracle.com/javaee/6/tutorial/doc/gipjf.html but failed.
What works is this format:
java:global/myProject-ear-1.0.0/myProject/MyService!com.czetsuya.myProject.service.membership.MyService,
My question is can it be simplify? Seems too long.
Thanks,
czetsuya
Look at the server logs. A bit decent EJB container (at least, Glassfish 3 and JBoss 6/7 do), logs all available JNDI names of the EJB during EJB deployment step. Provided that the validator is properly been put in the WAR and the EJB has a #Local interface, then the shortest JNDI name would be the java:app one which should in your case have been java:app/myProject/MyService.
A completely different alternative is to just make the validator a JSF or CDI managed bean instead, so that you can just use the #EJB annotation.
#ManagedBean // Or #Named.
#ApplicationScoped // Provided that the instance doesn't have any state.
public class MyValidator implements Validator {
#EJB
private MyService myService;
// ...
}
and reference it by binding instead of validatorId:
<f:validator binding="#{myValidator}" />
Note that from JSF 2.2 on, you should be able to inject #EJB in a #FacesValidator (and #FacesConverter).
See also:
How to inject in #FacesValidator with #EJB, #PersistenceContext, #Inject, #Autowired

Injecting EJB in struts2 action?

Am using struts2 , EJB 3.0 ...
My requirement is to call the EJB layer from struts2 action class .
I hope there are two ways in achieving this :
1.Using #EJB annotation in Action class
2.Using JNDI look up
I tried both ,
but the problem with JNDI lookup is , eventhough am using correct naming , am getting NameNotFoundException . So the ultimately , my team moved to other method which is using #EJB annotation .
But when am using #EJB annotation am getting null out of it , I think its not injected :
am getting the NullPointerException
code :
#EJB(mappedName="BeanLocal/local")
BeanLocal bean ;
Can any one suggest me what i have to do further ...
Also if there is anylink in SOF , please do refer me as i found nothing related to this
You can't use the traditional dependency injection in Struts 2 action classes because actions aren't managed. However there is a way to achieve this by using the CDI plugin or Guice.
You can also use JNDI look up but the syntax depends on your server. Your best option is to check the documentation according to what you have (JBoss 7.1, Glassfish...)
I had this same problem and here is how I solved it.
As Shinosha said, the #EJB annotation will not work since the action classes are managed by the Struts container.
In order to use JNDI lookup, I had to make the bean #Remote and specify a mappedName. Then the code is as follows (it depends on the server your are using, in my case Weblogic):
Context ctx = new InitialContext();
MyBean bean= (MyBean) ctx.lookup("MyBeanMappedName#myapp.MyBean");
The lookup string should be the fully qualified name of the bean.
I wrote following interceptors to solve this issue. Have a look and share any feedback you may have :
http://gauravwrites.blogspot.com/2014/11/ejb-injection-in-struts2-interceptor.html

Inject singleton bean into session bean via remote interface, object always "null"

I need to inject a singleton bean into the session bean. Below are the corresponding classes. The problem is that the injected object is always null. I tried all of the JNDI lookup strings which my JBoss 7.0.1 server showed me during startup (i.e. JNDI bindings for session bean named GlobalBean in deployment unit subdeployment .. of deployment .. are as follows: ..). I also tried commenting out the #EJB annotation in GlobalBean.java and also tried to use the "ejb/GlobalBean" during injection. However, no luck. What could be the reason? Thx.
GlobalBean.java:
#Startup
#Singleton
#Remote(GlobalBeanRemote.class)
#EJB(name="ejb/GlobalBean", beanName="GlobalBean", beanInterface=GlobalBeanRemote.class)
#ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
public class GlobalBean implements GlobalBeanRemote
{
// CODE
}
SessionBean.java:
#Stateful
public class SessionBean extends ParentBean
{
#EJB(name="java:module/GlobalBean!project.framework.interfaces.GlobalBeanRemote")
private GlobalBeanRemote globalBeanAPI3;
// CODE
}
In your SessionBean class try changing name attribute of #EJB to mappedName.
#EJB(mappedName="java:module/GlobalBean!project.framework.interfaces.GlobalBeanRemote")
This will, of course, only work if your two beans are in the same module.
Update
Given that your beans are in separate modules, try using the java:app namespace:
#EJB(mappedName="java:app ...")
The java:app namespace is used to look up local enterprise beans packaged within the same application. That is, the enterprise bean is packaged within an EAR file containing multiple Java EE modules. JNDI addresses using the java:app namespace are of the following form:
java:app[/module name]/enterprise bean name[/interface name]
Also try removing GlobalBean's #EJB annotation. #EJB is used to define a dependency.

Resources