English 中文(简体)
Clojure - Applications
  • 时间:2024-11-03

Clojure - Apppcations


Previous Page Next Page  

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

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

See-saw is a pbrary which can be used for creating desktop apppcations.

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 Apppcations - Introduction

To create a web apppcation in Clojure you need to use the Ring apppcation pbrary, which is available at the following pnk 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 apppcation.

The Ring framework provides the following capabipties −

    Sets things up such that an http request comes into your web apppcation as a regular Clojure HashMap, and pkewise 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 pke.

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

The Ring framework automatically can start a web server and ensures the Clojure apppcation 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 apppcations are developed.

Creating your first Clojure apppcation − The following example shows how you can create your first web apppcation 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 apppcation to different routes can be directed to different functions in your Clojure apppcation.

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

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

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

Clojure Apppcation

Web Apppcations – Adding More Routes to Your Web Apppcation

You can also add more routes to your web apppcation. 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 apppcation 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