Recently in my project we got one problem that we are getting wrong barcodes. We use to call one API which used to give us a byte array which is a barcode. We are using ice faces and we were rendering the byte array as bellow.
<ice:graphicImage value="#{bytearray}" height="32px" width="150px" rendered="true">
Initially we thought it is the barcode API, which is giving the wrong barcode. I spent some time to find the root cause and tried creating the barcode image file in each layer of the application. Even I found the correct barcode in my MBean, but the moment it goes to the UI it came wrong. That is how we conclude that there is no problem with the barcode API, rather there is a problem with image flushing in JSF. Next task as to find a alternative solution and we came up with a solution to use a servlet to get the byte array. And we used the url to call the servlet from the graphicImage object. The demo solution is bellow.
JSF:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<jsp:root version="1.2" xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:ice="http://www.icesoft.com/icefaces/component">
<jsp:directive.page contentType="text/html;charset=ISO-8859-1"
pageEncoding="ISO-8859-1" />
<f:view>
<ice:graphicImage id="autogenbookmark2"
url="${testBean.imgUrl}"
/>
</f:view>
</jsp:root>
MBean:
public class TestBean {
private static int i =0;
public String imgUrl;
public TestBean() {
imgUrl = "/barcode?i=" + i + "&p=" + System.currentTimeMillis();
i++;
}
public String getImgUrl() {
return imgUrl;
}
public void setImgUrl(String imgUrl) {
this.imgUrl = imgUrl;
}
}
Servlet:
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class TestServlet extends HttpServlet {
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
@SuppressWarnings("deprecation")
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("img/gif");
System.out.println(request.getParameter("i"));
ServletOutputStream os = response.getOutputStream();
FileInputStream fis = null;
int i = Integer.parseInt(request.getParameter("i"));
if(i % 2 == 0) {
fis = new FileInputStream("d://1.gif");
}
else {
fis = new FileInputStream("d://2.gif");
}
byte b[] = new byte[fis.available()];
response.setContentLength(fis.available());
fis.read(b);
os.write(b);
os.flush();
os.close();
}
}
0 comments:
Post a Comment