Microflows的Java升级版

在写Java以前想一下

在决定用Java解决问题前,其实很多功能在内建的Microflows中都能够解决了,再加上Java有可能会使原本的结构变得更混乱,甚至让之後接手工作的工程师工作上困难,因此,跟自创widget一样,官方不太建议自己用Java写Mendix上的Microflows。

当然,特殊情况下,该写的Java还是要卷起袖子来写,这时要注意, 用Java写出来的activity要符合
1.单一职责原则
2.符合Unix哲学
3.让不同的microflows都能够重复使用

使用Java Action

点选Project explorer > User Manager > Add other... > Java Action,在点选Java Action 後会跳出下列命名的视窗

https://ithelp.ithome.com.tw/upload/images/20201011/201295307icfMOLXji.png

https://ithelp.ithome.com.tw/upload/images/20201011/20129530Y1ebKAn5zZ.png

按OK後会自动开启新的档案,这里可以让我们设定输出的parameters 和设定return type。

https://ithelp.ithome.com.tw/upload/images/20201011/20129530iXlM0jBJrF.png

在设定parameter时,可以设定type和相关的category

有效的type选项有
• Object
• List
• Enumeration
• Boolean
• Date and time
• Decimal
• Integer/Long
• String
• Nothing

转往Java IDE

1.选择 Project > Deploy for Eclipse,之後系统会自动将source code档案存在project资料夹中

2.开启Eclipse後,可以在package explorer 中的 javasource > usermanager.actions 找到档案

开始编写Java

Mendix 会自动生成档案,格式如下:

// This file was generated by Mendix Studio Pro.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
// Special characters, e.g., é, ö, à , etc. are supported in comments.

package usermanager.actions;

import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.webui.CustomJavaAction;

public class Java_action extends CustomJavaAction<java.util.List<IMendixObject>>
{
	public Planner(IContext context)
	{
		super(context);
	}

	@java.lang.Override
	public java.util.List<IMendixObject> executeAction() throws Exception
	{
		// BEGIN USER CODE
		throw new com.mendix.systemwideinterfaces.MendixRuntimeException("Java action was not implemented");
		// END USER CODE
	}

	/**
	 * Returns a string representation of this action
	 */
	@java.lang.Override
	public java.lang.String toString()
	{
		return "Java_action";
	}

	// BEGIN EXTRA CODE
	// BEGIN EXTRA CODE

}

要注意,编写的部分要在 // BEGIN USER CODE// END USER CODE// BEGIN EXTRA CODE// BEGIN EXTRA CODE 之间!

Mendix Java API

  1. 前往 com.mendix.core.Core 资料夹,在这个资料夹中有会使用到的函数(function)

3.要连结Domain model中的实体(entity)时,需使用Mendix设定好的proxies,这可以在javasource > usermanager.proxies 找到,另外,每个实体在IMendixObject中都有自己的constructors、 getters和 setters 可以使用

// This file was generated by Mendix Studio Pro.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
// Special characters, e.g., é, ö, à , etc. are supported in comments.

package usermanager.actions;

import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.webui.CustomJavaAction;
import com.mendix.systemwideinterfaces.core.IMendixObject;

public class Java_action extends CustomJavaAction<java.util.List<IMendixObject>>
{
	private IMendixObject __Parameter;
	private usermanager.proxies.Employee Parameter;
    private IMendixObject __Parameter_2;

	public Planner(IContext context, IMendixObject Parameter, IMendixObject Parameter_2)
	{
		super(context);
		this.__Parameter = Parameter;
		this.__Parameter_2 = Parameter_2;
	}

	@java.lang.Override
	public java.util.List<IMendixObject> executeAction() throws Exception
	{
		this.Parameter = __Parameter == null ? null : usermanager.proxies.Employee.initialize(getContext(), __Parameter);

		// BEGIN USER CODE
		throw new com.mendix.systemwideinterfaces.MendixRuntimeException("Java action was not implemented");
		// END USER CODE
	}

	/**
	 * Returns a string representation of this action
	 */
	@java.lang.Override
	public java.lang.String toString()
	{
		return "Java_action";
	}

	// BEGIN EXTRA CODE
	// END EXTRA CODE
}

这篇文章只有简单介绍Mendix上的Java使用方式,但其实Mendix上的Java有很多不同的使用方法,关於更深入的Java使用方式可以参考官方文件


<<:  [LeetCode30] Day26 - 1106. Parsing A Boolean Expression

>>:  Day26练习java-抛出例外

js:callback中调用类的function

如何实现在callback中加入类的function; 比如一个界面的button,点击之後,需要调...

VSCode 套件推荐系列

身为一名工程师,将文字编辑器打造出具个人风格也是理所当然的。VSCode 是现在主流的文字编辑器,也...

POCO_设定所有Table继承同一个class

因为专案刚好需要用到 所以纪录一下参数在哪边 纪录一下 使用Visual Studio Entity...

[Day02] 程序菜鸟自学C++资料结构 – 简单QA

程序语言百百种,C++的优势在哪? C++是一种使用广泛的电脑程序设计语言,继承C语言数据类型丰富、...

Day 25 - 影像、声音与影片的整合与拆解

能够载入与以动作触发声音 能够使用图片结合物件互动 了解如何解构图片为粒子跟像素 了解如何将影片放...