Struts 2ダイナミックイメージの例
このチュートリアルでは、カスタム結果タイプを使用してStruts 2で動的イメージを生成する方法を示します。 例えば、
imageId
の値は、サーバーパスのイメージ名です。 上記の場合、ImageAction
クラスはimageIdパラメーターに基づいて画像ファイルを出力します。
このチュートリアルで使用されるツールとStruts:
-
Struts 2.3.1.2
-
Eclipse 3.7
-
メーベン3
1. ディレクトリ構造
このチュートリアルのディレクトリ構造。 イメージファイルのパスを確認します。
2. アクションクラス
File : DisplayAction.java –通常のアクションで、何もしません。
package com.example.image.action; import com.opensymphony.xwork2.ActionSupport; public class DisplayAction extends ActionSupport { }
File : ImageAction.java –コアアクションクラス。指定されたimageId
パラメータに基づいて画像を取得し、バイト配列に変換します。
package com.example.image.action; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.interceptor.ServletRequestAware; import com.opensymphony.xwork2.ActionSupport; public class ImageAction extends ActionSupport implements ServletRequestAware { byte[] imageInByte = null; String imageId; private HttpServletRequest servletRequest; public String getImageId() { return imageId; } public void setImageId(String imageId) { this.imageId = imageId; } public ImageAction() { System.out.println("ImageAction"); } public String execute() { return SUCCESS; } public byte[] getCustomImageInBytes() { System.out.println("imageId" + imageId); BufferedImage originalImage; try { originalImage = ImageIO.read(getImageFile(this.imageId)); // convert BufferedImage to byte array ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(originalImage, "jpg", baos); baos.flush(); imageInByte = baos.toByteArray(); baos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return imageInByte; } private File getImageFile(String imageId) { String filePath = servletRequest.getSession().getServletContext().getRealPath("/"); File file = new File(filePath + "/Image/", imageId); System.out.println(file.toString()); return file; } public String getCustomContentType() { return "image/jpeg"; } public String getCustomContentDisposition() { return "anyname.jpg"; } @Override public void setServletRequest(HttpServletRequest request) { this.servletRequest = request; } }
3. カスタム結果タイプ
カスタム結果タイプ。画像をブラウザに出力します。
ファイル:CustomImageBytesResult.java
package com.example.image.result; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.Result; import com.example.image.action.ImageAction; public class CustomImageBytesResult implements Result { public void execute(ActionInvocation invocation) throws Exception { ImageAction action = (ImageAction) invocation.getAction(); HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType(action.getCustomContentType()); response.getOutputStream().write(action.getCustomImageInBytes()); response.getOutputStream().flush(); } }
4. Struts.xml
すべてが一緒にリンクされている方法を参照してください。
ファイル:struts.xml
pages/display.jsp
5. JSPページ
ImageAction
を介して動的画像を表示するJSPページ。
ファイル:display.jsp
<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%>Struts 2 Dynamic Image Example
ソースコードをダウンロード
ダウンロード–Struts2-Dynamic-Image-Example.zip(26kb)