Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

java - How to send and display QRCode image from action class to JSP in Struts 2

I am submitting a JSP form with a string and on submit I am calling a Struts 2 action. In that action I am creating a QRCode image using QRGen library as below

File QRImg=QRCode.from("submitted String").to(ImageType.PNG).withSize(100, 100).file();

my JSP form:

<form action="createQR">
Enter Your Name<input type="text" name="userName"/><br/>
<input type="submit" value="Create QRCode"/>
</form>

My action Mapping struts.xml:

<action name="createQR" class="CreateQRAction">
 <result name="success">displayQR.jsp</result>
</action>

My Action class:

import java.io.File;
import net.glxn.qrgen.QRCode;
import net.glxn.qrgen.image.ImageType;  
import com.opensymphony.xwork2.ActionSupport;
public class CreateQRAction extends ActionSupport{
 private File QRImg;
 Private String userName;

 public String execute() {
   QRImg=QRCode.from(userName).to(ImageType.PNG).withSize(100, 100).file();
return SUCCESS;
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}
public File getQRImg() {
    return QRImg;
}

public void setQRImg(File QRImg) {
    this.QRImg = QRImg;
}
}

Now if result is success that I want to display this image on my JSP.

<s:property value="QRImg"/>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It seems you need the action the <s:url from that you could substitute in the <img tag as href attribute to retrieve the image similar to use static images like in the /images folder.

Let's call it ImageAction. This is a simple action that writes to the response out. To use it you need to put the file with image into the session. Because images are retrieved by the separate threads. In the execute method write

@Action(value = "image",  interceptorRefs = @InterceptorRef("basicStack"))
public class ImageAction extends ActionSupport {

public String execute() {

Get the file from the session

File file = session.get("file");

then you need to read file

FileInputStream fis = new FileInputStream(file);
byte[] data = new byte[fis.available()];
fis.read(data);
fis.close();

then write to the response out

response.setContentType("image/png");

BufferedImage bi;
OutputStream os = response.getOutputStream();
bi = ImageIO.read(new ByteArrayInputStream(data));
ImageIO.write(bi, "PNG", os);
os.flush();

and return NONE result because this action only writes to the response

return NONE;
}

done

then in the JSP forwarded from your action just use <img src="<s:url action="image"/>" style="width:100%;"/>. If you need to add path then use namespace annotation on the action and attribute in the url.

I feel you are familiar with the session concept in the Struts2, i.e. how to inject the session into your action and map objects in it. Map the file object in your action before return the result.

Good luck.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...