0
votes

While working with Spring 3.x and and JSF 2.x, we can register the Spring Beans EL resolver for JSF in the faces-config.xml as:

<application>
    <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>

Is there a way we can do this programmatically and get rid of faces-config.xml?

Edit: 02/27/2012
I found a way to get this working. Not quite sure yet but I'm still reviewing my solution. Here's is how I got it working.
1.Write a Java Servlet and implement its init() method which contains the code to register ELResolver i.e.

public final class ELResolverInitializerServlet extends HttpServlet {

private static final Logger LOGGER = Logger.getLogger(ELResolverInitializerServlet.class.getName());

private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public ELResolverInitializerServlet() {
    super();
}

/**
 * @see Servlet#init(ServletConfig)
 */
public void init(ServletConfig config) throws ServletException {
    FacesContext context = FacesContext.getCurrentInstance();
    LOGGER.info("::::::::::::::::::: Faces context: " + context);
    context.getApplication().addELResolver(new SpringBeanFacesELResolver());

}

}

2.Register this servlet using Spring's bootstrapper org.springframework.web.WebApplicationInitializer i.e.

public class AppInitializer implements WebApplicationInitializer {

private static final Logger LOGGER = Logger.getLogger(AppInitializer.class
        .getName());

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    LOGGER.info("Bootstraping Spring...");
            ...

    ServletRegistration.Dynamic elResolverInitializer = servletContext
            .addServlet("elResolverInit",
                    new ELResolverInitializerServlet());
    elResolverInitializer.setLoadOnStartup(2);
            ...
}

}

With this configuration, Spring bootstrapper registers the servlet as a loadOnStartup servlet. This servlet is loaded after the JSF's FacesContext is created. Then it adds the ELResolver to the JSF's context which is available for the application after the application starts up.

Any thoughts / reviews are welcome.

1

1 Answers

0
votes

Take a look at addELResolver method in Application class. Something like:

FacesContext.getCurrentInstance().getApplication().addELResolver(myResolver);