开发工具:
1。eclipse3.0 + spindle
2.容器:tomcat
开始:
1,首先,新建一个Tapestry工程文件,命名为upload,在向导里面有三个步骤,我基本是打入工程名就直接按回车了。新建好工程后,你可以看到一个典型的web目录,在 WEB-INF自动生成4个文件:
upload.appliaction tapestry的应用程序文件。
Home.html tapestry的模版文件
Home.page tapestry的定义的摸版的配置文件
web.xml
2.接着,再新建两个Tapestry page文件,各命名为Upload ,Download.(注意:要确认Generate an assiated HTML File,Ceate a new class被选定,对于要生成的.java我还是用Upload,Download来命名!);
对于生产.page和.jwc文件spindle提供了一个比较有用的向导,省了我好多工夫,呵呵。
3,现在就可以写代码了。
(1)Home.html
代码如下:
<html jwcid="@Shell" title="欢迎学习Tapestry!">
<H1><FONT color="red">十分钟搞定----在Tapestry实现文件上传与下载</FONT></H1> <body> <p><a href="#" jwcid="@PageLink" page="Upload">上传文件</a></p> <p><a href="#" jwcid="@PageLink" page="Dowload">下载文件</a></p>
</body></html>
(2) Upload.html
<html jwcid="@Shell" title="上传文件"> <body> <H1><FONT color="red">十分钟搞定----在Tapestry实现文件上传与下载</FONT></H1> <P><FORM jwcid="@Form" listener="ognl:listeners.submit"> 请选择你要上传的文件<input jwcid="@Upload" file="ognl:fileName" type="file"></input><INPUT type="submit" value="上传"/> </FORM> </body> </html>
(3)Dowload.html
<html jwcid="@Shell" title="上传文件"> <body> <H1><FONT color="red">十分钟搞定----在Tapestry实现文件上传与下载</FONT></H1>
<a href="#" jwcid="@DirectLink" listener="ognl:listeners.dowload">点击下载</a> </body> </html>
4.到此,三个简单的html文件已经写好,接下来就实现java文件。
(1).,Upload.java
package wenone;
import org.apache.tapestry.IRequestCycle;import java.io.*; import org.apache.tapestry.html.BasePage; import org.apache.tapestry.request.IUploadFile;
public abstract class Upload extends BasePage { public abstract IUploadFile getFile();
public void formSubmit(IRequestCycle cycle) { IUploadFile file = getFile();
if (file == null) return;
File newFile=new File(file.getFileName());
file.write(newFile);//存到当前工作区 cycle.activate("Home"); } }
5.Dowload.java
package wenone;
import org.apache.tapestry.IRequestCycle; import org.apache.tapestry.html.BasePage; import java.io.*;
public abstract class Download extends BasePage{
public void download(IRequestCycle cycle) {
File file=new File("file.zip"); try { HttpServletResponse response = cycle.getRequestContext().getResponse();
byte[] data = new byte[1024]; FileInputStream in = document.getFileInputstream();
response.setHeader("Content-disposition", "inline; filename=" + document.getFileName()); response.setContentType(file.getMimeType()); response.setContentLength(new Long(file.getSize()).intValue()); ServletOutputStream out = response.getOutputStream();
while (in.read(data) > -1) { out.write(data); } in.close(); response.flushBuffer(); } catch (IOException e) { e.printStackTrace(); } } 6,接近尾声;有了spindle这个例子配置文件基本不用管了,呵呵。
发布到tomcat就可以运行了,http://localhost:8080/upload/app
|