dev@jsftemplating.java.net

Re: jsfTemplating: dynamically including page fragments

From: Ken Paulsen <Ken.Paulsen_at_Sun.COM>
Date: Thu, 29 May 2008 14:03:19 -0700

Hi Sean,

Sorry I didn't get back to you sooner. I did not write JSFT to work
with VisualWeb JSF, nor did I write it to not work with it. :) Nothing
comes to mind at the moment that would prevent this from working in a
visual web environment, but I don't know if they do anything that might
break this. For example, if prerender happens to late in the process,
it won't work (although the name suggests that it happens before
rendering, so I would think it should work).

I decided to try it out myself in a pure JSFTemplating environment
(note: I am already doing similar things in a few other areas, so I know
it works... but I wanted a simpler example). This is what I did:

<sun:page>
    <sun:head title="test" />
    <sun:body>
        <sun:form id="form">
            <sun:markup id="foo">
                <!afterCreate
                    setPageSessionAttribute(key="target"
value="$this{clientId}");
                />
            </sun:markup>
            <sun:button>
                <!command
                    getLayoutDefinition("newPage.jsf"
layoutDefinition=>$attribute{newPage});
                    getUIComponent("#{pageSession.target}"
component=>$attribute{parent});
                    buildUIComponentTree(layoutElement="#{newPage}",
parent="#{parent}");
                />
            </sun:button>
        </sun:form>
    </sun:body>
</sun:page>

This creates a page with a button and a markup component (with nothing
inside). You click the button and it adds the page. You can change the
event from "command" to "afterCreate" and it will happen on page load
vs. the next request. It works as expected. Note: JSFT will not create
duplicate component ids, so if you click the button again, most likely
nothing will happen as the components already exist. If you make the id
in the "newPage.jsf" page dynamic, it will add the content each time you
click the button.

If you send me a small (preferably 1 page) example of a VisualWeb JSF
application, I can try to modify it to see what happens when I execute
similar code in that environment.

Note: the above code is using JSFTemplating events / handlers. These
handlers do "almost" the same thing I wrote for you to do... but may be
slightly different (shouldn't be significant, though). Also this using
the "template" syntax instead of the faclets syntax, that shouldn't
matter either.

Thanks,

Ken Paulsen

Connolly, Sean wrote:
>
> Hi Ken & the dev team,
>
>
>
> We've been looking into our general problem
> (http://forum.java.sun.com/thread.jspa?threadID=5299960&messageID=10271569#10271569
> <http://forum.java.sun.com/thread.jspa?threadID=5299960&messageID=10271569#10271569>)
> a bit more and have come to realize that what is put in the XHTML file
> (tags and such) is essentially immutable, meaning for us that we
> cannot create <jsp:include..> tags where we want and move them to the
> correct location in the UIView tree at runtime. As we also can't
> create a <jsp:include..> tag programmatically in the backing bean,
> jsfTemplating is becoming a serious consideration for our group. We
> appreciate your help thus far and are eager to see if jsfTemplating
> can do what seems undoable.
>
>
>
> We went through our test application and simplified it a little:
>
> * dummy.jsf now consists of a single line: <sun:hyperlink text="TEST!" />
>
> * We played around with "LayoutDefinition def = .." a little more and
> it looks like we are finding the file correctly.
>
> * "def" is not null.
>
> * The prerender method is being called and the code to include
> dummy.jsf is being executed.
>
> * The main page was simplified slightly and we are ensuring that the
> destination component is an instance of UIComponent.
>
>
>
> Do you feel the environment may be the problem; was jsfTemplating not
> intended to work in VisualWeb JSF?
>
> Is there any documentation for programmatically emulating an 'include'
> in this fashion?
>
>
>
> Thanks a lot,
>
> Sean Connolly
>
>
>
> ------------------------------------------------------------------------
>
> *From:* Ken.Paulsen_at_Sun.COM [mailto:Ken.Paulsen_at_Sun.COM]
> *Sent:* Wednesday, May 28, 2008 2:00 PM
> *To:* Connolly, Sean {InSi~Palo Alto}
> *Cc:* jsftemplating
> *Subject:* Re: jsfTemplating: dynamically including page fragments
>
>
>
>
> Hi Sean,
>
> I haven't tried using this exactly as you described (in the visual web
> environment), but it might work. I'll try an example later today when
> I have time. A couple quick comments before I run to an appointment.
>
> * In dummy.jsf, it does not need to be a complete page... you can
> simply include the <sun:hyperlink .../> if you want.
>
> * Make sure "def" in your example is not (null), if it is, then it
> didn't find your dummy.jsf page. Make sure you give it a path
> relative to the context root of your application (or it may exist in a
> .jar file in your web-inf/lib directory if you want).
>
> * Make sure your prerender method is called.
>
> * Ensure markup1 is a valid non-null UIComponent.
>
> Feel free to cc the JSFTemplating "dev" alias. More people will see
> this and might be able to help. I cc'd the alias in this response.
>
> Ken
>
> Connolly, Sean wrote:
>
> Hi Ken,
>
>
>
> You recently commented on a thread I started in the JSF Sun Forums:
>
> http://forum.java.sun.com/thread.jspa?messageID=10271569&
> <http://forum.java.sun.com/thread.jspa?messageID=10271569&>
>
>
>
> I appreciate your suggestion and have actually already looked into
> jsfTemplating a little bit. I must admit I was a little overwhelmed by
> it in general and do not feel I really grasp the code snippet you
> suggested. You said I "/just need the parent UIComponent and the
> viewId of the content to add/." Is the parent UIComponent the
> component I want to include the other page's content in (such as a
> UIDiv component) or would it refer to a component in the other page
> that I want to 'grab'? Also, I don't see where the viewID comes into
> play or what its role would be; should I get an instance of UIViewRoot
> and get its ID?
>
>
>
>
>
> Playing dumb I threw together a quick 'dummy.jsf' page:
>
> <sun:page>
>
> <sun:html>
>
> <sun:head id="head" />
>
> <sun:body>
>
> <sun:form id="form">
>
> <sun:hyperlink text="hello world" />
>
> </sun:form>
>
> </sun:body>
>
> </sun:html>
>
> </sun:page>
>
>
>
>
>
> And then made a VisualWeb JSF page (The platform I am working in) and
> added the following code to the prerender() phase:
>
> FacesContext facesContext = getFacesContext().getCurrentInstance();
>
> LayoutDefinition def =
> LayoutDefinitionManager.getLayoutDefinition(facesContext, "dummy.jsf");
>
> LayoutViewHandler.buildUIComponentTree(facesContext, markup1, def);
>
>
>
> Where markup1 refers to the UIComponent:
> <webuijsf:markup binding="#{Page3.markup1}" id="markup1" tag="div"/>
>
>
>
>
>
> This did nothing, and I'm sure my naïve code it a butchery of your
> work & suggestion. If you see what I am doing wrong I would much
> appreciate some direction.. perhaps I am thinking about this whole
> thing wrong. Also, if we were to get the above example to work such
> that the contents of /dummy.jsf /were included as children of
> /markup1/, would I then be able to port this concept to a include
> components from a page that uses VisualWeb JSF UIComponents (such as
> Woodstock's in NetBeans) or must the included components be
> LayoutElements?
>
>
>
> Thank you for your time and I appreciate any assistance you may be
> able to offer,
>
> Sean Connolly
>
>
>