如何在Struts 2中将动作类的QRCode图像发送和显示到JSP [英] How to send and display QRCode image from action class to JSP in Struts 2

查看:141
本文介绍了如何在Struts 2中将动作类的QRCode图像发送和显示到JSP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在提交带有字符串的JSP表单,并且在提交时我正在调用Struts 2操作。在该操作中,我使用QRGen库创建QRCode图像,如下所示

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();

我的JSP表单:

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

我的操作映射 struts.xml

My action Mapping struts.xml:

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

我的行动类:

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;
}
}

现在如果结果成功,我想显示这个我的JSP上的图片。

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

<s:property value="QRImg"/>


推荐答案

看来你需要行动< s:url 你可以在< img 标签中替换 href 属性检索图像类似于使用静态图像,如 / images 文件夹。

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.

让我们打电话它 ImageAction 。这是一个写入响应的简单操作。要使用它,您需要将带有图像的文件放入会话中。因为图像是由单独的线程检索的。在execute方法中写

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() {

从会话中获取文件

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

然后你需要阅读文件

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

然后写回响应

response.setContentType("image/png");

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

并返回NONE结果,因为此操作仅写入响应

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

return NONE;
}

完成

然后在从您的操作转发的JSP中使用< img src =< s:url action =image/>风格= 宽度:100%;/> 。如果你需要添加路径,那么在url中的action和attribute上使用namespace注释。

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.

我觉得你熟悉Struts2中的会话概念,即如何将会话注入您的操作并将对象映射到其中。在返回结果之前映射操作中的文件对象。

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.

祝你好运。

这篇关于如何在Struts 2中将动作类的QRCode图像发送和显示到JSP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆