I would be very reluctant to try to directly call the JSP tag outside the JSP context. As the documentation points out, the similarities between JSP and Facelets are pretty superficial.
One hacked one (I suspect that any solution will be a hack) may include JSP, dropping the servlet API.
This function enables this resource using RequestDispatcher :
public class Includer { public static String include(String resource) { FacesContext context = FacesContext .getCurrentInstance(); ExternalContext ext = context.getExternalContext(); include(ext.getContext(), ext.getRequest(), ext .getResponse(), resource); return ""; } private static void include(Object context, Object request, Object response, String resource) { ServletContext servletContext = (ServletContext) context; ServletRequest servletRequest = (ServletRequest) request; ServletResponse servletResponse = (ServletResponse) response; RequestDispatcher dispatcher = servletContext .getRequestDispatcher(resource); try { dispatcher.include(servletRequest, servletResponse); } catch (IOException e) { throw new FacesException(e); } catch (ServletException e) { throw new FacesException(e); } } }
This function is defined in the taglet file of the Facelet file. WEB-INF / facelets / include.taglib.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE facelet-taglib PUBLIC "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN" "http://java.sun.com/dtd/facelet-taglib_1_0.dtd"> <facelet-taglib xmlns="http://java.sun.com/JSF/Facelet"> <namespace>http://demo</namespace> <function> <function-name>include</function-name> <function-class>inc.Includer</function-class> <function-signature> java.lang.String include(java.lang.String) </function-signature> </function> </facelet-taglib>
This is indicated as a library in WEB-INF / web.xml using the context parameter :
<context-param> <param-name>facelets.LIBRARIES</param-name> <param-value>/WEB-INF/facelets/include.taglib.xml</param-value> </context-param>
Using example
JSP to include, includeme.jsp:
<?xml version="1.0" encoding="UTF-8" ?> <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0"> <jsp:directive.page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" /> <b>Some data: ${foo}</b> </jsp:root>
Front side including JSP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:demo="http://demo"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>JSP include hack</title> </head> <body> <h:form> <p> ${demo:include('/includeme.jsp')} </p> <h:inputText type="text" value="#{foo}" /> <h:commandButton type="submit" /> </h:form> </body> </html>
Note the use of $ {demo: include ('/includeme.jsp')} to invoke the query manager (the function returns an empty string). The function is enabled with the xmlns: demo = "http: // demo" attribute. The request scoping variable foo is bound to a text field and matched by JSP.
All I can say about this is that it worked for me, and there are probably a dozen reasons why it will not work with a certain combination of tags or with certain combinations of JSF libraries. Caution emptor.