While working on a recent project in GWT, I needed to embed a full HTML document inside an IFRAME. And I didn't want to specify a remote URL for the IFRAME - I actually wanted to shove the HTML content directly into the IFRAME.
Initially I thought it was trivial: create an IFrameElement and call setInnerHTML.
IFrameElement iframe = Document.get().createIFrameElement(); iframe.setInnerHTML(htmlContent);
Unfortunately, that doesn't work. It doesn't cause any errors, but it doesn't actually fill in the iframe. Instead, you have to use native javascript to write into the document object for the iframe:
private final native void fillIframe(IFrameElement iframe, String content) /*-{ var doc = iframe.document; if(iframe.contentDocument) doc = iframe.contentDocument; // For NS6 else if(iframe.contentWindow) doc = iframe.contentWindow.document; // For IE5.5 and IE6 // Put the content in the iframe doc.open(); doc.writeln(content); doc.close(); }-*/;
Voila! However, one of the side effects is that the iframe doesn't include any CSS unless your embedded HTML references a stylesheet. If you want to manually add a reference to a specific stylesheet, you can do that through native javascript as well:
private final native void addHeadElement(IFrameElement iframe, String cssUrl) /*-{ setTimeout(function() { var body; if ( iframe.contentDocument ) { // FF iframe.contentDocument.designMode= "On"; iframe.contentDocument.execCommand('styleWithCSS',false,'false'); body= iframe.contentDocument.body; } else if ( iframe.contentWindow ) { // IE body = iframe.contentWindow.document.body; } if (body == null) { return; } body.className = "custom-body-classname"; var head = body.previousSibling; if(head == null) { head = iframe.contentWindow.document.createElement("head"); iframe.contentWindow.document.childNodes[0].insertBefore(head, body); } var fileref = iframe.contentWindow.document.createElement("link"); fileref.setAttribute("rel", "stylesheet"); fileref.setAttribute("type", "text/css"); fileref.setAttribute("href", cssUrl); head.appendChild(fileref); }, 50); }-*/;}
There's still one more problem. You can't use either of native methods until the IFRAME element has been attached to the DOM. The easiest way around this is to add the IFRAME element to a panel and over the onLoad() method for the panel:
final IFrameElement iframe = Document.get().createIFrameElement(); FlowPanel innerBox = new FlowPanel() { @Override protected void onLoad() { super.onLoad(); // Fill the IFrame with the content html fillIframe(iframe, contentHtml); // Add a HEAD element to the IFrame with the appropriate CSS addHeadElement(iframe, cssUrl); } }; innerBox.getElement().appendChild(iframe);
I got the idea and the important code from an article titled Inject HTML into an IFrame from Software As She's Developed.
So I found a scenario today where we had two different PropertyPlaceHolderConfigurer beans declared in two separate XML files. Unfortunately, the second property file wasn’t getting loaded, so we would get errors about Spring being unable to resolve particular properties.
I found this great post summarizing the solution:
http://dotal.wordpress.com/2007/09/14/mulitple-propertyplaceholderconfigurer-configurations/
After my recent work with Ruby, I've really started looking at creating RESTful web services in Java. I work with a client who has a lot of Struts applications, so my real area of interest is figuring out how to get Struts to provide RESTful interfaces. Here's what I've found so far (much of which doesn't relate to Struts):
In an earlier post, I described a class to handle redirects in Struts and passing parameters along. That technique is not necessary; as of Struts 1.2.7, you can use the ActionRedirect class instead.
Here's a basic example from inside the execute method in an Action.
OnJava has an interesting article on generic approach to handling exceptions within a J2EE framework. The basic idea involves creating a generic base class for exceptions and a basic handler for all requests that includes dispatcher to pass the request along to the appropriate method.
Recent comments
1 year 40 weeks ago