BeanFactory vs ApplicationContext

The spring docs are great on this: 3.8.1. BeanFactory or ApplicationContext?. They have a table with a comparison, I’ll post a snippet: Bean Factory Bean instantiation/wiring Application Context Bean instantiation/wiring Automatic BeanPostProcessor registration Automatic BeanFactoryPostProcessor registration Convenient MessageSource access (for i18n) ApplicationEvent publication So if you need any of the points presented on the Application … Read more

ViewParam vs @ManagedProperty(value = “#{param.id}”)

<f:viewParam>: Sets the value during update model values phase only (since it extends UIInput). The set value is not available during @PostConstruct, so you need an additional <f:event type=”preRenderView” listener=”#{bean.init}” /> inside the <f:metadata> to do initialization/preloading based on the set values. Since JSF 2.2 you could use <f:viewAction> for that instead. Allows for nested … Read more

How do I process GET query string URL parameters in backing bean on page load?

Yes, you can use the <f:viewParam> to set a request parameter as a managed bean property. <f:metadata> <f:viewParam name=”companyId” value=”#{bean.companyId}” /> </f:metadata> You can if necessary invoke a bean action using <f:viewAction> (JSF 2.2+ only) or <f:event type=”preRenderView”>. <f:metadata> <f:viewParam name=”companyId” value=”#{bean.companyId}” /> <f:viewAction action=”#{bean.onload}” /> </f:metadata> When using <f:viewAction> you can even return a … Read more

Get JSF managed bean by name in any Servlet related class

In a servlet based artifact, such as @WebServlet, @WebFilter and @WebListener, you can grab a “plain vanilla” JSF @ManagedBean @RequestScoped by: Bean bean = (Bean) request.getAttribute(“beanName”); and @ManagedBean @SessionScoped by: Bean bean = (Bean) request.getSession().getAttribute(“beanName”); and @ManagedBean @ApplicationScoped by: Bean bean = (Bean) getServletContext().getAttribute(“beanName”); Note that this prerequires that the bean is already autocreated by … Read more

Spring JSF integration: how to inject a Spring component/service in JSF managed bean?

@ManagedBean vs @Controller First of all, you should choose one framework to manage your beans. You should choose either JSF or Spring (or CDI) to manage your beans. Whilst the following works, it is fundamentally wrong: @ManagedBean // JSF-managed. @Controller // Spring-managed. public class BadBean {} You end up with two completely separate instances of … Read more

Identifying and solving javax.el.PropertyNotFoundException: Target Unreachable

1. Target Unreachable, identifier ‘bean’ resolved to null This boils down to that the managed bean instance itself could not be found by exactly that identifier (managed bean name) in EL like so #{bean}. Identifying the cause can be broken down into three steps: a. Who’s managing the bean? b. What’s the (default) managed bean … Read more

How to choose the right bean scope?

Introduction It represents the scope (the lifetime) of the bean. This is easier to understand if you are familiar with “under the covers” working of a basic servlet web application: How do servlets work? Instantiation, sessions, shared variables and multithreading. @Request/View/Flow/Session/ApplicationScoped A @RequestScoped bean lives as long as a single HTTP request-response cycle (note that … Read more