如何在java代码中获取html中的js返回的结果
-
背景
在开发html转pdf中,需求中有个逻辑需要获取html中js代码,js代码对数据进行处理,并返回给java使用;
-
使用
-
依赖:
<!-- 解析html --> <dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.8.3</version> </dependency>
-
html代码
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <script> function reverse(code, type) { var output = "EP"; return output = output + type + code; } </script> <head> <meta charset="UTF-8"> </meta> <meta http-equiv="X-UA-Compatible" content="IE=edge"> </meta> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </meta> <title>Document</title> <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script> <style> body { font-family: SimSun; } ul { padding: 0; } ul li { list-style-type: none; } div { box-sizing: border-box; } .homeContent { padding: 40px /* 40/28 */ 40px 20px /* 20/28 */ 40px; width: 650px; height: 900px /* 742/28 */ ; margin: 0 auto; } .headerLogo { width: 100%; height: 88px /* 88/28 */ ; overflow: hidden; } .headerLogo .left { width: 248px /* 248/28 */ ; height: 80px /* 80/28 */ ; float: left; } .headerLogo .left img { width: 100%; height: 100%; } .headerLogo .right { width: 378px /* 378/28 */ ; height: 88px /* 88/28 */ ; float: right; } .headerLogo .right img { width: 100%; height: 100%; } .contentTable { padding-top: 20px /* 20/28 */ ; } .information { border: 3px solid #D9D9D9; } .information .infoItem { padding: 15px 25px; border-bottom: 1px solid #d9d9d9; overflow: hidden; } .infoTitle { width: 20%; color: #666666; font-size: 14px; float: left; } .infoTag { width: 80%; color: #000000; font-size: 14px; float: right; } .information .infoItem:last-child { border-bottom: none; } .infoPage { margin-top: 20px; float: right; } </style> </head> <body> <div class="homeContent"> <div class="right"> <img th:src="@{${oneDimCode}}"></img> </div> </div> <div class="contentTable"> <h2 style="text-align: center;" th:text="${erFormDefName}"></h2> <div class="information"> <div class="infoItem"> <div class="infoTitle">单据编号</div> <div class="infoTag" th:text="${erFormCode}"></div> </div> <div class="infoItem"> <div class="infoTitle">单据名称</div> <div class="infoTag" th:text="${djName}"></div> </div> <div class="infoItem"> <div class="infoTitle">公司</div> <div class="infoTag"> <span th:text="${corpName}"></span> ( <span th:text="${custCode}"></span> ) </div> </div> <div class="infoItem"> <div class="infoTitle">经办人</div> <div class="infoTag" th:text="${userName}"></div> </div> <div class="infoItem"> <div class="infoTitle">部门</div> <div class="infoTag" th:text="${userPersonHROrgunitName}"></div> </div> <div class="infoItem"> <div class="infoTitle">金额合计</div> <div class="infoTag" th:text="${totalMoney}"></div> </div> <div class="infoItem"> <div class="infoTitle">创建日期</div> <div class="infoTag" th:text="${erFormDateFormat}"></div> </div> </div> </div> </div> </body> </html>
-
java代码
private String generateCode() { String html = ""; // html串 Document doc = Jsoup.parse(html); Element tds = doc.getElementsByTag("script").get(0); // 标识获取html中第一个<script>标签 String data = tds.toString().replaceAll("\\&[a-zA-Z]{0,9};", "").replaceAll("<[^>]*>", "\n\t"); // 去除html中的标签 String o = null; try { engine.eval(data); Invocable invokeEngine = (Invocable) engine; o = (String)invokeEngine.invokeFunction("reverse", "--code--", "==type=="); //执行的js,以及参数 } catch (ScriptException | NoSuchMethodException e) { e.printStackTrace(); } } return o;
-
测试
public static void main(String[] args) { String html = ""; // 上面的html代码 String c = generateCode(); System.out.println(o); // EP==type==--code-- }
-