追忆Tapstry4.0------学习笔记(一)

官方主页:http://tapestry.apache.org/

这里使用Tapestry4.0.2。这个版本在T3的基础上,引入了HiveMind依赖注入框架。

T4.1版本还追加了很多Ajax特性,目前还没有Realse。

参考前面T3的例子http://blog.csdn.net/kunshan_shenbin/archive/2008/11/14/3299672.aspx

下面是T4版本的实现.

如下图所示建立工程:

所需Jar包如下图:

代码如下:

Home.java

  1. package com.ttdev.album;
  2. import org.apache.tapestry.annotations.*;
  3. import org.apache.tapestry.engine.*;
  4. import org.apache.tapestry.html.*;
  5. public abstract class Home extends BasePage {
  6.     
  7.     @InjectObject("engine-service:image")
  8.     public abstract IEngineService getImageService();
  9.     public String getImageUrl(int imageId) {
  10.         return getImageService().getLink(falsenew Object[] { Integer.toString(imageId) }).getURL();
  11.     }
  12.     public abstract String getImageId();
  13.     public void onOk() {
  14.         getRequestCycle().activate("Upload");
  15.     }
  16. }

ImageDB.java

  1. package com.ttdev.album;
  2. import java.io.*;
  3. public class ImageDB {
  4.     
  5.     public static byte[] loadImage(int imageId, String imageFolder) {
  6.         try {
  7.             File imageFile = new File(imageFolder, imageId + ".jpg");
  8.             FileInputStream input = new FileInputStream(imageFile);
  9.             ByteArrayOutputStream output = new ByteArrayOutputStream();
  10.             byte buf[] = new byte[1024];
  11.             for (;;) {
  12.                 int noBytesRead = input.read(buf);
  13.                 if (noBytesRead == -1) {
  14.                     return output.toByteArray();
  15.                 }
  16.                 output.write(buf, 0, noBytesRead);
  17.             }
  18.         } catch (IOException e) {
  19.             throw new RuntimeException(e);
  20.         }
  21.     }
  22.     
  23.     public static void saveImage(int imageId, byte[] imageData, String imageFolder) {
  24.         File imageFile = new File(imageFolder, imageId + ".jpg");
  25.         try {
  26.             FileOutputStream output = new FileOutputStream(imageFile);
  27.             try {
  28.                 output.write(imageData);
  29.             } finally {
  30.                 output.close();
  31.             }
  32.         } catch (IOException e) {
  33.             throw new RuntimeException(e);
  34.         }
  35.     }
  36. }

ImageService.java

  1. package com.ttdev.album;
  2. import java.io.*;
  3. import java.util.*;
  4. import javax.servlet.ServletContext;
  5. import javax.servlet.http.*;
  6. import org.apache.hivemind.*;
  7. import org.apache.tapestry.*;
  8. import org.apache.tapestry.engine.*;
  9. import org.apache.tapestry.services.*;
  10. public class ImageService implements IEngineService {
  11.     
  12.     private HttpServletResponse response;
  13.     private LinkFactory linkFactory;
  14.     private Infrastructure infrastructure;
  15.     private ServletContext servletContext;
  16.     
  17.     public void setServletContext(ServletContext servletContext) {
  18.         this.servletContext = servletContext;
  19.     }
  20.     public void setInfrastructure(Infrastructure infrastructure) {
  21.         this.infrastructure = infrastructure;
  22.     }
  23.     public void setResponse(HttpServletResponse response) {
  24.         this.response = response;
  25.     }
  26.     
  27.     public void setLinkFactory(LinkFactory linkFactory) {
  28.         this.linkFactory = linkFactory;
  29.     }
  30.     
  31.     public String getName() {
  32.         return "image";
  33.     }
  34.     
  35.     public void service(IRequestCycle cycle) throws IOException {
  36.         int imageId =  Integer.parseInt(cycle.getParameter("imageId"));
  37.         byte imageData[] = ImageDB.loadImage(imageId, 
  38.                 servletContext.getRealPath(infrastructure.getApplicationPropertySource().getPropertyValue("com.ttdev.album.image-folder")));
  39.         response.setHeader("Content-disposition""attachment; filename=foo.jpg");
  40.         response.setContentType("image/jpeg");
  41.         response.setContentLength(imageData.length);
  42.         try {
  43.             OutputStream out = response.getOutputStream();
  44.             out.write(imageData);
  45.         } catch (IOException e) {
  46.             throw new ApplicationRuntimeException(e);
  47.         }
  48.     }
  49.     
  50.     public ILink getLink(boolean post, Object parameter) {
  51.         String imageId = (String)((Object[])parameter)[0];
  52.         Map<String, String> parameters = new HashMap<String, String>();
  53.         parameters.put("imageId", imageId);
  54.         return linkFactory.constructLink(this, post, parameters, false);
  55.     }
  56. }

ImageServiceEncoder.java

  1. package com.ttdev.album;
  2. import org.apache.tapestry.engine.*;
  3. public class ImageServiceEncoder implements ServiceEncoder {
  4.     public void encode(ServiceEncoding encoding) {
  5.         String service = encoding.getParameterValue("service");
  6.         if (!service.equals("image")) {
  7.             return;
  8.         }
  9.         String imageId = encoding.getParameterValue("imageId");
  10.         encoding.setServletPath("/images/" + imageId + ".jpg");
  11.         encoding.setParameterValue("service"null);
  12.         encoding.setParameterValue("imageId"null);
  13.     }
  14.     public void decode(ServiceEncoding encoding) {
  15.         String path = encoding.getServletPath();
  16.         if (!path.equals("/images")) {
  17.             return;
  18.         }
  19.         String filename = encoding.getPathInfo().substring(1);
  20.         String imageId = filename.substring(0, filename.length() - 4);
  21.         encoding.setServletPath("/app");
  22.         encoding.setParameterValue("service""image");
  23.         encoding.setParameterValue("imageId", imageId);
  24.     }
  25. }

Upload.java

  1. package com.ttdev.album;
  2. import java.io.*;
  3. import javax.servlet.ServletContext;
  4. import org.apache.tapestry.annotations.*;
  5. import org.apache.tapestry.html.*;
  6. import org.apache.tapestry.request.*;
  7. public abstract class Upload extends BasePage {
  8.     
  9.     public abstract IUploadFile getFile();
  10.     
  11.     @InjectMeta("com.ttdev.album.image-folder")
  12.     public abstract String getImageFolder();
  13.     
  14.     @InjectObject("service:tapestry.globals.ServletContext"
  15.     public abstract ServletContext getServletContext();
  16.     public void onOk() {
  17.         if (getFile().getFileName().length()==0) {
  18.             return;
  19.         }
  20.         byte imageData[] = new byte[(int) getFile().getSize()];
  21.         InputStream fileInput = getFile().getStream();
  22.         try {
  23.             fileInput.read(imageData);
  24.         } catch (IOException e) {
  25.             throw new RuntimeException(e);
  26.         }
  27.         ImageDB.saveImage(101, imageData, getServletContext().getRealPath(getImageFolder()));
  28.         
  29.         getRequestCycle().activate("Home");
  30.     }
  31. }

hivemodule.xml

  1. <?xml version="1.0"?>
  2. <module id="com.ttdev.album" version="1.0.0">
  3.     <service-point id="ImageService" interface="org.apache.tapestry.engine.IEngineService">
  4.         <invoke-factory>
  5.             <construct class="com.ttdev.album.ImageService">
  6.                 <set-object property="linkFactory" value="service:tapestry.url.LinkFactory"/>
  7.                 <set-object property="servletContext" value="service:tapestry.globals.ServletContext"/>
  8.             </construct>
  9.         </invoke-factory>
  10.     </service-point>
  11.     <contribution configuration-id="tapestry.services.ApplicationServices">
  12.         <service name="image" object="service:com.ttdev.album.ImageService"/>
  13.     </contribution>
  14.     <contribution configuration-id="tapestry.url.ServiceEncoders">
  15.         <page-service-encoder id="page" extension="html" service="page"/>
  16.         <encoder id="image" object="instance:com.ttdev.album.ImageServiceEncoder"/>
  17.     </contribution>
  18. </module>

Album.application

  1. <?xml version="1.0"?>
  2. <!DOCTYPE application PUBLIC
  3.   "-//Apache Software Foundation//Tapestry Specification 4.0//EN"
  4.   "http://jakarta.apache.org/tapestry/dtd/Tapestry_4_0.dtd">
  5. <application name="Album">
  6.     <meta key="com.ttdev.album.image-folder" value="/images"/>
  7. </application>

Home.html

  1. <html>
  2. <a href="" jwcid="download">Download photo #101</a>   
  3. <img jwcid="@Any" src="ognl:getImageUrl(101)"/>
  4. <form jwcid="form">
  5.     <input type="Submit" value="OK"/>
  6. </form>
  7. </html>

Home.page

  1. <?xml version="1.0"?>
  2. <!DOCTYPE page-specification PUBLIC
  3.   "-//Apache Software Foundation//Tapestry Specification 4.0//EN"
  4.   "http://jakarta.apache.org/tapestry/dtd/Tapestry_4_0.dtd">
  5. <page-specification class="com.ttdev.album.Home">
  6.     <component id="download" type="ServiceLink">
  7.         <binding name="service" value="literal:image"/>
  8.         <binding name="parameters" value="{'101'}"/>
  9.     </component>
  10.     <component id="form" type="Form">
  11.         <binding name="listener" value="listener:onOk"/>
  12.     </component>
  13. </page-specification>

Upload.html

  1. <html>
  2. <form jwcid="uploadForm">
  3.     <input type="file" jwcid="upload"/><p>
  4.     <input type="submit" value="OK"/>
  5. </form>
  6. </html>

Upload.page

  1. <?xml version="1.0"?>
  2. <!DOCTYPE page-specification PUBLIC
  3.   "-//Apache Software Foundation//Tapestry Specification 4.0//EN"
  4.   "http://jakarta.apache.org/tapestry/dtd/Tapestry_4_0.dtd">
  5. <page-specification class="com.ttdev.album.Upload">
  6.     <component id="uploadForm" type="Form">
  7.         <binding name="listener" value="listener:onOk"/>
  8.     </component>
  9.     <component id="upload" type="Upload">
  10.         <binding name="file" value="file"/>
  11.     </component>
  12. </page-specification>

web.xml

  1. <?xml version="1.0"?>
  2. <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
  3.     xmlns:xsi="http://www.w3.org/TR/xmlschema-1/"
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
  5.     <display-name>Album</display-name>
  6.     <servlet>
  7.         <servlet-name>Album</servlet-name>
  8.         <servlet-class>org.apache.tapestry.ApplicationServlet</servlet-class>
  9.         <load-on-startup>1</load-on-startup>
  10.     </servlet>
  11.     <servlet-mapping>
  12.         <servlet-name>Album</servlet-name>
  13.         <url-pattern>/app</url-pattern>
  14.     </servlet-mapping>
  15.     <servlet-mapping>
  16.         <servlet-name>Album</servlet-name>
  17.         <url-pattern>*.html</url-pattern>
  18.     </servlet-mapping>
  19.     <servlet-mapping>
  20.         <servlet-name>Album</servlet-name>
  21.         <url-pattern>/images/*</url-pattern>
  22.     </servlet-mapping>
  23. </web-app>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值