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:
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:
Name it GETIP:
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;
}
}
The screenshot without error is as follows:
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:
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.