Step 1: Override the AbstractAjaxBehavior class functionality as below.
public abstract class ClassName extends AbstractAjaxBehavior {
private static final long serialVersionUID = 1L;
/**
* initiate to call the java script file open dialog
* @param target
*/
public void initiate(AjaxRequestTarget target) {
target.appendJavascript("window.open('" + getCallbackUrl()
+ "', '', 'scrollbars=yes,location=no,menuBar=no,resizable=yes,status=no,toolbar=no')");
}
/**
* Called when a request to a behavior is received.
* @see org.apache.wicket.behavior.IBehaviorListener#onRequest()
*/
@Override
public void onRequest() {
RequestCycle rc = getComponent().getRequestCycle();
final WebResponse webResponse = (WebResponse) getComponent().getRequestCycle().getResponse();
webResponse.setLastModifiedTime(Time.now());
webResponse.setContentType("application/pdf");
webResponse.setHeader("Content-Disposition", "inline; filename=\"" + getFileName() + "\"");
rc.setRequestTarget(new IRequestTarget() {
public void respond(RequestCycle requestCycle) {
OutputStreamWriter writer = null;
try {
OutputStream stream = webResponse.getOutputStream();
writer = new OutputStreamWriter(stream, "ISO-8859-1");
writer.write(getStringToWriteonPDF());
writer.flush();
} catch (IOException ioException) {
ioException.printStackTrace();
} finally {
try {
writer.close();
} catch (IOException ioException) {
}
}
}
public void detach(RequestCycle requestCycle) {
}
});
}
/**
* get the File Name
* @see ResourceStreamRequestTarget#getFileName()
*/
protected abstract String getFileName();
protected abstract String getStringToWriteonPDF();
}
Step 2: Use the override behavior in Component class
public void onClick(AjaxRequestTarget target) {
ClassName behavior = new ClassName () {
private static final long serialVersionUID = 1L;
@Override
protected String getStringToWriteonPDF() {
return "out put of PDF-it may get from Source (Database/webservices)";
}
@Override
protected String getFileName() {
return "File Name of PDF";
}
};
add(behavior);
behavior.initiate(target);
}
No comments:
Post a Comment