Posts Tagged ‘jsf’

Using Spring with Mojarra InjectionProvider

Friday, May 22nd, 2009

Are you using SpringBeanFacesELResolver to handle dependency injection on managed beans? It works nice, but your faces-config grows fast with this approach.

Two months ago, I posted about using Spring injection outside application context. I forgot to mention that the same trick can be used to replace ELResolver solution if you are using Mojarra as your JSF provider.

You only need to add a servlet context parameter:

<context-param>
  <param-name>
    com.sun.faces.injectionProvider
  </param-name>
  <param-value>
    mypkg.SpringJSFInjectionProvider
  </param-value>
</context-param>

And the implementation of SpringJSFInjectionProvider is as simple as:

public class SpringJSFInjectionProvider
       implements InjectionProvider {
  private WebContainerInjectionProvider wcip =
      new WebContainerInjectionProvider();

  @Override
  public void inject(Object bean)
         throws InjectionProviderException {
    FacesContextUtils
      .getWebApplicationContext(
         FacesContext.getCurrentInstance())
      .getAutowireCapableBeanFactory()
      .autowireBeanProperties(bean,
         AutowireCapableBeanFactory.AUTOWIRE_NO,
         false);
    wcip.inject(bean);
  }
  @Override
  public void invokePreDestroy(Object bean)
         throws InjectionProviderException {
    wcip.invokePreDestroy(bean);
  }
  @Override
  public void invokePostConstruct(Object bean)
         throws InjectionProviderException {
    wcip.invokePostConstruct(bean);
  }
}

Adding this, you can shrink your faces-context, removing all “managed-property” tags. Your managed beans can look more JavaEE 5 after you change this:

private BO bo;

public void setBo(BO bo) {
  this.bo = bo;
}

To this:

@Resource(name="bean-on-appctx")
private BO bo;

No setter, no ELResolver. This means less code to maintain! If you plan to migrate from Spring to EJB, this means you will only need to promote the BO to an stateless bean and change @Resource to @EJB!

Password and confirmation with JSF

Tuesday, March 17th, 2009

If you need to validate a field in JSF and the value is dependent of another field (like password/confirmation), BalusC posted a nice tutorial about it. You need to create a JSF Validator and add a <f:attribute/> to your field. Easier than I thought…

Marker-only validation error with RichFaces

Monday, January 12th, 2009

I’ve made a simple JSP 2.1 tag file to show only a small icon on my validation errors (and a tooltip to show message details). This is the .tag file:

<%@ tag pageEncoding="UTF-8" %>
<%@ attribute name="field" required="true" deferredValue="true" %>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
<%@ taglib prefix="a4j" uri="http://richfaces.org/a4j" %>
<%@ taglib prefix="rich" uri="http://richfaces.org/rich" %>
<rich:message for="#{field}" showSummary="false" showDetail="false">
  <f:facet name="errorMarker">
    <a4j:outputPanel>
      <h:graphicImage value="path-to-bullet"/>
      <rich:toolTip>
        <rich:message for="#{field}"/>
      </rich:toolTip>
    </a4j:outputPanel>
  </f:facet>
</rich:message>

It can be used like this:

<tags:errorMessages field="id-of-the-field"/>

Instead of showing a big chunk of text, your webdesigner will only deal with a small bullet that will show a nice tooltip with the full message. Some “catches”:

  • <rich:message tooltip=”true”> does not show tooltip if you disable both summary and details;
  • Since <rich:toolTip> can’t work with <h:panelGrid>, the <a4j:outputPanel> will show the tooltip without adding any extra visual effect (like <rich:panel> does);
  • The inner <rich:message> will render the message inside the tooltip (notice that outer <rich:message> renders only the marker, because summary and details are “off”).