Clojure - Applications



Clojure has some contributed libraries which have the enablement for creating Desktop and Web-based applications. Let’s discuss each one of them.

Sr.No. Applications & Description
1 Desktop – See-saw

See-saw is a library which can be used for creating desktop applications.

2 Desktop – Changing the Value of Text

The value of the content in the window can be changed by using the ‘config!’ option. In the following example the config! option is used to change the window content to the new value of “Good Bye”.

3 Desktop – Displaying a Modal Dialog Box

A modal dialog box can be shown by using the alert method of the see-saw class. The method takes the text value, which needs to be shown in the modal dialog box.

4 Desktop – Displaying Buttons

Buttons can be displayed with the help of the button class.

5 Desktop – Displaying Labels

Labels can be displayed with the help of the label class.

6 Desktop – Displaying Text Fields

Text Fields can be displayed with the help of the text class.

Web Applications - Introduction

To create a web application in Clojure you need to use the Ring application library, which is available at the following link https://github.com/ring-clojure/ring

You need to ensure you download the necessary jars from the site and ensure to add it as a dependency for the Clojure application.

The Ring framework provides the following capabilities −

  • Sets things up such that an http request comes into your web application as a regular Clojure HashMap, and likewise makes it so that you can return a response as a HashMap.

  • Provides a specification describing exactly what those request and response maps should look like.

  • Brings along a web server (Jetty) and connects your web application to it.

The Ring framework automatically can start a web server and ensures the Clojure application works on this server. Then one can also use the Compojure framework. This allows one to create routes which is now how most modern web applications are developed.

Creating your first Clojure application − The following example shows how you can create your first web application in Clojure.

(ns my-webapp.handler
   (:require [compojure.core :refer :all]
      [compojure.route :as route]
      [ring.middleware.defaults :refer [wrap-defaults site-defaults]]))
(defroutes app-routes
   (GET "/" [] "Hello World")
   (route/not-found "Not Found"))
(def app
   (wrap-defaults app-routes site-defaults))

Let’s look at the following aspects of the program −

  • The ‘defroutes’ is used to create routes so that request made to the web application to different routes can be directed to different functions in your Clojure application.

  • In the above example, the “/” is known as the default route, so when you browse to the base of your web application, the string “Hello World” will be sent to the web browser.

  • If the user hits any url which cannot be processed by the Clojure application, then it will display the string “Not Found”.

When you run the Clojure application, by default your application will be loaded as localhost:3000, so if you browse to this location, you will receive the following output.

Clojure Application

Web Applications – Adding More Routes to Your Web Application

You can also add more routes to your web application. The following example shows how to achieve this.

(ns my-webapp.handler
   (:require [compojure.core :refer :all]
      [compojure.route :as route]
      [ring.middleware.defaults :refer [wrap-defaults site-defaults]]))
(defroutes app-routes
   (GET "/" [] "Hello World")
   (GET "/Tutorial" [] "This is a tutorial on Clojure")
   (route/not-found "Not Found"))
(def app
   (wrap-defaults app-routes site-defaults))

You can see that adding a route in the application is as easy as just adding another GET function with the url route. (GET "/Tutorial" [] "This is a tutorial on Clojure")

If you browse to the location http://localhost:3000/Tutorial, you will receive the following output.

Localhost
Advertisements