自从7.0版本开始,您可以在 OpenXava Studio、Eclipse、IntelliJ IDEA 等使用 Kotlin 开发 OpenXava 项目。
Kotlin 是如今大多数 Android 应用程序使用的语言,它可以在 JVM 或 JS 之上开发。其中一个特性是 Kotlin 能与 Java 完全兼容,这意味着您可以编写这两种代码并一起使用而不会出现任何问题。
在創建一個 OpenXava 項目後(
如何創建),您必須在項目中根文件夾中的 pom.xml 中定義 Kotlin 的版本:
<properties>
<kotlin.version>1.8.0</kotlin.version>
</properties>
在 dependencies 中添加 Kotlin 库:
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies在>
要构建包含 Kotlin 和 Java 代码的项目,请在 build 中添加以下插件:
<build>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/main/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<goals> <goal>test-compile</goal> </goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/test/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<executions>
<!-- Replacing default-compile as it is treated specially by maven -->
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<!-- Replacing default-testCompile as it is treated specially by maven -->
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>java-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>java-test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
在 src/main 中創建 kotlin 文件夾以存儲 Kotlin 代碼,並在同一文件夾中創建 com.yourcompany.invoicing.model 包。 您可能需要使用 Maven Reload Project 以便您可以在 kotlin 文件夹中创建包。
在 model 包中创建一个名为 Customer 的 Kotlin 类(Customer.kt):
package com.yourcompany.invoicing.model
import org.openxava.annotations.Required
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.Id
@Entity
class Customer {
@Id
@Column(length = 6)
var number : Int ?= null
@Required
@Column(length = 50)
var name : String ?= null
}
在您第一次运行项目前,必须 mvn package,往后只需要 mvn compile 即可。然后在 src/main/java 中的 com.yourcompany.invoicing.run 包中找到 invoicing 类,并运行它。接下来在浏览器中输入
http://localhost:8080/invoicing,并使用 admin/admin(用户/密码)登录,就可以看到以上的成果。
要创建一个显示消息的按钮(动作),请在 kotlin文件夾的 com.yourcompany.invoicing.action 包中創建 ShowMessageAction 类(ShowMessageAction.kt):
package com.yourcompany.invoicing.action
import org.openxava.actions.ViewBaseAction
class ShowMessageAction : ViewBaseAction() {
@Throws(Exception::class)
override fun execute() {
addMessage("Hello world!")
}
}
记住,您必须在 src/main/resources/xava 中的 controllers.xml 声明动作:
<controller name ="Customer">
<extends controller ="Typical"/>
<action name ="showMessage" class ="com.yourcompany.invoicing.action.ShowMessageAction" mode="detail"/>
</controller>
具有注解、属性及其它功能的实体:
package com.yourcompany.invoicing.model
import org.openxava.annotations.*
import org.openxava.calculators.CurrentLocalDateCalculator
import org.openxava.calculators.CurrentYearCalculator
import org.openxava.model.Identifiable
import java.time.LocalDate
import javax.persistence.*
@Entity
@View(members="""
year, number, date,
data {
customer;
details;
remarks
}
"""
)
class Invoice : Identifiable() {
@Column(length = 4)
@DefaultValueCalculator(CurrentYearCalculator::class)
var year : Int? = null
@Column(length = 6)
var number : Int? = null
@Required
@DefaultValueCalculator(CurrentLocalDateCalculator::class)
var date: LocalDate? = null
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@ReferenceView("Simple")
var customer: Customer? = null
@ElementCollection
@ListProperties("product.number, product.description, quantity")
var details: Collection<Detail>? = null
@TextArea
var remarks: String? = null
}
如果您决定使用 Kotlin 开发您的 OpenXava 项目,我们建议您使用 IntelliJ IDEA,因为它包含 Kotlin,並提供智能代码补全、代码着色、自动编译等功能。
您可以在 Kotlin 的
文档中找到更多有关的信息以及如何将
Kotlin 集成到 Maven 项目。