Process of custom function development

  • Last update:June 04, 2021
  • I. Description

    Scenario description: FineReport has provided a large number of built-in functions, which are sufficient to meet the user's report production needs under normal circumstances, but in some special fields, some special functions may be required. In this case, FineReport provides custom functions mechanism, users can define some functions by themselves according to business needs.

    FineReport function definition rules: Functionname(Para,Para,...), where Functionname is the function name and Para is the parameter.

    II. Idea

    In FineReport, each function is defined as a class. This class must implement the Function interface. When performing operations, first obtain this class through function name reflection, and then call its run(Object[] agrs) method.

    III. Steps

    1. Write function code

    For complex functions, complete Java code can be formed after passing the test in Java development tools (IDEA, Eclipse, etc.)


    2. Compile custom function

    (1) Compile into a class file through Java development tools (IDEA, Eclipse, etc.)

    Open Eclipse and create a new project:

    1.png

    2.png

    Right-click the project>Properties, add FineReport JAR, please refer to Compile Java Program for details.

    Create a new class file in the src directory:

    3.png

    Name it GETIP:

    4.png

    Copy the code below and load the reference step by step according to the prompts until no error is reported

    package com.fr.function;
    import com.fr.script.AbstractFunction;
    import java.net.InetAddress;
    public class GETIP extends AbstractFunction {
        @Override
        public Object run(Object[] objects) {
            try {
                InetAddress ia = InetAddress.getLocalHost();
                return ia.getHostAddress();
            } catch (Exception e) {
                return e.getMessage();
            }
        }
        public static InetAddress getInetAddress() {
            return null;
        }
    }
    Show Code

    The screenshot without error is as follows:

    5.png

    After saving the Java file, there will be a corresponding class file in the working directory %FR_HOME%\webapps\webroot\WEB-INF\classes\com\fr\function folder, that is, the compilation is successful.

    (2) Compile through the designer (this method requires that the javac compile under the local cmd normally does not report an error)

    Click Server>Function Manager, add a custom function name StringCat, click Edit, and enter the code:

    6.png

    After confirming that it is correct, click Compile, and it prompts Successfully Compiled!, indicating that the compilation has been successful, and then click Save.

    The StringCat.class file will be generated in the %FR_HOME%\webapps\webroot\WEB-INF\classes\com\fr\function .

     

    3. Use custom function

    After registering the custom function, it can be used directly when making reports. The usage method is the same as that of the built-in function.

    For details, please refer to Custom Functions.

    Attachment List


    Theme: Secondary Development
    • Helpful
    • Not helpful
    • Only read

    滑鼠選中內容,快速回饋問題

    滑鼠選中存在疑惑的內容,即可快速回饋問題,我們將會跟進處理。

    不再提示

    9s后關閉

    Get
    Help
    Online Support
    Professional technical support is provided to quickly help you solve problems.
    Online support is available from 9:00-12:00 and 13:30-17:30 on weekdays.
    Page Feedback
    You can provide suggestions and feedback for the current web page.
    Pre-Sales Consultation
    Business Consultation
    Business: international@fanruan.com
    Support: support@fanruan.com
    Page Feedback
    *Problem Type
    Cannot be empty
    Problem Description
    0/1000
    Cannot be empty

    Submitted successfully

    Network busy