官方主页:http://tapestry.apache.org/
Apache Tapstry一直是我比较欣赏的MVC框架,他的很多设计理念都比同时代的Struts来得先进。
可能是由于中文资料的不足而一直没有被广大Java爱好者们所熟知。
目前Tapstry5正在热火朝天的开发中(说实话,这个版本已经让我苦苦等待很久了,至今还没有Release)。
事先声明一下,这个系列的所有代码并非都是我所作,但是当年,我正是通过它们才对T3有了现在的认识。
IDE:MyEclipse6.0(他自带Tapestry3的类库)
如下图所示建立工程:
用到的jar包一览如下:
bsf-2.3.0.jar
commons-beanutils-1.6.1.jar
commons-codec-1.2.jar
commons-collections-2.1.jar
commons-digester-1.5.jar
commons-fileupload-1.0.jar
commons-lang-1.0.jar
commons-logging-1.0.2.jar
jakarta-oro-2.0.6.jar
javassist-2.5.1.jar
ognl-2.6.7.jar
tapestry-3.0.3.jar
tapestry-contrib-3.0.3.jar
xercesImpl.jar
xmlParserAPIs.jar
Home.java
- package mo.org.cpttm.album;
- import org.apache.tapestry.*;
- import org.apache.tapestry.html.*;
- public abstract class Home extends BasePage {
- public String getImageUrl(int imageId) {
- return getEngine().getService("image").getLink(
- getRequestCycle(),
- null,
- new Object[] { new Integer(imageId) }).getURL();
- }
- public abstract String getImageId();
- public void onOk(IRequestCycle cycle) {
- cycle.activate("Upload");
- }
- }
ImageDB.java
- package mo.org.cpttm.album;
- import java.io.*;
- import javax.servlet.*;
- public class ImageDB {
- public static byte[] loadImage(int imageId, ServletContext context) {
- File imageFile = new File(
- context.getRealPath("/WEB-INF/imagedb"),
- imageId + ".jpg");
- try {
- FileInputStream input = new FileInputStream(imageFile);
- try {
- byte[] imageData = new byte[(int) imageFile.length()];
- input.read(imageData);
- return imageData;
- } finally {
- input.close();
- }
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- public static void saveImage(int imageId, byte[] imageData, ServletContext context) {
- File imageFile = new File(
- context.getRealPath("/WEB-INF/imagedb"),
- imageId + ".jpg");
- try {
- FileOutputStream output = new FileOutputStream(imageFile);
- try {
- output.write(imageData);
- } finally {
- output.close();
- }
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- }
ImageService.java
- package mo.org.cpttm.album;
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- import org.apache.tapestry.*;
- import org.apache.tapestry.engine.*;
- import org.apache.tapestry.request.*;
- public class ImageService extends AbstractService {
- public String getName() {
- return "image";
- }
- public void service(
- IEngineServiceView engine,
- IRequestCycle cycle,
- ResponseOutputStream output) throws ServletException, IOException {
- int imageId = ((Integer) getParameters(cycle)[0]).intValue();
- byte imageData[] = ImageDB.loadImage(
- imageId,
- cycle.getRequestContext().getServlet().getServletContext());
- HttpServletResponse response = cycle.getRequestContext().getResponse();
- response.setContentType("image/jpeg");
- response.setHeader("Content-disposition", "attachment; filename=foo.jpg");
- response.setContentLength(imageData.length);
- try {
- OutputStream out = response.getOutputStream();
- out.write(imageData);
- } catch (IOException e) {
- throw new ApplicationRuntimeException(e);
- }
- }
- public ILink getLink(IRequestCycle cycle, IComponent component, Object[] args) {
- return constructLink(cycle, getName(), null, args, false);
- }
- }
Upload.java
- package mo.org.cpttm.album;
- import java.io.*;
- import org.apache.tapestry.*;
- import org.apache.tapestry.html.*;
- import org.apache.tapestry.request.*;
- public abstract class Upload extends BasePage {
- public abstract IUploadFile getFile();
- public void onOk(IRequestCycle cycle) {
- if (getFile().getFileName().length()==0) {
- return;
- }
- byte imageData[] = new byte[(int) getFile().getSize()];
- InputStream fileInput = getFile().getStream();
- try {
- fileInput.read(imageData);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- ImageDB.saveImage(101, imageData, cycle.getRequestContext().getServlet().getServletContext());
- cycle.activate("Home");
- }
- }
Album.application
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE application PUBLIC
- "-//Apache Software Foundation//Tapestry Specification 3.0//EN"
- "http://jakarta.apache.org/tapestry/dtd/Tapestry_3_0.dtd">
- <!-- generated by Spindle, http://spindle.sourceforge.net -->
- <application name="Album" engine-class="org.apache.tapestry.engine.BaseEngine">
- <description>add a description</description>
- <page name="Home" specification-path="Home.page"/>
- <service name="image" class="mo.org.cpttm.album.ImageService"/>
- </application>
Home.html
- <html>
- <a href="" jwcid="download">Download photo #101</a>
- <img src="/Album/app?service=image&sp=101"/>
- <form jwcid="form">
- <input type="Submit" value="OK"/>
- </form>
- </html>
Home.page
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE page-specification PUBLIC
- "-//Apache Software Foundation//Tapestry Specification 3.0//EN"
- "http://jakarta.apache.org/tapestry/dtd/Tapestry_3_0.dtd">
- <!-- generated by Spindle, http://spindle.sourceforge.net -->
- <page-specification class="mo.org.cpttm.album.Home">
- <property-specification name="imageId" type="java.lang.String"/>
- <component id="download" type="ServiceLink">
- <static-binding name="service" value="image"/>
- <binding name="parameters" expression="101"/>
- </component>
- <component id="form" type="Form">
- <binding name="listener" expression="listeners.onOk"/>
- </component>
- </page-specification>
Upload.html
- <html>
- <form jwcid="uploadForm">
- <input type="File" jwcid="upload"/><p>
- <input type="Submit" value="OK"/>
- </form>
- </html>
Upload.page
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE page-specification PUBLIC
- "-//Apache Software Foundation//Tapestry Specification 3.0//EN"
- "http://jakarta.apache.org/tapestry/dtd/Tapestry_3_0.dtd">
- <!-- generated by Spindle, http://spindle.sourceforge.net -->
- <page-specification class="mo.org.cpttm.album.Upload">
- <property-specification name="file" type="org.apache.tapestry.request.IUploadFile"/>
- <component id="uploadForm" type="Form">
- <binding name="listener" expression="listeners.onOk"/>
- </component>
- <component id="upload" type="Upload">
- <binding name="file" expression="file"/>
- </component>
- </page-specification>
web.xml
- <?xml version="1.0"?>
- <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/TR/xmlschema-1/"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
- <display-name>Album</display-name>
- <servlet>
- <servlet-name>Album</servlet-name>
- <servlet-class>org.apache.tapestry.ApplicationServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>Album</servlet-name>
- <url-pattern>/app</url-pattern>
- </servlet-mapping>
- </web-app>