David,
We recently had a requirement where we wanted to include a panel from our
new Wicket app on one of our non-wicket legacy applications. To do this I
created a behavior and container page which will spit out any component
between a javascript document.write method. Then on your non-wicket page you
include a script tag whose src attribute is the address of the container
page. Below are the 3 files that make up the solution. If the files don't
show up properly let me know and I can find another way to post them.
ComponentAsWidgetContainerPage.java:
package com.company.ui.wicket.page;
import wicket.Component;
import com.company.ui.wicket.behaviors.JavascriptDocumentWritingBehavior;
/**
* A page which widgetizes a component so it can be used on any page outside
of
* the containing wicket application.
*
* @see [EMAIL PROTECTED] JavascriptDocumentWritingBehavior}
*
*/
public abstract class ComponentAsWidgetContainerPage extends WebPage
{
private static final String WIDGET_ID = "componentAsWidget";
public ComponentAsWidgetContainerPage()
{
super();
Component widget = getComponent(WIDGET_ID);
if (widget.getId() != WIDGET_ID)
{
throw new IllegalArgumentException("ID for the widget
component should be
set to "
+ WIDGET_ID);
}
widget.add(new JavascriptDocumentWritingBehavior());
add(widget);
}
/**
* Implementations should return the component that is to be widgetized.
*
* @param id
* The id which should be set on the returned component.
*
* @see [EMAIL PROTECTED] JavascriptDocumentWritingBehavior}
* @return
*/
public abstract Component getComponent(String id);
}
JavascriptDocumentWritingBehavior.java:
package com.company.ui.wicket.behaviors;
import wicket.Component;
import wicket.IRequestTarget;
import wicket.MarkupContainer;
import wicket.RequestCycle;
import wicket.Response;
import wicket.behavior.AbstractBehavior;
import wicket.markup.ComponentTag;
import wicket.markup.html.internal.HtmlHeaderContainer;
import wicket.markup.parser.filter.HtmlHeaderSectionHandler;
import wicket.protocol.http.servlet.ServletWebRequest;
import wicket.request.target.component.ComponentRequestTarget;
import wicket.response.StringResponse;
import com.company.ui.wicket.page.ComponentAsWidgetContainerPage;
/**
* This behavior encapsulates any component in a
* "document.write('[renderedComponent]');" javascript line. The main use of
* this is to be able to "widgetize" any component to be used on any page
* outside of the containing application with a script tag in the form
of:<br/><br/>
*
* <script
*
src="/mywebapp/app?wicket:bookmarkablePage=:com.company.app.pages.[ComponentContainerPage]"
* type="text/javascript"></script> <br/> <br/> To be used with
* [EMAIL PROTECTED] ComponentAsWidgetContainerPage}
*/
public class JavascriptDocumentWritingBehavior extends AbstractBehavior
{
private static final long serialVersionUID = 1L;
private Response renderToResponse;
private Response originalResponse;
@Override
public void bind(Component component)
{
IRequestTarget target = new ComponentRequestTarget(component);
RequestCycle.get().setRequestTarget(target);
super.bind(component);
}
@Override
public void onComponentTag(Component component, ComponentTag tag)
{
renderToResponse = new StringResponse();
originalResponse =
RequestCycle.get().setResponse(renderToResponse);
renderToResponse.reset();
renderHeader(component);
super.onComponentTag(component, tag);
}
@Override
public void onRendered(Component component)
{
String renderedComponent =
renderToResponse.toString().replaceAll("(
?[\\r\\n] ?)+", "")
.replaceAll("'", "\\\\'");
// Need to make sure any relative server paths get the server
prepended
String host = ((ServletWebRequest)
component.getRequest()).getHttpServletRequest().getHeader(
"host");
renderedComponent = renderedComponent.replaceAll("href=\"/",
"href=\"http://" + host + "/");
renderedComponent = renderedComponent.replaceAll("src=\"/",
"src=\"http://" + host + "/");
originalResponse.write("document.write('");
originalResponse.write(renderedComponent);
originalResponse.write("');");
RequestCycle.get().setResponse(originalResponse);
super.onRendered(component);
}
/**
* Renders the header contributed tags to the response.
*
* @param response
* @param component
*/
private void renderHeader(final Component component)
{
final HtmlHeaderContainer header = new
HtmlHeaderContainer(HtmlHeaderSectionHandler.HEADER_ID);
if (component.getPage().get(HtmlHeaderSectionHandler.HEADER_ID)
!= null)
{
component.getPage().replace(header);
}
else
{
component.getPage().add(header);
}
component.renderHead(header);
component.detachBehaviors();
if (component instanceof MarkupContainer)
{
((MarkupContainer) component).visitChildren(new
Component.IVisitor()
{
public Object component(Component component)
{
if (component.isVisible())
{
component.renderHead(header);
component.detachBehaviors();
return CONTINUE_TRAVERSAL;
}
else
{
return
CONTINUE_TRAVERSAL_BUT_DONT_GO_DEEPER;
}
}
});
}
}
}
ComponentAsWidgetContainerPage.html:
<html xmlns:wicket="http://wicket.sourceforge.net">
<body>
<div wicket:id="componentAsWidget" />
</body>
</html>
David Leangen-8 wrote:
>
>
> Hello!
>
> We are currently happily using Wicket to develop our web applications.
> Wicket rules!
>
>
> However, now we are required to make "embeddable Ajax components", you
> know, like Google Maps that anybody can embed in their own html.
>
>
> Can anybody suggest an approach that would allow us to keep as much
> overlap/reusability/cohabitation between our existing Wicket apps and
> these Ajax components that we now need to make?
>
>
> For instance, would a reasonable approach be to use only Sciptaclous
> components in Wicket so we can use the exact same lib for our embeddable
> components?
>
> I guess ideally I'd like to be able to serve the XHRs from these
> components directly from Wicket (ultimate reusability/cohabitation), but
> this doesn't look like it would be an easy task...
>
>
> Any comments would be most welcome. I'm new to this. :-)
>
>
> Cheers,
> Dave
>
>
>
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by DB2 Express
> Download DB2 Express C - the FREE version of DB2 express and take
> control of your XML. No limits. Just data. Click to get it now.
> http://sourceforge.net/powerbar/db2/
> _______________________________________________
> Wicket-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/wicket-user
>
>
--
View this message in context:
http://www.nabble.com/Wicket-and-embeddable-Ajax-components-tf3604793.html#a10081903
Sent from the Wicket - User mailing list archive at Nabble.com.
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Wicket-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/wicket-user