EDIT: In my initial answer I overlooked the requirement to make this JavaScript file available from public URL. While researching for a way to to that, I realized that my whole approach was flawed. So, I move my original answer to the end and provide a more-to-the-point answer up here.
On order to publish a text resource on a public URL (JavaScript or CSS), you need to edit your init()
method in WicketApplication
by adding:
String resourceKey = "DYN_RES_KEY";
//load your text template
final TextTemplate textTemplate = new PackagedTextTemplate(MyPage.class, "script.js", "text/javascript", "UTF-8");
//add the resource
getSharedResources().add(resourceKey, new Resource() {
@Override
public IResourceStream getResourceStream() {
String queryParam = getParameters().getString("paramName");
//...do whatever you need with the parameters...
Map<String,Object> vars = new HashMap<String,Object>();
vars.put("param", queryParam);
String stringValue = textTemplate.asString(vars);
return new StringResourceStream(stringValue, textTemplate.getContentType());
}
});
//mount the resource at some public URL
mountSharedResource("/resource", Application.class.getName() + "/" + resourceKey);
//make alias, optional
getSharedResources().putClassAlias(MyPage.class, "scripts");
DISCLAIMER: This code is not written in IDE and never ran. As such, it might not even compile. Still, it should be enough to illustrate the way this could be accomplished.
This answer is based on "Dynamically Generate a CSS Stylesheet" and "Wicket Dynamic Image URL", see those sources for more details.
ORIGINAL ANSWER (useful if you want to add JavaScript or CSS as externally referenced file to your Wicket page, but not make it publicly visible):
Check out "Dynamically Generate a CSS Stylesheet" page in Wicket wiki and WICKET-2890 issue in JIRA. Since Wicket 1.4.10, the TextTemplateResourceReference class is now part of Wicket core, so you do not need to copy-paste it anymore.
The wiki page mentions CSS page, but the approach is the same for JavaScript or any other non-markup text content.