- Clojure - Libraries
- Clojure - Automated Testing
- Clojure - Applications
- Clojure - Concurrent Programming
- Clojure - Java Interface
- Clojure - Databases
- Clojure - Reference Values
- Clojure - Macros
- Clojure - Watchers
- Clojure - Agents
- Clojure - StructMaps
- Clojure - Metadata
- Clojure - Atoms
- Clojure - Date & Time
- Clojure - Destructuring
- Clojure - Predicates
- Clojure - Regular Expressions
- Clojure - Sequences
- Clojure - Exception Handling
- Clojure - Namespaces
- Clojure - Maps
- Clojure - Vectors
- Clojure - Sets
- Clojure - Lists
- Clojure - Strings
- Clojure - File I/O
- Clojure - Recursion
- Clojure - Numbers
- Clojure - Functions
- Clojure - Decision Making
- Clojure - Loops
- Clojure - Operators
- Clojure - Variables
- Clojure - Data Types
- Clojure - REPL
- Clojure - Basic Syntax
- Clojure - Environment
- Clojure - Overview
- Clojure - Home
Clojure Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Clojure - Apppcations
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 | See-saw is a pbrary which can be used for creating desktop apppcations. |
2 | 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 | 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 | Buttons can be displayed with the help of the button class. |
5 | Labels can be displayed with the help of the label class. |
6 | 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
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.
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.
Advertisements