简体   繁体   English

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

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

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. 现在,在我将此模板返回到浏览器之前,我想将整个HTML存储到我需要从.vm模板访问整个HTML的本地文件系统中。 I am stuck at doing the last step. 我坚持做最后一步。

Web applications are client⟷server applications, meaning that there is a clear separation between the client, which is your browser, and the web server. Web应用程序是client⟷server应用程序,这意味着客户端(即您的浏览器)与Web服务器之间存在明显的分离。 There is no direct connection between the server and the HTML that you see in your browser. 您在浏览器中看到的服务器和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. 用户尝试打开网页,因此浏览器会向服务器发送HTTP请求。
  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 .vm file that should be used to render the response. 服务器处理所请求的URL,并确定它应该转到处理速度模板的servlet,并标识应该用于呈现响应的.vm文件。
  3. The .vm file is read by the servlet on the server and rendered into a string representation of the HTML. .vm文件由服务器上的servlet读取,并呈现为HTML的字符串表示形式。
  4. The HTML is sent to the client in the HTTP response. HTML将在HTTP响应中发送到客户端。 From now on, the server has no connection to that HTML. 从现在开始,服务器与该HTML没有任何关联。
  5. The browser reads the HTML from the response, parses it, and displays it. 浏览器从响应中读取HTML,解析它并显示它。
  6. The JavaScript resources associated with that HTML are also fetched from the server, parsed and executed (in the client browser). 与该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 click event. 编写另一段侦听click事件的JavaScript代码。
  2. The JS gets the serialized HTML from your target element, something like var html = document.getElementById('id_of_the_element').innerHTML; JS从目标元素中获取序列化的HTML,类似于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. JS使用XMLHttpRequest将此字符串发送到服务器,使用浏览器的原始XHR支持或您选择的JS框架。
  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. 在服务器上,您编写另一个servlet(或扩展现有servlet的功能),该servlet接收此HTML并根据需要对其进行处理。

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

相关问题 你如何从apache velocity * .vm文件中调用javascript函数? - How do you invoke javascript function from an apache velocity *.vm file? 如何通过Java脚本将参数从另一个vm文件(速度)传递到vm文件(速度)? - How to pass parameters to a vm file (Velocity) from another vm file (velocity) through java script? 如何从Dijit编辑器中获取整个HTML值 - How to get the whole HTML value from Dijit Editor 如何从 Apify Cheerio 爬虫获取整个 html? - How do I get whole html from Apify Cheerio crawler? 有没有办法将apache速度模板文件与任何HTML模板引擎(例如mustache或handlebar.js)一起使用 - Is there way to use apache velocity template file with any HTML templating engine like mustache or handlebar.js 将JavaScript代码存储在Velocity VM模板文件中并包含在页面中 - Store javascript codes in velocity vm template file and included in the page 如何修复整个HTML的获取请求和响应 - How to fix get request and response with whole html 如何在 AngularJS 中获取整个 HTML 页面? - How to get a whole HTML page in AngularJS? 如何将整个html文件嵌入JS - how to embed the whole html file into JS 如何将整个HTML文件导入iframe? - How to import a whole different HTML file into an iframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM