JSF Spring Bean set property - spring

i have a JSF web application. I use Beans as Spring Beans (not JSF managed beans). Now i have an URL to application www.example.com?parameter=2
I would like to set this parameter into bean on the page load. I now how to do this with spring web flow but with JSF Navigation i cant do this.
What do you think about using JSTL c:set or jsp:setProperty?
Thanks for your help.
Kind regards
Sebastian

From here:
One could extend a Springs org.springframework.beans.factory.config.PropertyPlaceholderConfigurer which accesses the RequestContext (org.springframework.web.context.request.RequestContextHolder#getRequestAttributes()) to resolve ${xyz}-like properties in the bean.
Of course that would only work for Spring beans with “request”-scope.
If the bean is in session-scope you could simply use the following in a Phase Listener method:
property = FacesContezt.getCurrentInstance().getExternalContext
.getRequestMap().get("paramName");
the phase listener is defined with
<f:view beforePhase="#{bean.method}">
if using facelets, its beforePhaseListener
If you were using faces-context.xml, you could've used the <managed-property>.

Related

How can I inject dependencies into a javax.faces.context.ExceptionHandler using Spring?

I have a JSF application using PrimeFaces 6.2 and Spring 5.1.4. I read, that an exception handler can be defined in the faces-config.xml like this:
<factory>
<exception-handler-factory>my.package.MyExceptionHandlerFactory</exception-handler-factory>
</factory>
I wondered how I could get the dependencies injected into an ExceptionHandlerFactory and ExceptionHandler?
You can't do this out of the box.
It would be possible by creating an "spring aware" ExceptionHandlerFactory and create the ExceptionHandler instance via Spring but i would just get the beans manually in your ExceptionHandler like: Best way to manually pull a spring bean?

How to lookup a JSF bean from a Spring bean

I am having a JSF application and I am using Spring for bean management. I registered all JSF beans as Spring beans and everything was running very smoothly. Now, I have got a requirement to use JSF's view scope, so it means I have to create a JSF managed bean (which I did using JSF annotations and all).
Now, I want to access this JSF bean in a Spring bean, certainly I cannot inject or get it using Spring's application context because Spring container doesn't know about this bean. So, currently in my Spring bean code I am getting this JSF bean using JSF's EL Resolver (examples mentioned here).
What I want to know is that is there any better way to do this?
My application is on Spring 4.0 and JSF 2.0 (MyFaces implementation), please let me know if you need any other information. I have not placed code because all my code is in remote server, moreover I think code is not required for this as there is no debugging over here.

Integrating Spring request scope with JSF controllers

I am working on a project that uses a hybrid of JSF and Spring-MVC. User interface endpoints are accessed through a JSF front controller (javax.faces.webapp.FacesServlet), whereas REST service calls are accessed through a Spring-MVC front controller (org.springframework.web.servlet.DispatcherServlet). Deeper layers are Spring-managed (more-or-less). I don't like this arrangement, but I'm not in a position to change it.
The problem is that Spring's request-scoped beans aren't initialized when processing requests that come in through the JSF front controller. Is there an off-the-shelf solution for integrating Spring's WebApplicationContext with the JSF machinery so that the Spring request-scoped beans are initialized for each request regardless of whether that request comes through the JSF or Spring servlet?
There are two ways to integrate JSF with Spring, depending on which framework you want to give most control:
JSF front controller:
One way is to route all requests via the JSF faces servlet and let JSF route them to controllers, let JSF manage navigation state via faces-config. Then inject spring beans into JSF managed beans and access spring beans from the facelets view using value expressions via SpringBeanFacesELResolver.
See this post for a working example.
Spring front controller: put spring as a front controller with a dispatcher servlet and put in place spring webflow. This is the preferred and most powerful way to integrate the two frameworks, see this section of the documentation.
Spring webflow will manage the navigation state and the faces config file is mostly empty. There is no need for the JSF managed beans layer, request get directly handled by webflow.
Actions in JSF buttons trigger webflow transitions directly, and spring beans can also be used in value expressions to build the view. With this solution the integration with Spring is more seamless as webflow offers more possibilities then the JSF navigation mechanism: trigger bean methods between transitions, post redirect get pattern for avoiding double submissions.
Namelly the problem with the initialization of spring request scoped beans is solved with this direction, altough an alternative for this is to add a RequestContextListener OR a RequestContextFilter to web.xml (see section 3.4.4.1 of the docs).

use javax.faces.view.ViewScoped with CDI Spring bean and JSF

I'm using Spring 3.1 JSF 2.2.
Annoting Bean with ViewScoped introduced by JSF 2.2 not work.
#javax.inject.Named
#javax.faces.view.ViewScoped
public class TestBean {
#PostConstruct
public void init(){sysout("Why spring invoke this when initializing context :-( ");}
}
In my applicationContext.xml there is an annotation component-scan tag
<context:component-scan base-package="com.test"/>
Spring 3.1 detect and deal with CDI annotation but #javax.faces.view.ViewScoped not work. I know there is another solution by creating my own ViewScoped implementation but i want to know why #javax.faces.view.ViewScoped not work
Best solution was removing spring and using a Java EE implementation of CDI
You should notice that JSF annotations will nor work for Spring beans, because JSF beans located in different context.
But view scope implementation is pretty simple. I've created an artifact to solve this problem.
See my github javaplugs/spring-jsf repository.
javax.faces.view.ViewScoped will only work for JSF Managed Bean and not for CDI.
Use javax.faces.bean.ManagedBean annotation if you want to have a correct behavior with View scope instead of javax.inject.Named.
Regards

ICEfaces JSF Beans and Spring Beans, what VariableResolver?

I'm trying to understand the best way to use Spring (for dependency injection) with ICEfaces (Spring 3, ICEfaces 1.8.2, JSF RI 1.1).
Regarding the Spring reference manual, there are several possibilities to handle EL resolutions of beans:
If I use SpringBeanVariableResolver (which look best at first sight):
Beans that are refered in an EL-Expression will be managed by Spring
BUT the missing "extended request"-scope of Spring will cause problems, won't it?
If the DelegatingVariableResolver is used:
Beans that are refered in an EL-Expression will be managed by JSF
I need to define the EL-aware beans in faces-config.xml, which means I'm restraint to the limited options (e.g. no constructor DI).
Bean declarations are scattered across different files
Is this correct? Any suggestions? Hints? Best practices?
I ended up in mixing common JSF DI with Spring DI. That means that I inject Spring beans as JSF managed properties.

Resources