I've been trying to use JSF 2.3 with TomEE server and I'm having problems using the #Inject annotation with the FacesContext object.
When I use it, i get the following exception while starting my TomEE server:
SEVERE: CDI Beans module deployment failed
org.apache.webbeans.exception.WebBeansDeploymentException: javax.enterprise.inject.UnsatisfiedResolutionException: Api type [javax.faces.context.FacesContext] is not found with the qualifiers
Qualifiers: [#javax.enterprise.inject.Default()]
Am I missing something?
Thanks.
Seems like some JSF2.3 features must be activated by setting the used JSF version.
Try setting JSF version by adding this empty class:
import javax.faces.annotation.FacesConfig;
/**
* The presence of the #FacesConfig annotation on a managed bean deployed within an application enables version specific
* features. In this case, it enables JSF CDI injection and EL resolution using CDI.
*
*/
#FacesConfig(version = FacesConfig.Version.JSF_2_3)
public class ConfigurationBean {
}
from https://github.com/javaee/glassfish/issues/22094
Related
I am in the process of trying to convert a suite of applicationss from JSF1.2 to JSF2.2. I have successfully converted 2 but am having a problem with the third. When I try to deploy it to GlassFish 4.1, I get the following errors from the GF console:
Severe: Exception while loading the app : CDI deployment failure:WELD-001408: Unsatisfied dependencies for type Injector with qualifiers #Default
at injection point [BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedMethod] #Inject public synchronized org.sonatype.guice.bean.locators.DefaultBeanLocator.add(Injector)
at org.sonatype.guice.bean.locators.DefaultBeanLocator.add(DefaultBeanLocator.java:0)
org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type Injector with qualifiers #Default
The program and its dependdencies are huge so I don't want to enter it all here, but here is the annotations I am using for the managed bean:
#ManagedBean(name="projectbean")
#SessionScoped
public class ProjectCreatorBean implements Serializable, ClientCreatorAware {
Any idea of what I should be looking for?This really has me stumped.
Thanks,
Mike
we are using PicketLink 2.7 in an EE7 CDI/JSF app with Wildfly.
According to the PicketLink documentation there are some EL methods
like #{hasRole('ROLE_NAME')}. When we try to use these in an JSF page
<ui:fragment rendered="#{hasRole('ROLE_NAME')}">
we get
Caused by: javax.el.ELException: Function 'hasRole' not found
When we use the EL expression on a CDI bean with
#Restrict("#{hasRole('ROLE_NAME')}")
public void doWhatEver(){}
It works fine (throwing an exception when it doesn't have the role).
So the PicketLink interceptor is configured in beans.xml, we use the uber dependency for PicketLink in the pom file.
What are we missing?
The methods are provided by org.picketlink.internal.el.ELFunctionMethods
as far as I can make out:
public static boolean hasRole(String roleName)
Checks if an authenticated user is granted with a role with the given name.
This method requires that valid ELEvaluationContext associated with the current invation thread.
The EL expressions defined by PicketLink are not available in JSF context. I was facing the same problem and decided to use an #ApplicationScoped bean with the needed methods:
#Named("auth")
#ApplicationScoped
public class AuthorizationManager {
#Inject Identity identity;
#Inject PartitionManager partitionManager;
public void hasRole(String roleName) {
return AuthorizationUtil.hasRole(identity, this.partitionManager, roleName);
}
}
Then you can use it in JSF like:
<ui:fragment rendered="#{auth.hasRole('ROLE_NAME')}">
How can I get a properties file from CDI bean, I mean properties file for internationalization purposes as mentioned here.
In #ManagedBean everything is simple #ManagedProperty(name="....") but I can't encounter the way to achieve the same in CDI bean.
Thank you very much.
As far as I know, CDI doesn't support the kind of field-level access that #ManagedProperty gives (where you can have #ManagedProperty(name="#{msgs.title}")). If you want that level of control in CDI, you'll have to write a CDI Producer.
Considering that the resource bundle is simply a class of ResourceBundle, you could easily obtain your defined bundle with:
FacesContext ctxt = FacesContext.getCurrentInstance();
ResourceBundle bundle = ctxt.getApplication().getResourceBundle(ctxt, aValue);
bundle.get("title");
Alternatively, you could simply inject either your FacesContext or Application into your bean:
#Inject
Application theApplication
public void getBundle{
ResourceBundle bundle = theApplication.getResourceBundle(ctxt, aValue);
}
If you're looking for internationalization support within CDI for JSF purposes, you may want to look at DeltaSpike's JSF module. It builds off of the core i18n support available in core.
Using CDI is there a way to cause a bean to be instantiated when the application server starts up?
I would like to be able to do something like:-
#Singleton
#Startup
public class StartupBean {
...
}
Unfortunately, although the #Singleton annotation exists in the javax.inject.* package the #Startup annotation doesn't.
Use both of those from the javax.ejb.* package. EJBs support CDI and you can kick off anything you want.
I use JSF 2 and EJB 3.1, all of them are deployed on Glassfish 3.1.
When i setup a class named MyInterceptor which is implemented PhaseListener, i can not revoke remote EJB interface inside it.
It notice "NullPointerException ..."
public class MyInterceptor implements PhaseListener {
#EJB(name="AuthorizationEJB",
beanInterface=AuthorizationService.class,
mappedName="corbaname:iiop:localhost:3700#ejb/AuthorizationEJB")
public AuthorizationService authorizationService;
....
}
When I call authorizationService.dosomestuff(), it raise error NullPointerException
How can i do to fix it?
Thank in advance
In JSF 2.1 and earlier, PhaseListeners are unfortunately no injection targets (meaning you can't use injection in them). You can do your lookup programmatically via JNDI instead though.
In JSF 2.2 all JSF artifacts (including PhaseListeners) will be injection targets, but this will probably not help you now.
Unrelated to your question, but I'm not sure what you're trying to achieve by specifying the beanInterface in your annotation. Most likely you'll also don't need the name attribute and if your bean is a local bean you'll also don't need mappedName.
Use a servlet filter instead of a JSF phase listener to do authorization. You can inject an #EJB in a #WebFilter.
Yeah in web filter you could have just used plain #EJB. Maximimum you needed to add beanName if you had two beans implement same AuthorizationService interface.
Servlet filter is per request, I don't think you need to do security stuff at a certain phase from JSF's lifecycle (which is a more granular level than the whole http request).
For normal lookup you can do:
AuthorizationService.class.cast(new InitialContext().lookup("corbaname:iiop:localhost:3700#ejb/AuthorizationEJB")).dosomestuff();
in a try catch javax.naming.NamingException