3

I am a Java developer that is very new to Kotlin. I love the language though, and I like how easily web applications can be done with it. The problem is that I cannot figure out how to run Kotlin HTML builder files in the browser, so that I can create a basic web page skeleton in kotlin. I can output it in the IDE, but it is silly how hard it seems to be to get it to run in my browser. This may be a dumb question and I'm missing something very obvious, but I can't seem to find the answer online.

Keep in mind that I'm not using the Intelli-J IDE. Would love to, but can't afford to pay out the nose just to do web development in Kotlin. Been using Eclipse.

Thanks in advance.

Patrick S.
  • 275
  • 1
  • 5
  • 19
  • The Kotlin HTML builder, AFAIK, is just an example of how builders can be used with Kotlin to create a DSL. I wouldn't advise it to use it in any real use-case. Use your preferred web framework (Spring-MVC, plain servlets/JSPs, whatever), and just write your classes in Kotlin rather than Java. – JB Nizet Apr 10 '16 at 15:19
  • That explains a lot. Haha. Thanks for the help. – Patrick S. Apr 10 '16 at 15:23
  • What is the problem with InteliJ Community Edition? It is free and OOS https://www.jetbrains.com/idea/download/ – voddan Apr 10 '16 at 18:10
  • @JBNizet `kotlinx.html` is a well working solution, and arguably better than JSP templates – voddan Apr 10 '16 at 18:23

3 Answers3

3

When you use Kotlin html builders kotlinx.html or any other of that sort, you need to, well, build them in order to get HTML for the browser.

There are no such thing as "Kotlin builder files". Those constructs are plain Kotlin code, so you write them inside your (server?) codebase, compile them and then invoke them to generate HTML responses. This also means you need a (normal Java) router framework, like Spark for example.

To sum up, html-builders are a way to generate HTML strings, so they do not include a way to ship the HTML elsewhere.

voddan
  • 31,956
  • 8
  • 77
  • 87
1

Kotlinx itself doesn't have any utilities to send the result to the user's browser. It's just a regular Kotlin code which can create HTML string. You need a way to send it to the user. There are some.

The simplest one is plain old Java servlets. Anybody still using them?

@WebServlet(urlPatterns = arrayOf("/servlet"), loadOnStartup = 1)
class KotlinxHtmlServlet : HttpServlet() {
    override fun doGet(request: HttpServletRequest?, response: HttpServletResponse?) {
        response!!.setContentType("text/html")
        response!!.writer.appendHTML(true).html {
            head {
                title = "Hello from kotlinx.html + Servlets"
            }
            body {
                h1 { +"Kotlin is awesome" }
                p {
                    +"Read more about "
                    a("http://kotlinlang.org") {
                        target = ATarget.blank
                        +"it"
                    }
                }
            }
        }
    }
}

Spring Boot is very popular today. However, this @Controller will work in vanilla Spring too:

@Controller
class KotlinxHtmlController {
    @ResponseBody
    @RequestMapping(path = arrayOf("controller"), method = arrayOf(RequestMethod.GET))
    fun doGet(): String {
        return createHTML(true).html {
            head {
                title = "Hello from kotlinx.html + Servlets"
            }
            body {
                h1 { +"Kotlin is awesome" }
                p {
                    +"Read more about "
                    a("http://kotlinlang.org") {
                        target = ATarget.blank
                        +"it"
                    }
                }
            }
        }
    }
}

SparkJava is one of the plenty of young Java micro-frameworks. Note, that in case of SparkJava you can just write routes inside your main:

fun main(args: Array<String>): Unit {
    get("spark", { request: Request, response: Response ->
        createHTML(true).html {
            head {
                title = "Hello from kotlinx.html + Servlets"
            }
            body {
                h1 { +"Kotlin is awesome" }
                p {
                    +"Read more about "
                    a("http://kotlinlang.org") {
                        target = ATarget.blank
                        +"it"
                    }
                }
            }
        }
    })
}

I'm leaving dependency management, running the app and guessing the correct URLs to access generated pages to you. All of the above examples will result in this HTML:

<html>
  <head title="Hello from kotlinx.html + Servlets"></head>
  <body>
    <h1>Kotlin is awesome</h1>
    <p>Read more about <a href="http://kotlinlang.org" target="_blank">it</a></p>
  </body>
</html>

You can also try Dropwizard or Ninja frameworks.

Also, you can take a look at Kara – a web frameworks especially designed for Kotlin – but it is still in alpha stage.

madhead
  • 31,729
  • 16
  • 153
  • 201
0

I may be missing something here, but if using kotlinx.html javascript version, the resultant js code does perform as a DOM builder ... can add more if this is what is required.

innov8
  • 2,093
  • 2
  • 24
  • 31