Sorry, I forgot a class import org.apache.wicket.IResourceListener; import org.apache.wicket.markup.html.WebMarkupContainer;
/** * @author Ernesto Reinaldo Barreiro (reier...@gmail.com) * */ public class DocumentResourceListener extends WebMarkupContainer implements IResourceListener { private static final long serialVersionUID = 1L; private IResourceListener resourceListener; /** * Constructor receiving an IResourceListener.. * * @param id * @param resourceListener */ public DocumentResourceListener(final String id, IResourceListener resourceListener) { super(id); this.resourceListener = resourceListener; } /** * Gets the url to use for this link. * * @return The URL that this link links to */ protected CharSequence getURL() { return urlFor(IResourceListener.INTERFACE); } @Override protected boolean getStatelessHint() { return false; } public void onResourceRequested() { this.resourceListener.onResourceRequested(); } } Best, Ernesto On Tue, Oct 6, 2009 at 9:53 AM, Ernesto Reinaldo Barreiro < reier...@gmail.com> wrote: > Hi Cristian, > I've got it working! > > Just place all the following files at the same package and you will get a > working example (I tested them with FF35 and IE7). > > import org.apache.wicket.ajax.AjaxRequestTarget; > import org.apache.wicket.ajax.markup.html.AjaxLink; > import org.apache.wicket.markup.html.WebPage; > import org.apache.wicket.markup.html.basic.Label; > import org.apache.wicket.model.AbstractReadOnlyModel; > > /** > * @author Ernesto Reinaldo Barreiro (reier...@gmail.com) > * > */ > public class TestPage extends WebPage{ > > private Label text; > private String labelText = "Hi!"; > private DocumentResourceListener documentResourceListener; > > /** > * > */ > public TestPage() { > AjaxLink<Void> link = new AjaxLink<Void>("link") { > private static final long serialVersionUID = 1L; > > @Override > public void onClick(AjaxRequestTarget target) { > TestPage.this.labelText = "Hi! and donwload image!"; > if(target!= null) { > target.addComponent(TestPage.this.text); > String url = documentResourceListener.getURL().toString(); > target.appendJavascript(";alert('Hi');window.location.href='"+url+"';"); > } > } > }; > add(link); > text = new Label("text", new AbstractReadOnlyModel<String>() { > private static final long serialVersionUID = 1L; > > @Override > public String getObject() { > return TestPage.this.labelText; > } > }); > text.setOutputMarkupId(true); > add(text); > documentResourceListener = new DocumentResourceListener("listener", new > MyPdfResource()); > add(documentResourceListener); > } > > } > > --------------------------- > > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> > <html xmlns:wicket="org.apache.wicket"> > <head> > </head> > <body> > <a wicket:id="link">Click Me</a> > <span wicket:id="text"></span> > <div wicket:id="listener"></div> > </body> > </html> > > --------------------------------- > > import java.io.ByteArrayOutputStream; > import java.io.IOException; > import java.io.InputStream; > import java.io.OutputStream; > > import org.apache.wicket.markup.html.DynamicWebResource; > import org.apache.wicket.protocol.http.WebResponse; > > /** > * @author Ernesto Reinaldo Barreiro (reier...@gmail.com) > * > */ > public class MyPdfResource extends DynamicWebResource { > > private static final long serialVersionUID = 1L; > > static int BUFFER_SIZE = 10*1024; > /** > * > */ > public MyPdfResource() { > } > > /* (non-Javadoc) > * @see org.apache.wicket.markup.html.DynamicWebResource#getResourceState() > */ > @Override > protected ResourceState getResourceState() { > return new ResourceState() { > @Override > public String getContentType() { > return "application/pdf"; > } > @Override > public byte[] getData() { > try { > return > bytes(MyPdfResource.class.getResourceAsStream("jta-1_1-spec.pdf")); > } catch (Exception e) { > return null; > } > } > }; > } > public static byte[] bytes(InputStream is) throws IOException { > ByteArrayOutputStream out = new ByteArrayOutputStream(); > copy(is, out); > return out.toByteArray(); > } > @Override > protected void setHeaders(WebResponse response) { > super.setHeaders(response); > response.setAttachmentHeader("jta-1_1-spec.pdf"); > } > public static void copy(InputStream is, OutputStream os) throws > IOException { > byte[] buf = new byte[BUFFER_SIZE]; > while (true) { > int tam = is.read(buf); > if (tam == -1) { > return; > } > os.write(buf, 0, tam); > } > } > } > > Just replace "jta-1_1-spec.pdf" with your own PDF file on MyPdfResource and > the exampel should work. An important bit here if > > @Override > protected void setHeaders(WebResponse response) { > .... > response.setAttachmentHeader("jta-1_1-spec.pdf"); > } > > which makes the file to treated as an attachment instead of replacing the > current page. > > Best, > > Ernesto > > P.S. Would it useful to add this to Wicket's wiki? >