<i id='DHaBu'><tr id='DHaBu'><dt id='DHaBu'><q id='DHaBu'><span id='DHaBu'><b id='DHaBu'><form id='DHaBu'><ins id='DHaBu'></ins><ul id='DHaBu'></ul><sub id='DHaBu'></sub></form><legend id='DHaBu'></legend><bdo id='DHaBu'><pre id='DHaBu'><center id='DHaBu'></center></pre></bdo></b><th id='DHaBu'></th></span></q></dt></tr></i><div id='DHaBu'><tfoot id='DHaBu'></tfoot><dl id='DHaBu'><fieldset id='DHaBu'></fieldset></dl></div>

  • <small id='DHaBu'></small><noframes id='DHaBu'>

    <legend id='DHaBu'><style id='DHaBu'><dir id='DHaBu'><q id='DHaBu'></q></dir></style></legend>

      <tfoot id='DHaBu'></tfoot>

        • <bdo id='DHaBu'></bdo><ul id='DHaBu'></ul>
      1. 如何在 Struts 2 中将 QRCode 图像从动作类发送和显示到 JSP

        时间:2023-09-25
      2. <tfoot id='n9gjF'></tfoot>

          1. <small id='n9gjF'></small><noframes id='n9gjF'>

                <tbody id='n9gjF'></tbody>
                <bdo id='n9gjF'></bdo><ul id='n9gjF'></ul>

                  <legend id='n9gjF'><style id='n9gjF'><dir id='n9gjF'><q id='n9gjF'></q></dir></style></legend>

                  <i id='n9gjF'><tr id='n9gjF'><dt id='n9gjF'><q id='n9gjF'><span id='n9gjF'><b id='n9gjF'><form id='n9gjF'><ins id='n9gjF'></ins><ul id='n9gjF'></ul><sub id='n9gjF'></sub></form><legend id='n9gjF'></legend><bdo id='n9gjF'><pre id='n9gjF'><center id='n9gjF'></center></pre></bdo></b><th id='n9gjF'></th></span></q></dt></tr></i><div id='n9gjF'><tfoot id='n9gjF'></tfoot><dl id='n9gjF'><fieldset id='n9gjF'></fieldset></dl></div>

                  本文介绍了如何在 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:

                  <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 的动作,你可以在 标记为 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"/>"style="width:100%;"/>.如果您需要添加路径,请在 url 中的操作和属性上使用命名空间注释.

                  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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  <tfoot id='wNvY9'></tfoot>
                    <bdo id='wNvY9'></bdo><ul id='wNvY9'></ul>

                    1. <legend id='wNvY9'><style id='wNvY9'><dir id='wNvY9'><q id='wNvY9'></q></dir></style></legend>
                    2. <i id='wNvY9'><tr id='wNvY9'><dt id='wNvY9'><q id='wNvY9'><span id='wNvY9'><b id='wNvY9'><form id='wNvY9'><ins id='wNvY9'></ins><ul id='wNvY9'></ul><sub id='wNvY9'></sub></form><legend id='wNvY9'></legend><bdo id='wNvY9'><pre id='wNvY9'><center id='wNvY9'></center></pre></bdo></b><th id='wNvY9'></th></span></q></dt></tr></i><div id='wNvY9'><tfoot id='wNvY9'></tfoot><dl id='wNvY9'><fieldset id='wNvY9'></fieldset></dl></div>

                        1. <small id='wNvY9'></small><noframes id='wNvY9'>

                            <tbody id='wNvY9'></tbody>