如何在JSF 2.0中编程注册ELResolver



在使用Spring 3.x和JSF 2.x时,我们可以在faces-config.xml中为JSF注册Spring Beans EL解析器,如下所示:

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

有没有一种方法可以通过编程实现这一点,并去掉faces-config.xml?

编辑:2012年2月27日
我找到了一种方法让它发挥作用。还不太确定,但我仍在审查我的解决方案。以下是我的工作原理
1.编写一个Java Servlet并实现其init()方法,该方法包含注册ELResolver的代码,即

公共最终类ELResolverInitializerServlet扩展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.使用Spring的引导程序org.springframework.web.WebApplicationInitializer注册此servlet,即

公共类AppInitializer实现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);
            ...
}

}

使用此配置,Spring引导程序将servlet注册为loadOnStartup servlet。这个servlet是在JSF的FacesContext创建之后加载的。然后,它将ELResolver添加到JSF的上下文中,该上下文在应用程序启动后可用于应用程序。

欢迎任何想法/评论

看看Application类中的addELResolver方法。类似于:

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

最新更新