How to write our own result type in struts2
Action Class
Result Type Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | package com.lkr.action; import com.lkr.service.UserService; import com.lkr.vo.User; import com.opensymphony.xwork2.ActionSupport; public class GetImageAction extends ActionSupport { private static final long serialVersionUID = 1L; private UserService userService; private Long id; private User user; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } @Override public String execute() throws Exception { System.out.println("this is come from after display image jsp"); user = userService.getUserById(id); return "imageBytesResult"; } public String getContentDisposition() { return ""; } public int getBufferSize() { return 4096; } } |
Result Type Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | package com.lkr.action; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.Result; public class ImageBytesResult implements Result { private static final long serialVersionUID = 1L; @Override public void execute(ActionInvocation invocation) throws Exception { GetImageAction action = (GetImageAction) invocation.getAction(); HttpServletResponse response = ServletActionContext.getResponse(); //response.setContentType(action.getContentType()); //response.setContentLength(action.getContentLength()); response.getOutputStream().write(action.getImageInBytes()); response.getOutputStream().flush(); } } |
Service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | package com.lkr.service; import java.util.List; import com.lkr.dao.UserDAO; import com.lkr.vo.User; public class UserService{ private UserDAO userDao; public UserDAO getUserDao() { return userDao; } public void setUserDao(UserDAO userDao) { this.userDao = userDao; } public User save(User user) throws Exception { return userDao.save(user); } public List<User> getAllUsers() throws Exception { return userDao.getAllUsers(); } public User getUserById(long id) throws Exception { return userDao.getUserById(id); } } |
DAO
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | package com.lkr.dao; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.List; import org.hibernate.Transaction; import com.lkr.vo.User; public class UserDAO { public User save(User user) throws Exception { Transaction tx=getSession().beginTransaction(); getSession().save(user); tx.commit(); return user; } public List<User> getAllUsers() throws Exception { List<User> allUsers = getSession().createQuery("from User").list(); return allUsers; } public User getUserById(long id) throws Exception { User user = (User) getSession().get(User.class, new Long(id)); // Convert BLOB to byte array user.setImageInBytes(getImageInBytes(user)); user.setContentLength((int) user.getImage().length()); return user; } public byte[] getImageInBytes(User user) { if (user.getImage() == null) { return new byte[0]; } ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { InputStream is = user.getImage().getBinaryStream(); byte[] buf = new byte[1024]; int i = 0; while ((i = is.read(buf)) >= 0) { baos.write(buf, 0, i); } is.close(); } catch (Exception ex) { ex.printStackTrace(); } return baos.toByteArray(); } } |
struts.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="false" /> <constant name="struts.custom.i18n.resources" value="global" /> <constant name="struts.objectFactory" value="spring" /> <package name="commonPackage" namespace="/" extends="struts-default"> <result-types> <result-type name="imageBytesResult" class="com.lkr.action.ImageBytesResult" /> </result-types> <action name="addUserData" class="com.lkr.action.UserAction"> <result name="input"> WEB-INF/jsp/imageUploader.jsp </result> </action> <action name="displayFullImage" class="com.lkr.action.FullImageAction"> <result name="input"> WEB-INF/jsp/dispalyFullImage.jsp </result> </action> <action name="getImage" class="com.lkr.action.GetImageAction"> <result name="imageBytesResult" type="imageBytesResult"> </result> </action> </package> </struts> |
If you are directly getting Byte[] from the database then you don't need to convert Blob to Byte[]
Jsp:
-----
<!DOCTYPE HTML>
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta charset="utf-8">
<title>Tracking Gadget - View User</title>
<link href="style.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="css/menu.css" type="text/css"
media="screen" charset="utf-8">
</head>
<body>
<div id="content">
<!-- scroll -->
<s:form method="post" theme="simple" name="myprofile"
id="myprofile">
<fieldset>
<legend>Licence</legend>
<table width="100%" border="0">
<tr>
<td><img alt="" width="150" height="100"
src="getImage!getImage?id=<s:property value="user.id" />"></td>
</tr>
</table>
</fieldset>
</table> <br />
</s:form>
</div>
</body>
</html>
No comments:
Post a Comment