在使用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?
编辑:02/27/2012
我找到了一种方法来实现这一目标。还不太确定,但我还在检查我的解决方案。这就是我如何运作。
1.编写Java Servlet并实现其init()方法,该方法包含注册ELResolver的代码,即
public final class 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的bootstrapper org.springframework.web.WebApplicationInitializer注册这个servlet,即
public class 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。在创建JSF的FacesContext之后加载此servlet。然后它将ELResolver添加到JSF的上下文中,该上下文在应用程序启动后可供应用程序使用。
欢迎任何想法/评论。
答案 0 :(得分:0)
查看Application
课程中的addELResolver方法。
类似的东西:
FacesContext.getCurrentInstance().getApplication().addELResolver(myResolver);