Get the HTML output data from a wicket component - jsonp

I'm currently writing a web widget, and I would like to fill the content of this widget with some HTML data generated by a wicket component on my server.
To do that, the server will output the HTML data via JSONP. So far so good.
However I need to get this HTML data. How can I get on the server the HTML output from some wicket component?

I dont know if this can be applied to your configuration, but I am using a view lines of code to retrieve rendered html which I wrote some time ago for building html based emails to be able to use wicket components in it
protected final String renderPage(Component page) {
final Response oldResponse = RequestCycle.get().getResponse();
BufferedWebResponse tempResponse = new BufferedWebResponse((WebResponse) RequestCycle.get().getOriginalResponse());
try {
RequestCycle.get().setResponse(tempResponse);
page.render();
}
finally {
RequestCycle.get().setResponse(oldResponse);
}
return tempResponse.toString();
}
As this rendering is made within an actual webapplication cycle but independently from the actual requestcycle, it is recommended to preserve the original requestcycle. The page will be rendered in your temporary webresponse from which you can retrieve the rendered html output.
Hope this may be what you are looking for

You might find everything you need in this Wicket Wiki article and the linked source code: Use wicket as template engine
Although I must admit that I never tried that, just read it and remembered for further reference...

Related

How to render a jade block(section) using links?

I was hoping someone had any insight on this basic approach. Sample scenario:
I have a dashboard template with menu links a(href "/page") and I want to click the links to render a different section/view on the template. I used block content...but does it need a specific route?
If I understand correctly, you want to update the content of the page on click of the link without the page getting refreshed.
In that case, no you can't do it using block content.
The purpose of block content is to apply inheritance in your templates.
The typical use of block content would be creating a layout and then creating more specific page from the layout. This is what the official documentation says.
The reason why you cannot do it because, jade is server side templating library. This resolves the block content on server. Once rendered in client, the html looses all the information that was specific to jade (which is obvious because its an html afterall).
What you can do here is
Create a /page.jade and make a ajax call to a service. That service should return an already compiled html string. Since you are using jade, you can easily use jade.compile(source, options) to template / generate html.
Jade API documentation here

How to pass data between pages through worklight client API

I want to invoke a procedure in one page and use it in another page, and the response is only used by the next page, so I think JsonStore is not suit for that. Should I define a global var?
Is there any code sample to do such things? Thanks for your help.
I presume by pages you mean different HTML files. If so, that is not recommended, Worklight is intended for single page applications. There are no code samples that show how to do that.
I would recommended having a single HTML page and using something like jQuery.load to inject new HTML / DOM elements. By dynamically injecting new HTML your single/main HTML file shouldn't be too big and you can destroy (i.e. remove from memory / the DOM) unused DOM elements. Searching on Google for page fragments and html templates could help you find examples. The idea is that you don't lose the JavaScript context.
Maybe you can get away with doing a new init to re-initialize JSONStore (it won't delete any the data, just give you access) on every new HTML page and use get to get access to the JSONStore collections to perform operations such as find.

Apache Wicket how to render a (non-wicket) response page

I'm using Apache Wicket and I have following problem:
Inside a onSubmit() method I am sending a POST request to external web address with Apache httpClient. As a response I get html (inside my response object).
How can I get Wicket to render this html in browser?
So basically what I'm trying to do here, is simply what would normally happen if I submitted a html form to this web address. However for security reasons I don't want to give user pages containing forms that contain this data I'm trying to send.
You can get the response via getResponse() in any component. (I assume the onSubmit() is on a form).
How about something like:
getResponse().reset();
getResponse().write(htmlPage);
htmlPage should be a CharSequence containing the html page to be rendered.

Printing ListView items fetched from a JSON datasource in a Windows 8 (HTML + JS) app

I have a Windows 8 app built using HTML & JavaScript. It has a ListView showing a dynamic list of places generated from a JSON data source.
I'm using Print Contract code from a MSDN sample (that shows how to print a page with static content) to implement printing functionality to this app. Only half of the list in the ListView shows up when printed.
How can I print the full list from the ListView & make it spread across multiple pages when the content is large?
The trick here is in providing a document to the MSApp.getHtmlPrintDocumentSource method that correctly represents what you want printed, which isn't necessarily the same as what's displayed on the page.
The sample you reference simply passes the current document object to getHtmlPrintDocumentSource, and is the easiest way to print, but as you've noticed, may not give you exactly the results you want.
A simple way to customize the output, as noted in chapter 15 of Kraig Brockschmidt's excellent (and free) ebook on HTML and JavaScript Windows Store apps, is to add a media query for the print media type, and use CSS to modify the output of the page, like so:
#media print {
.win-backbutton {
display: none;
}
.watermark {
display: none;
}
.titlearea {
font-size: xx-large;
}
}
This will not, however, likely solve the problem you're facing. In your case, what will probably work best is to create a separate page that provides output which is more print-friendly.
As an example, based on scenario 4 in the print contract sample, if I place an HTML page called print.html in the root of my app, I can reference it using the following code (note the ms-appx:/// scheme, which refers to content in my app package):
// set the alternate content
var alternateLink = document.createElement("link");
alternateLink.setAttribute("id", "alternateContent");
alternateLink.setAttribute("rel", "alternate");
alternateLink.setAttribute("href", "ms-appx:///print.html");
alternateLink.setAttribute("media", "print");
document.getElementsByTagName("head")[0].appendChild(alternateLink);
Now, when the devices charm is invoked, my alternate content page is supplied as the source for printing, rather than the current page. You can have your alternate page load the content from the JSON source and render it in a printer-friendly fashion, which may be different from how the content is displayed on the page.
For more info on Windows Store app development, register for Generation App.

how to read/parse dynamically generated web content?

I need to find a way to write a program (in any language) that will connect to a website and read dynamically generated data from the website.
Note that it's dynamically generated--it's not enough to get the source html, because the data I'm interested in is generated via javascript that references back-end code. So when i view the webpage source, I can't see the data. (For example, go to google, and do a search. Check the source code on the search results page. Very little of the data your browser is displaying is reflected in the source--most of it is dynamically generated. I need some way to access this data.)
Pick a language and environment that includes an HTML renderer (e.g. .NET and the WebBrowser control). Use the HTML renderer to get the URL and produce an HTML DOM in memory (making sure that scripting is enabled). Read the contents of the HTML DOM after the renderer has done its work.
Example (you'll need to do this inside a System.Windows.Form derived class):
WebBrowser browser = new WebBrowser();
browser.Navigate("http://www.google.com");
HtmlDocument document = browser.Document;
// extract what you want from the document
I used to have a Perl program to access Mapguide.com to get the drive direction from one location to another location. I parsed the returned page and save to database. If the source never change their format, it is OK. the problem is the source format often change, your parser also need change.
A simple thought: if we're talking about AJAX, you can rather look up the urls for the dynamic data. Then you can use the javascript on the page you're talking about to reformat this.
If you have Firefox/greasemonkey making a DOM dumper should be a simple matter.