0

I have no experience with jsp and I am trying to help my colleague in an old struts2 project.

We receive from the backend some information about the characteristics of a product. One of this information is the image in a base64 format . . And I want to set it in the <img src...>

If I set it in a div I can see the image info (the base64 string) printed on the browser.

<div id="<s:property value="%{#attribute.id}"/>" > </div>

But if I try to do something similar for the <img element, the source element is not filled properly. I have this:

<img src="<s:property value="%{#attribute.id}"/>"  transform='<s:property value="%{#attribute.transformFunction}" />' />

The transform operation is to add 'data:'to the beginning of the base64 string (don't know why it doesn't come with it initially)

In the console if I inspect both elements I got this 1st

<div id="image" transform="transformBase64ToImage" original="iVBOR...(base64string here)">data:image/gif;base64,iVBOR...(base64string here)</div>

2nd - obviously doesn't load any image

<img src="image" transform="transformBase64ToImage">

How can add the base64 string to the image source property?

saomi
  • 855
  • 5
  • 16
  • 38

1 Answers1

0

Please try below code

  1. Add below code in action class -
private String  product; // add getter & setter
BASE64Encoder base64Encoder = new BASE64Encoder();
StringBuilder imageStr = new StringBuilder();
byte data[] = "Add Byte array here";
imageStr.append("data:image/png;base64,");
imageStr.append(base64Encoder.encode(data));
product = imageString.toString();
  1. Add below tag in jsp File

<img width="100" height="100" src="<s:property value="product" />">

Yogesh
  • 126
  • 9