Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to customize the jar package for javascript through Kettle

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to customize the jar package through Kettle for javascript use, the article is very detailed, has a certain reference value, interested friends must read it!

We all know that Kettle is developed in the Java language, and you can call java class methods directly in JavaScript. So sometimes, we can customize some methods for JavaScript to use.

First, create tool classes in the java project

In a project, create a utils utility class, such as a method to calculate the total page number. The code is as follows:

Public class PaginationUtils {/ * calculate the total page number * @ param totalRecords total number of records * @ param pageSize page size * @ return total page number * / public static int totalPage (String totalRecords,String pageSize) {int totalPage=0; try {BigDecimal records=new BigDecimal (totalRecords); BigDecimal size=new BigDecimal (pageSize); BigDecimal _ tmp=records.add (size) .subtract (new BigDecimal (1)); BigDecimal _ tp=_tmp.pide (size) .setScale (0mBigDecimal.ROUNDdevelopHALFupUP); totalPage=_tp.intValue () } catch (Exception e) {/ / error} return totalPage;}}

II. Deployment and packaging

After the tool class method has been developed, it can be packaged with the mvn clean package-DskipTests command, and a jar file will be generated in the target directory. You need to put this jar package in kettle's lib directory. As shown in the following figure:

3. Write JavaScript script

Restart Kettle and create a new JavaScript script. The js code for calculating the total page number is as follows:

/ / calculate the total page number var totalPage=com.study.spring.Utils.PaginationUtils.totalPage (countBySql,pageSize)

It is actually declaring the java class + method in the js code.

But the problem is that when kettle runs this JavaScript script, it prompts the following error:

Cannot compile _ javascript: org.mozilla.javascript.EcmaError: TypeError: Cannot call property totalPage in object [JavaPackage com.study.spring.Utils.PaginationUtils]. It is not a function, it is "object". (# 22)

According to the error message, we still haven't found the relevant class method, just like we didn't put this jar package.

IV. FAQ

What on earth is the above mistake? I'm weird, too. So I used the compression tool to look at other jar package structures in the lib directory and found that they all look like this:

And the directory of the jar package I just typed looks like this:

The com directory is not under the root path of the jar package, so you naturally cannot access that method.

OK, the cause of the problem has been found, and then we will talk about the solution:

Add to the pom.xml file:

Org.springframework.boot spring-boot-maven-plugin true

The internal structure of the jar package typed again is:

Success! The com directory is already under the root path of the jar package.

Replace the jar package to the lib directory of kettle, restart Kettle, and call the custom jar package via javascripts again successfully!

The above is all the contents of the article "how to customize the jar package for javascript through Kettle". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report