users@glassfish.java.net

Help with RequestDispatcher.include() in jsf component

From: <glassfish_at_javadesktop.org>
Date: Mon, 16 Apr 2007 00:41:17 PDT

Hi all,

Currently using Glassfish v2 b41c on WinXP with JDK 1.6.

I am trying to create a custom component that mimics jsp:include that will allow me to dynamically include snippets of jsf into a jsf page. The main idea is to use this to include dataTable jsf snippets that can be reconfigured at runtime. I also have some grander plans; but, for now, getting the includes to work would be great.

Unfortunately, I can't seem to get it to work at all. Currently I have a custom component that takes a page URL and uses the ServletContext to include the resource.

public class DataTableComponent extends UIOutput
{
    private String page;
    
    public String getFamily()
    {
        return UIOutput.COMPONENT_FAMILY;
    }
        
    public void encodeEnd(FacesContext context) throws IOException
    {
        // Include the template
        ServletContext sContext = (ServletContext)context.getExternalContext().getContext();
        ServletRequest request = (ServletRequest)context.getExternalContext().getRequest();
        ServletResponse response = (ServletResponse)context.getExternalContext().getResponse();
        try
        {
            sContext.getRequestDispatcher(page).include(request, response);
        }
        catch (Exception ex)
        {
            throw new RuntimeException(ex);
        }
    }
}

There are no exceptions and I can see the non jsf components from the included page in the html output, but the jsf components in the included page are not rendered.

My main page:

<%_at_page contentType="text/html"%>
<%_at_page pageEncoding="UTF-8"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<%_at_taglib prefix="tcms" uri="/WEB-INF/tcmsui" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
    <body>
        <f:view>
            <%-- This works when you uncomment it
            <jsp:include page="/include/includeTest1.jsp"/>
            --%>
            
            <%-- These never work --%>
            <tcms:dataTable page="/include/includeTest1.jsp"/>
            <tcms:dataTable page="/include/includeTest2.jsp"/>
        </f:view>
    </body>
</html>

And includeTest1.jsp looks like this:

<%_at_page contentType="text/html"%>
<%_at_page pageEncoding="UTF-8"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>

&lt;p>
    <h:outputText value="Some Text"/>
&lt;/p>
&lt;p>
    <h:outputText value="And some more text"/>
&lt;/p>


Any help would be greatly appreciated. I have zipped up the netbeans project and stuck it on http://ctr.id.au/includetest.zip

It will build a war file and the url to access is /includetest/index.faces. If someone has any ideas, or some time to do a bit of debugging, that would be fantastic.

Thanks
Cameron
[Message sent by forum member 'cameronr' (cameronr)]

http://forums.java.net/jive/thread.jspa?messageID=212490