javascript 如何从 Apache Velocity 的 .vm 文件中获取整个 HTML?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17958467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 10:14:10  来源:igfitidea点击:

How to get whole HTML from a .vm file in Apache Velocity?

javascripthtmlvelocity

提问by ASingh

I would appreciate your help on my use case. I have a Servlet which renders some information using javascript in a Apache Velocity template (.vm) file.

感谢您对我的用例的帮助。我有一个 Servlet,它在 Apache Velocity 模板 (.vm) 文件中使用 javascript 呈现一些信息。

Now, before I return this template to the browser, I want to store the entire HTML into my local file system for which I need to access the whole HTML from the .vm template. I am stuck at doing the last step.

现在,在我将此模板返回到浏览器之前,我想将整个 HTML 存储到我需要从 .vm 模板访问整个 HTML 的本地文件系统中。我坚持做最后一步。

回答by Sergiu Dumitriu

Web applications are client?serverapplications, meaning that there is a clear separation between the client, which is your browser, and the web server. There is no direct connection between the server and the HTML that you see in your browser.

Web 应用程序是客户端?服务器应用程序,这意味着客户端(即您的浏览器)和 Web 服务器之间有明确的分离。服务器和您在浏览器中看到的 HTML 之间没有直接连接。

Try to visualize the process:

尝试将过程可视化:

  1. The user tries to open a web page, so the browser sends a HTTP request to the server.
  2. The server processes the URL that was requested and identifies that it should go to the servlet that processes velocity templates, and identifies the .vmfile that should be used to render the response.
  3. The .vmfile is read by the servlet on the server and rendered into a string representation of the HTML.
  4. The HTML is sent to the client in the HTTP response. From now on, the server has no connection to that HTML.
  5. The browser reads the HTML from the response, parses it, and displays it.
  6. The JavaScript resources associated with that HTML are also fetched from the server, parsed and executed (in the client browser).
  1. 用户尝试打开网页,因此浏览器向服务器发送 HTTP 请求。
  2. 服务器处理请求的 URL 并确定它应该转到处理速度模板的 servlet,并确定.vm应该用于呈现响应的文件。
  3. .vm文件由服务器上的 servlet 读取并呈现为 HTML 的字符串表示形式。
  4. HTML 在 HTTP 响应中发送到客户端。从现在开始,服务器不再连接到该 HTML。
  5. 浏览器从响应中读取 HTML,解析它并显示它。
  6. 与该 HTML 关联的 JavaScript 资源也从服务器获取、解析和执行(在客户端浏览器中)。

There is no way for the Velocity template (or any other code on the server) to access the HTML that is now in the browser, unless the browser explicitly sends it back to the server in another request.

Velocity 模板(或服务器上的任何其他代码)无法访问现在在浏览器中的 HTML,除非浏览器在另一个请求中明确将其发送回服务器。

What you can do is:

你可以做的是:

  1. Write another piece of JavaScript code that listens to the clickevent.
  2. The JS gets the serialized HTML from your target element, something like var html = document.getElementById('id_of_the_element').innerHTML;
  3. The JS sends this string to the server using an XMLHttpRequest, either using the raw XHR support from the browser, or a JS framework of your choice.
  4. On the server you write another servlet (or extend the functionality of an existing servlet) that receives this HTML and processes it as you want.
  1. 编写另一段侦听click事件的 JavaScript 代码。
  2. JS 从您的目标元素中获取序列化的 HTML,例如 var html = document.getElementById('id_of_the_element').innerHTML;
  3. JS 使用 将这个字符串发送到服务器XMLHttpRequest,或者使用来自浏览器的原始 XHR 支持,或者使用您选择的 JS 框架。
  4. 在服务器上,您编写另一个 servlet(或扩展现有 servlet 的功能)来接收此 HTML 并根据需要对其进行处理。