当前位置:首页 > 教育培训 >

后端输出html代码前端怎么获取(前端如何获取线上代码)

来源:原点资讯(www.yd166.com)时间:2023-07-09 01:46:44作者:YD166手机阅读>>

有时候我们会遇到这样的一个需求:

通过前端传入的数据渲染一个现成的打印模板出来,最后返回一个html格式的文本给前端,模板是有一个现成的,但是每次传入进来的数据是不同的,所以需要后端经过渲染出来返回渲染好的html内容给前端,这个时候我们就可以用thymeleaf来实现这个功能。 我们先建造一个模板:

html复制代码<div id="print_main_full_box"> <style> #print_main_full_box { width: 100%; } #print_main_full_box > * { margin: 0; padding: 0; } #print_main_full_box > table, td, th { margin: 0; padding: 0; border: 1px solid black; border-collapse: collapse } .yiban { width: 49%; text-align: left; display: inline-block; /*border-left: 1px solid black;*/ } .jiachu { font-weight:bold; } td { font-size: 14px; } .center { text-align: center; } </style> <table width="100%"> <tbody> <tr class="jiachu"> <td colspan="8" class="center" style="font-size: 16px"> <div th:text = ${company}>公司</div> <div th:text = ${title}>出货单</div> </td> </tr> <tr> <td colspan="8" class="center"> <span th:text = ${address}>地址</span> </td> </tr> <tr> <td colspan="8"> <div class="yiban"> <span class="jiachu">客户名称:<span th:text = ${cursumerName}>客户名称</span></span> </div> <div class="yiban"> <span class="jiachu">订单号:</span><span th:text = ${orderNo}>订单号</span> </div> </td> </tr> <tr> <td colspan="8"> <div class="yiban"> <span class="jiachu">送货地址:<span th:text = ${deliveryAddr}>送货地址</span></span> </div> <div class="yiban"> <span class="jiachu">送货日期:</span><span th:text = ${actualDeliveryDate}>送货日期</span> </div> </td> </tr> <tr> <td colspan="8"> <div class="yiban"> <span class="jiachu">*<span th:text = ${phone}>联系电话</span></span> </div> <div class="yiban"> <span class="jiachu">送货单号:</span><span th:text = ${invoiceNo}>送货单号</span> </div> </td> </tr> <tr class="center"> <td colspan="8" class="jiachu"> <div>机器名称:<span th:text = ${machineName}></span></div> </td> </tr> <tr class="jiachu"> <td width="5%" align="center">序号</td> <td width="5%" align="center">内部序号</td> <td width="25%" align="center">图号</td> <td width="5%" align="center">单位</td> <td width="5%" align="center">数量</td> <td width="5%" align="center">单价</td> <td width="5%" align="center">总价</td> <td width="10%" align="center">备注</td> </tr> <tr th:each = "prod : ${prods}" data-line="5"> <td align="center"><div data-item-index style="overflow: hidden;max-height: 40px;line-height: 20px;">序号</div></td> <td align="center"><div th:text = ${prod.selfNumber} style="overflow: hidden;max-height: 40px;line-height: 20px;">内部序号</div></td> <td align="center"><div th:text = ${prod.chartNo} style="overflow: hidden;max-height: 40px;line-height: 20px;">图号</div></td> <td align="center"><div th:text = ${prod.company} style="overflow: hidden;max-height: 40px;line-height: 20px;">单位</div></td> <td align="center"><div th:text = ${prod.invoiceNumber} style="overflow: hidden;max-height: 40px;line-height: 20px;">数量</div></td> <td align="center"><div th:text = ${prod.unitPrice} style="overflow: hidden;max-height: 40px;line-height: 20px;">单价</div></td> <td align="center"><div th:text = ${prod.totalPrice} style="overflow: hidden;max-height: 40px;line-height: 20px;">总价</div></td> <td align="center"><div th:text = ${prod.remarks} style="overflow: hidden;max-height: 40px;line-height: 20px;">哦呵呵</div></td> </tr> <tr> <td colspan="4" align="right">总计:</td> <td align="right"><span th:text = ${invoiceNumber}>3</span></td> <td align="right"></td> <td align="right"><span th:text = ${totalPrice}>3</span></td> <td align="right"></td> </tr> <tr> <td colspan="4">送货单位签章:</td> <td colspan="4">收货单位签章:</td> </tr> </tbody> </table> </div>

这个里面是一个含有thymeleaf语法的模板,现在我们要通过传入的参数不同渲染出不一样的数据出来。 例如前端传入这样的数据:

bash复制代码{ "company":"csdner", "title":"出货单", "address":"中国", "cursumerName":"客户", "orderNo":"2432523234234234", "deliveryAddr":"工业园", "actualDeliveryDate":"20210526", "phone":"18888888888", "invoiceNo":"1567894", "machineName":"25661615", "prods":[ { "selfNumber":"5555", "chartNo":"6666", "company":"csdner", "invoiceNumber":"2222", "unitPrice":"55", "totalPrice":"555", "remarks":"哈哈哈哈" } ], "invoiceNumber":"22", "totalPrice":"102" }

后端要渲染出渲染好之后的html文档给前端:

html复制代码<div id="print_main_full_box"> <style> #print_main_full_box { width: 100%; } #print_main_full_box > * { margin: 0; padding: 0; } #print_main_full_box > table, td, th { margin: 0; padding: 0; border: 1px solid black; border-collapse: collapse } .yiban { width: 49%; text-align: left; display: inline-block; /*border-left: 1px solid black;*/ } .jiachu { font-weight:bold; } td { font-size: 14px; } .center { text-align: center; } </style> <table width="100%"> <tbody> <tr class="jiachu"> <td colspan="8" class="center" style="font-size: 16px"> <div>csdner</div> <div>出货单</div> </td> </tr> <tr> <td colspan="8" class="center"> <span>中国</span> </td> </tr> <tr> <td colspan="8"> <div class="yiban"> <span class="jiachu">客户名称:<span>客户</span></span> </div> <div class="yiban"> <span class="jiachu">订单号:</span><span>2432523234234234</span> </div> </td> </tr> <tr> <td colspan="8"> <div class="yiban"> <span class="jiachu">送货地址:<span>工业园</span></span> </div> <div class="yiban"> <span class="jiachu">送货日期:</span><span>20210526</span> </div> </td> </tr> <tr> <td colspan="8"> <div class="yiban"> <span class="jiachu">*<span>18888888888</span></span> </div> <div class="yiban"> <span class="jiachu">送货单号:</span><span>1567894</span> </div> </td> </tr> <tr class="center"> <td colspan="8" class="jiachu"> <div>机器名称:<span>25661615</span></div> </td> </tr> <tr class="jiachu"> <td width="5%" align="center">序号</td> <td width="5%" align="center">内部序号</td> <td width="25%" align="center">图号</td> <td width="5%" align="center">单位</td> <td width="5%" align="center">数量</td> <td width="5%" align="center">单价</td> <td width="5%" align="center">总价</td> <td width="10%" align="center">备注</td> </tr> <tr data-line="5"> <td align="center"><div data-item-index style="overflow: hidden;max-height: 40px;line-height: 20px;">序号</div></td> <td align="center"><div style="overflow: hidden;max-height: 40px;line-height: 20px;">5555</div></td> <td align="center"><div style="overflow: hidden;max-height: 40px;line-height: 20px;">6666</div></td> <td align="center"><div style="overflow: hidden;max-height: 40px;line-height: 20px;">csdner</div></td> <td align="center"><div style="overflow: hidden;max-height: 40px;line-height: 20px;">2222</div></td> <td align="center"><div style="overflow: hidden;max-height: 40px;line-height: 20px;">55</div></td> <td align="center"><div style="overflow: hidden;max-height: 40px;line-height: 20px;">555</div></td> <td align="center"><div style="overflow: hidden;max-height: 40px;line-height: 20px;">哈哈哈哈</div></td> </tr> <tr> <td colspan="4" align="right">总计:</td> <td align="right"><span>22</span></td> <td align="right"></td> <td align="right"><span>102</span></td> <td align="right"></td> </tr> <tr> <td colspan="4">送货单位签章:</td> <td colspan="4">收货单位签章:</td> </tr> </tbody> </table> </div>

好了,需求说完,开始实战:

第一步,我们是需要一个thymeleaf的模板,让在项目中引入一个工具类:

添加依赖:

xml复制代码 <!-- Thymeleaf 模板引擎 --> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf</artifactId> <version>3.0.9.RELEASE</version> </dependency> <dependency> <groupId>ognl</groupId> <artifactId>ognl</artifactId> <version>3.1.12</version> </dependency>

这是一个 Maven 项目的 dependencies 配置,用于声明项目所依赖的库。

该项目依赖了两个库:thymeleaf 和 ognl。

  • thymeleaf 是基于 java 的模板引擎,在 Web 应用中经常用来将数据和 HTML 页面进行绑定。该库提供了丰富的标签和表达式语法,可以轻松地实现数据绑定和页面渲染。
  • ognl(Object-Graph Navigation Language)是一个面向对象的表达式语言,它可以通过对象属性的名称、方法调用和 Java 表达式来访问对象的属性。在模板引擎中,ognl 主要用于获取模板中需要使用的数据,以及执行一些动态操作。

以上两个库都是 Java 语言编写的,并且在当前代码中被用作 Thymeleaf 模板引擎的依赖库。当编译或运行程序时,Maven 将自动下载并安装这些库。

第二步,创建一个工具类:

这段代码是一个静态方法,其目的是将传入的模板和参数进行渲染,并返回渲染后的结果字符串。

该方法接收两个参数:template 和 params。其中,template 是一个字符串类型的参数,表示要使用哪个模板进行渲染。params 是一个 Map 类型的参数,表示要传递给模板的参数值。

在方法内部,首先创建了一个 Context 对象,然后将传入的参数设置到该对象中。Context 是 Thymeleaf 模板引擎中的一个上下文对象,它提供了模板所需的所有数据。

最后,通过 templateEngine.process() 方法,将模板和上下文对象进行渲染,得到最终的渲染结果,并将其作为方法返回值返回。

java复制代码public class HTMLTemplateUtils { private final static TemplateEngine templateEngine = new TemplateEngine(); /** * 使用 Thymeleaf 渲染 HTML * @param template HTML模板 * @param params 参数 * @return 渲染后的HTML */ public static String render(String template, Map<String, Object> params){ Context context = new Context(); context.setVariables(params); return templateEngine.process(template, context); } } 第三步:传入参数:

这段代码调用了一个名为 HTMLTemplateUtils.render() 的方法,传入了两个参数:content 和 map。

其中,content 是一个字符串类型的参数,表示模板的内容;map 是一个 Map<String, Object> 类型的参数,表示模板中所需的数据。

该方法返回一个字符串类型的结果,经过渲染后的模板内容将存储在该字符串中,可以根据需要进行输出或其他操作。

java复制代码 String output = HTMLTemplateUtils.render(content, map);

content为模板,map为前端传入的json数据

第四步,返回output,这个时候output就是我们已经渲染好的模板了

希望这篇文章能够帮助您了解如何在Spring Boot应用程序中集成Thymeleaf模板引擎,以便使用HTML/CSS/JavaScript文件动态生成Web文档。在本文中,我们简要介绍了如何在Maven项目中配置Thymeleaf,以及如何在控制器类中使用Thymeleaf来渲染HTML模板。通过这些步骤,您可以轻松地将Thymeleaf集成到您的Spring Boot应用程序,并开始使用模板引擎来映射数据并生成动态内容。


作者:掉头发的王富贵
链接:https://juejin.cn/post/7243451555931602981

栏目热文

html发送数据给后端(html数据传到后端)

html发送数据给后端(html数据传到后端)

在电商网站搭建过程中,前端经常会向后端请求数据,有时候通过HTML、JS和PHP文件的处理来实现数据的连通。通常情况下,...

2023-07-09 02:07:00查看全文 >>

html代码中怎么获取后端传递参数

html代码中怎么获取后端传递参数

提到axaj很多人总说很难,什么回调函数呀等等就让人心烦,其实懂得ajax在js里面是如何实现向服务器请求数据的原理,那...

2023-07-09 02:10:51查看全文 >>

html获取实时数据(html如何获取另一个html数据)

html获取实时数据(html如何获取另一个html数据)

在本Pandas教程中,我们将详细介绍如何使用Pandas read_HTML方法从HTML中获取数据。首先,在最简单的...

2023-07-09 01:57:34查看全文 >>

怎么获取本地html页面的数据(html获取后台实时数据)

怎么获取本地html页面的数据(html获取后台实时数据)

不要使用sed、awk、grep等工具进行尝试(这会导致不可预期的结果)。在许多情况下,你最好选择使用支持XML数据的编...

2023-07-09 01:24:09查看全文 >>

html获取后台数据库(html调用数据库数据最简单方法)

html获取后台数据库(html调用数据库数据最简单方法)

大家常见的网站数据转移(数据迁移)通常是一种同样的cms之间。但是,如果网站换了CMS,后台数据库类型和数据表结构都不一...

2023-07-09 01:59:53查看全文 >>

html页面怎么获取后端数据(html初始数据怎么从后台获取)

html页面怎么获取后端数据(html初始数据怎么从后台获取)

本文内容已假定您已拥有一些 html 以及爬虫的基础知识需求背景这几天在做一款资源站产品,开发过程中需要一定的测试数据,...

2023-07-09 01:42:41查看全文 >>

怎么在html显示从后台获取的数据(html查询后台数据)

怎么在html显示从后台获取的数据(html查询后台数据)

html简单教程简单登录界面以下是一个简单的html登录页面的代码,你可以根据自己的需要进行修改:<!DOCTYP...

2023-07-09 01:48:03查看全文 >>

html怎么抓取页面数据(html怎么提取数据)

html怎么抓取页面数据(html怎么提取数据)

在日常的前端开发中,我们经常需要从网页中获取特定的数据,这就需要用到JavaScript来抓取网页内容。今天我就来分享一...

2023-07-09 01:46:05查看全文 >>

html怎么获取表数据(html 获取数据库数据填充到表单)

html怎么获取表数据(html 获取数据库数据填充到表单)

#php js jquery功能片段#唯一需求:表格id。注意:因提交数据不能包含\n,所以要替换。呈现时可以替换回换行...

2023-07-09 01:30:11查看全文 >>

html怎么与后端交互(html怎么与后端交互的)

html怎么与后端交互(html怎么与后端交互的)

前言在 html 文档中 <input type="file"> 标签每出现一次,一个 Fi...

2023-07-09 01:30:56查看全文 >>

文档排行