官方主页: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
- package com.ttdev.album;
- import org.apache.tapestry.annotations.*;
- import org.apache.tapestry.engine.*;
- import org.apache.tapestry.html.*;
- public abstract class Home extends BasePage {
- @InjectObject("engine-service:image")
- public abstract IEngineService getImageService();
- public String getImageUrl(int imageId) {
- return getImageService().getLink(false, new Object[] { Integer.toString(imageId) }).getURL();
- }
- public abstract String getImageId();
- public void onOk() {
- getRequestCycle().activate("Upload");
- }
- }
ImageDB.java
- package com.ttdev.album;
- import java.io.*;
- public class ImageDB {
- public static byte[] loadImage(int imageId, String imageFolder) {
- try {
- File imageFile = new File(imageFolder, imageId + ".jpg");
- FileInputStream input = new FileInputStream(imageFile);
- ByteArrayOutputStream output = new ByteArrayOutputStream();
- byte buf[] = new byte[1024];
- for (;;) {
- int noBytesRead = input.read(buf);
- if (noBytesRead == -1) {
- return output.toByteArray();
- }
- output.write(buf, 0, noBytesRead);
- }
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- public static void saveImage(int imageId, byte[] imageData, String imageFolder) {
- File imageFile = new File(imageFolder, 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 com.ttdev.album;
- import java.io.*;
- import java.util.*;
- import javax.servlet.ServletContext;
- import javax.servlet.http.*;
- import org.apache.hivemind.*;
- import org.apache.tapestry.*;
- import org.apache.tapestry.engine.*;
- import org.apache.tapestry.services.*;
- public class ImageService implements IEngineService {
- private HttpServletResponse response;
- private LinkFactory linkFactory;
- private Infrastructure infrastructure;
- private ServletContext servletContext;
- public void setServletContext(ServletContext servletContext) {
- this.servletContext = servletContext;
- }
- public void setInfrastructure(Infrastructure infrastructure) {
- this.infrastructure = infrastructure;
- }
- public void setResponse(HttpServletResponse response) {
- this.response = response;
- }
- public void setLinkFactory(LinkFactory linkFactory) {
- this.linkFactory = linkFactory;
- }
- public String getName() {
- return "image";
- }
- public void service(IRequestCycle cycle) throws IOException {
- int imageId = Integer.parseInt(cycle.getParameter("imageId"));
- byte imageData[] = ImageDB.loadImage(imageId,
- servletContext.getRealPath(infrastructure.getApplicationPropertySource().getPropertyValue("com.ttdev.album.image-folder")));
- response.setHeader("Content-disposition", "attachment; filename=foo.jpg");
- response.setContentType("image/jpeg");
- response.setContentLength(imageData.length);
- try {
- OutputStream out = response.getOutputStream();
- out.write(imageData);
- } catch (IOException e) {
- throw new ApplicationRuntimeException(e);
- }
- }
- public ILink getLink(boolean post, Object parameter) {
- String imageId = (String)((Object[])parameter)[0];
- Map<String, String> parameters = new HashMap<String, String>();
- parameters.put("imageId", imageId);
- return linkFactory.constructLink(this, post, parameters, false);
- }
- }
ImageServiceEncoder.java
- package com.ttdev.album;
- import org.apache.tapestry.engine.*;
- public class ImageServiceEncoder implements ServiceEncoder {
- public void encode(ServiceEncoding encoding) {
- String service = encoding.getParameterValue("service");
- if (!service.equals("image")) {
- return;
- }
- String imageId = encoding.getParameterValue("imageId");
- encoding.setServletPath("/images/" + imageId + ".jpg");
- encoding.setParameterValue("service", null);
- encoding.setParameterValue("imageId", null);
- }
- public void decode(ServiceEncoding encoding) {
- String path = encoding.getServletPath();
- if (!path.equals("/images")) {
- return;
- }
- String filename = encoding.getPathInfo().substring(1);
- String imageId = filename.substring(0, filename.length() - 4);
- encoding.setServletPath("/app");
- encoding.setParameterValue("service", "image");
- encoding.setParameterValue("imageId", imageId);
- }
- }
Upload.java
- package com.ttdev.album;
- import java.io.*;
- import javax.servlet.ServletContext;
- import org.apache.tapestry.annotations.*;
- import org.apache.tapestry.html.*;
- import org.apache.tapestry.request.*;
- public abstract class Upload extends BasePage {
- public abstract IUploadFile getFile();
- @InjectMeta("com.ttdev.album.image-folder")
- public abstract String getImageFolder();
- @InjectObject("service:tapestry.globals.ServletContext")
- public abstract ServletContext getServletContext();
- public void onOk() {
- 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, getServletContext().getRealPath(getImageFolder()));
- getRequestCycle().activate("Home");
- }
- }
hivemodule.xml
- <?xml version="1.0"?>
- <module id="com.ttdev.album" version="1.0.0">
- <service-point id="ImageService" interface="org.apache.tapestry.engine.IEngineService">
- <invoke-factory>
- <construct class="com.ttdev.album.ImageService">
- <set-object property="linkFactory" value="service:tapestry.url.LinkFactory"/>
- <set-object property="servletContext" value="service:tapestry.globals.ServletContext"/>
- </construct>
- </invoke-factory>
- </service-point>
- <contribution configuration-id="tapestry.services.ApplicationServices">
- <service name="image" object="service:com.ttdev.album.ImageService"/>
- </contribution>
- <contribution configuration-id="tapestry.url.ServiceEncoders">
- <page-service-encoder id="page" extension="html" service="page"/>
- <encoder id="image" object="instance:com.ttdev.album.ImageServiceEncoder"/>
- </contribution>
- </module>
Album.application
- <?xml version="1.0"?>
- <!DOCTYPE application PUBLIC
- "-//Apache Software Foundation//Tapestry Specification 4.0//EN"
- "http://jakarta.apache.org/tapestry/dtd/Tapestry_4_0.dtd">
- <application name="Album">
- <meta key="com.ttdev.album.image-folder" value="/images"/>
- </application>
Home.html
- <html>
- <a href="" jwcid="download">Download photo #101</a>
- <img jwcid="@Any" src="ognl:getImageUrl(101)"/>
- <form jwcid="form">
- <input type="Submit" value="OK"/>
- </form>
- </html>
Home.page
- <?xml version="1.0"?>
- <!DOCTYPE page-specification PUBLIC
- "-//Apache Software Foundation//Tapestry Specification 4.0//EN"
- "http://jakarta.apache.org/tapestry/dtd/Tapestry_4_0.dtd">
- <page-specification class="com.ttdev.album.Home">
- <component id="download" type="ServiceLink">
- <binding name="service" value="literal:image"/>
- <binding name="parameters" value="{'101'}"/>
- </component>
- <component id="form" type="Form">
- <binding name="listener" value="listener: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"?>
- <!DOCTYPE page-specification PUBLIC
- "-//Apache Software Foundation//Tapestry Specification 4.0//EN"
- "http://jakarta.apache.org/tapestry/dtd/Tapestry_4_0.dtd">
- <page-specification class="com.ttdev.album.Upload">
- <component id="uploadForm" type="Form">
- <binding name="listener" value="listener:onOk"/>
- </component>
- <component id="upload" type="Upload">
- <binding name="file" value="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>
- <servlet-mapping>
- <servlet-name>Album</servlet-name>
- <url-pattern>*.html</url-pattern>
- </servlet-mapping>
- <servlet-mapping>
- <servlet-name>Album</servlet-name>
- <url-pattern>/images/*</url-pattern>
- </servlet-mapping>
- </web-app>