title | category | tags |
---|---|---|
Extend Your Application with Custom Java |
Logic & Business Rules |
Most application logic can be developed using microflows. Microflows are very powerful and contain a lot of the features that you need in every application. To prevent you from getting stuck due to a missing feature, Mendix microflows are extendable. So, whenever you feel something is missing, you can add it yourself with the use of Java actions.
This how-to will teach you how to do the following:
- Extend your application with custom Java code
Before starting this how-to, make sure you have completed the following prerequisites:
- Read How to Create and Deploy Your First App
- You need Eclipse: download it here
{{% alert type="info" %}}
You can use any text editor to create custom Java actions, but we highly recommend using Eclipse. The Modeler contains a Deploy for Eclipse feature verifying that everything that needs to be configured in Eclipse is done automatically. All you have to do is import the project into your Eclipse working environment.
{{% /alert %}}
-
Right-click the MyFirstModule module and select Add > Resources > Java action:
-
Name the new Java action ReverseCustomerName and click OK:
-
Click Add to add a parameter:
-
On the Parameter window, do the following:
- Name the parameter inputCustomer
- Select Object as type
- Click Select... and select MyFirstModule.Customer as the object type Click OK
-
Change the return type of the Java action to String and click OK to save the Java action:
-
Select Project > Deploy for Eclipse in the Modeler:
To edit the Java action in Eclipse, follow these steps:
-
Open Eclipse and right-click somewhere in the Package Explorer.
-
Select Import... from the menu:
-
In the Import window, select Existing Projects into Workspace and click Next:
-
Set the project directory as the root directory for this project and click Finish:
If you don't know what the project directory is, select Project > Show Project Directory in Explorer in the Modeler:
-
Double-click ReverseCustomerName.java in the Package Explorer of Eclipse:
In the Java code, there is a placeholder marked with
//BEGIN USER CODE
and//END USER CODE
comment statements. This is where you can add your own Java code. The modeler will never overwrite the code between those two statements.As you can see, the Modeler generated a variable for the input customer object name
inputCustomer
. You can use that variable to get the name of the customer and reverse it like this:String customerName = this.inputCustomer.getName(this.getContext()); return new StringBuilder(customerName).reverse().toString();
-
Insert the above code between the
//BEGIN USER CODE
and//END USER CODE
comment statements. It should look like this: -
Save the Java action in Eclipse:
-
Open the Modeler and locate the Customer_Overview page.
-
Add a new Microflow button to the control bar of the data grid and change the caption to Reverse Name:
-
Right-click the new Microflow button and select Select microflow...:
-
Name the new microflow Customer_ReverseName and click OK:
-
Open the new microflow, which should look like this:
-
Drag the ReverseCustomerName Java action from the Project Explorer onto the line between the green start event and red end event. This generates a Java action activity:
-
Double-click the generated activity to open the Call Java Action properties editor, and then double-click the first argument to open the expression editor:
-
Press and hold the Ctrl key and press the spacebar to open the code completion editor.
-
Select $Customer (MyFirstModule.Customer):
-
Click OK to save the expression.
-
In the Call Java Action window, change the output Variable to ReversedName:
-
Click OK to save the properties. The microflow should now look like this:
-
Open the Toolbox from the bottom-right corner of the Modeler:
You can also open the Toolbox from the View menu:
-
Drag a show message activity from the Toolbox to the line between the green start and red end event.
-
Double-click the activity to open the Show Message properties editor.
-
Enter Reversed name: {1} for Template:
-
Click the New button to add a new parameter to open the expression editor.
-
Press and hold the Ctrl button and press the spacebar to open the code completion editor.
-
Select $ReversedName (String), which is the output variable of the Java action:
-
Click OK to save the parameter. The show message activity properties should now look like this:
-
Click OK to save the show message activity. The microflow should now look like this:
-
Click Run to deploy the application to the cloud. The Modeler automatically commits the changes to Team Server for version control.
-
As soon as the deployment process is completed, click View App to open the application in your browser:
-
Open the customers overview, select a row, and click Reverse Name. You should now see a message pop-up window with the reversed customer name.