简体   繁体   English

从 JavaScript 函数获取返回 URL 并将其作为参数发送到操作

[英]Get the return URL from a JavaScript function and send it as a parameter to the action

I have a JavaScript file: informacoesImportantes.js and in it I have a function that generates a destination URL to Whatsapp.我有一个 JavaScript 文件: informacoesImportantes.js ,其中我有一个函数可以生成 Whatsapp 的目标 URL。

You need to get the URL and pass it as a parameter to a qrCode.do action, but apparently it's calling the qrCode even before generating the link, because it's always going empty, but when you open the browser console and print the variable, the link is there .您需要获取 URL 并将其作为参数传递给qrCode.do操作,但显然它甚至在生成链接之前就调用了qrCode ,因为它始终为空,但是当您打开浏览器控制台并打印变量时,链接在那里。

I'm trying to send a parameter to Action , but the parameter comes through a JavaScript function.我正在尝试向Action发送一个参数,但该参数来自一个 JavaScript 函数。 I tried several ways with <c:set> or even directly call the :javascript after the parameter directly in the src , but without success.我用<c:set>尝试了几种方法,甚至直接在src的参数之后直接调用:javascript ,但没有成功。

informacoesImportantes.js : informacoesImportantes.js

function gerarUrlWhatsapp(especialidade,data,hora,medico,unidadeMedica,enderecoCompleto) {

    var texto = "Seguem dados de confirma\u00e7\u00e3o do agendamento: "+"\n"+
    " Especialidade: "+especialidade+"\n"+
    " Data: "+data+"\n"+
    " Hora: "+hora+"\n"+
    " M\u00e9dico: "+medico+"\n"+
    " Unidade "+unidadeMedica+"\n"+
    " Endere\u00e7o: "+enderecoCompleto + "\n"+
    " Informa\u00e7\u00f5es importantes:" + "\n";
    
    return "https://wa.me/?text=" + texto;
}

agendamentoDetalhe.jsp : agendamentoDetalhe.jsp :

<div class="col-sm-3 gnm-container-qrcode">
  <script>
      var urlWhats = gerarUrlWhatsapp('${ agendamento.especialidade.dsEspecialidade}','${dateParts[0]}','${dateParts[1]}','${agendamento.nomePrestador}','${agendamento.unidadeMedica.nome}','${agendamento.unidadeMedica.enderecoCompleto}');
  </script>
    <c:set var="qrCodeUrlWhatsapp" value="${urlWhats}" />
  <img src="../qrCode.do?qrCodeUrlWhatsapp=javascript:gerarUrlWhatsapp('${ agendamento.especialidade.dsEspecialidade}','${dateParts[0]}','${dateParts[1]}','${agendamento.nomePrestador}','${agendamento.unidadeMedica.nome}','${agendamento.unidadeMedica.enderecoCompleto}');"/>
    <p>${urlWhats}</p>
</div>

struts.xml : struts.xml

  <action name="qrCode" class="**" method="qrCode">
    <result name="success" type="stream">
      <param name="contentType">image/jpeg</param>
      <param name="inputName">qrCodeImage</param>
      <param name="bufferSize">4096</param>
    </result>
  </action>

AgendamentoAction.java : AgendamentoAction.java :

public String qrCode() {
  qrCodeImage = generateQrCodeImpl.createQrCode(accessToken, qrCodeUrlWhatsapp);
}

You can't call a JavaScript function in the <img src= .您不能在<img src=调用 JavaScript 函数。 But you can modify the value of the src attribute.但是您可以修改src属性的值。 If you add an id attribute to the <img> tag then you can get this element from the document using JavaScript and modify src attribute after calling a custom function.如果向<img>标签添加id属性,则可以使用 JavaScript 从document获取此元素,并在调用自定义函数后修改src属性。

The below script should be in the JSP file, so the EL expressions have being evaluated before the document is rendered.下面的脚本应该在 JSP 文件中,因此在呈现文档之前已经评估了 EL 表达式。

<img id="qrCodeId" src="#">
<script>
   var urlWhats = gerarUrlWhatsapp('${ agendamento.especialidade.dsEspecialidade}','${dateParts[0]}','${dateParts[1]}','${agendamento.nomePrestador}','${agendamento.unidadeMedica.nome}','${agendamento.unidadeMedica.enderecoCompleto}');
   document.getElementById("qrCodeId").src="../qrCode.do?qrCodeUrlWhatsapp="+encodeURIComponent(urlWhats);
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM