Over a million developers have joined DZone.

Polyglot Everywhere – Part 2

· Java Zone

Navigate the Maze of the End-User Experience and pick up this APM Essential guide, brought to you in partnership with CA Technologies

Last week, we set up a new project using the YAML flavor of Polyglot Maven. Now is time for some server-side code!

As a long time Vaadin advocate, let’s create a very simple Vaadin application. This will have the added advantage to let us hack something on the client-side as well for the last part of this serie. As we are fully polyglot, we will avoid the old Java language and use something very cool instead. As I’ve have been to some conferences with its number 1 advocate, I settled on the Kotlin language.

Disclaimer: I’m far from a Kotlin user – and this is not about Kotlin anyway, so please pardon my mistakes in the following.

Java is still Maven’s first class citizen so the first step is to add some configuration for the Kotlin compiler to kick in. It’s quite easy, especially given the previous work on the polyglot POM:

build:
    sourceDirectory: ${project.basedir}/src/main/kotlin
    plugins:
        -   groupId: org.jetbrains.kotlin
            artifactId: kotlin-maven-plugin
            version: 0.11.91.1
            executions:
            -   id: compile
                phase: compile
                goals:
                    - compile
        # Other plugins go there

The next step is to configure the web application. It can be done either with the old XML web deployment descriptor or since Servlet 3.0 with annotations. Since XML is far from polyglot, let’s use the Kotlin way. Besides, Kotlin annotations are cooler than in Java:

WebServlet (
    name = "VaadinServlet",
    urlPatterns = array("/*"),
    initParams = array(
        WebInitParam(
            name = "UI",
            value = "ch.frankel.blog.polyglot.MainUi"
        )
    )
)
VaadinServletConfiguration(
    productionMode = false,
    ui = javaClass<MainUi>()
)
class KotlinServlet: VaadinServlet()

Regular Vaadin users know about the final step. A UI needs to be created. Again, this is quite straightforward:

class MainUi: UI() {
    override fun init(request: VaadinRequest) {
        val label = Label("Hello from Polyglot Everywhere")
        val layout = VerticalLayout(label)
        layout.setMargin(true)
        layout.setSpacing(true)
        setContent(layout)
    }
}

And with these steps, we’ve achieved a polyglot webapp!
The next article in this serie will add a client-side component for Vaadin. Don’t miss it!


Thrive in the application economy with an APM model that is strategic. Be E.P.I.C. with CA APM.  Brought to you in partnership with CA Technologies.

Topics:

Published at DZone with permission of Nicolas Frankel, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}