Name |
Deprecated : see io.cayla.web |
---|---|
Backends | JVM |
Maven coordinates | |
Compatible Ceylon release |
JVM: 1.0.0 (outdated) |
Published | Dec 1, 2013 |
Stats |
Downloads (JVM): 506 Source downloads: 485 |
Description |
The Cayla Web FrameworkCayla makes easy creating web application with Ceylon. Creating a simple application in secondsImport the Cayla modulemodule my.app "1.0.0" { import cayla "0.2.4"; } Write the controllerimport cayla { ... } object controllers { route("/") shared class Index() extends Controller() { shared actual default Response handle() => ok().body("Hello World"); } } shared void run() { value application = Application(controllers); application.run(); } Build and run!> ceylon compile my.app ... > ceylon run my.app/1.0 started ApplicationApplications are create via the [[Application]] objects that takes as argument a Ceylon declaration or an object that is scanned to discover the controllers: Package applicationsApplication(`package my.controllers`) Object applicationsApplication(controllers) Application life cycleApplication can be started with [[Application.run]] or [[Application.start]]:
Application configurationConfiguration is handled by the [[Config]] class. By default the appliation is bound on the 8080 port and on all available interfaces. Explicit config can be provided when the application is created: Application(`package my.controllers`, Config{port = 80;}); ControllersControllers must extend the [[Controller]] interface, they can declare parameters which shall declares the
Query parametersroute("/greeter") shared clas Greeter(String name) extends Controller() { shared actual default Response handle() => ok().body("Hello ``name``"); } Path parametersroute("/greeter/:name") shared clas Greeter(String name) extends Controller() { shared actual default Response handle() => ok().body("Hello ``name``"); } Path parameters can match
Controller URLControllers can be addressed using the redefined [[Controller.string]] method. In a controller the expression
RoutingRouting is declared with the route("/") shared class Index() extends Controller() { ... } Objects wrapping controllers can optionally be annotated with the route("/products") object products { route("/") shared class Index() extends Controller() { ... } route("/cars") shared class Cars() extends Controller() { ... } } Controller logicDuring an invocation Cayla dispatches the request to the [[Controller.handle]] method. This method should implement the controller behavior. It is also possible to override the [[Controller.invoke]] method instead that provides access to the [[RequestContext]] class that exposes Cayla objects. Both methods returns a [[Response]] object that is sent to the client via Vert.x ResponsesThe [[Response]] class is the base response, the [[Status]] class extends the response class to define the http status and the [[Body]] class extends the [[Status]] with a response body. Creating responses is usually done via the fluent API and the top level functions such as return notFound().body("Not found!!!!"); Http methodsBy default a controller is bound to a route for all Http methods. This can be restricted by using annotations like
get route("/greeter") shared clas Greeter(String name) extends Controller() { shared actual default Response handle() => ok().body("Hello ``name``"); } TemplatingCayla provides an extensible template engine build on top of Ceylon language constructs. HTML { BODY { H1 { "Hello World" } } } ElementsThe engine provides a set of out of the box elements such as HTML, BODY, DIV, etc… Elements can have attributes and content. A { href="http://localhost:8080/home"; "Home" } String can also be produced with A { href= ()=>url; ()=>name } The AliasesThe template engine defines two important alias
Those aliases are used by the engine declarations and it is best to keep them in mind when using the engine. TagsThe engine provides a set of useful tags for creating dynamic templates Each tagThe each tag provides iteration over Ceylon iterables, design for working with Ceylon comprehensions. UL { each({for (i in 0..3) LI { "The item ``i`` } }) } When tagThe when tag provides conditional dispatch: DIV { when(() => x). eval { to = 0; "zero" }. eval { to = 1; "one" }. otherwise { "too large" }. } Usage patternsSince the engine is based on Ceylon syntax, there are a few useful constructs naturally possible for using the engine in a web application. Parameterized templateTemplate index(String title) => HTML { HEAD { TITLE { title } } }; class Controller1(shared String param) extends Controller() { shared actual default Response handle() => index(param).ok(); } Encapsulate and reuseTemplate fragment can be encapsulated DIV controlGroup(Child* children) => DIV { className = "control-group"; DIV { className= "controls"; children = children; } }; and can then be reused easily value form = FORM { className = "form-signin"; action = "http://localhost:8080/"; controlGroup(INPUT { type = "text"; name = "name"; }), controlGroup(BUTTON { className = "btn btn-default"; type = "submit"; "Say Hello" }) } Mix with controller URLsControllers can be used for creating safe urls (i.e generated by Cayla), such syntax can be used with lazy evaluation: A { href= ()=>Product("cars"); "Cars" } ExtensibleThe engine is extensible, you can easily extends the engine with your own elements and tags. |
Dependencies |
ceylon.net/1.0.0
shared
java.base/7 JDK
shared
vietj.vertx/0.3.4
shared
|
Usage |
import cayla "0.2.4"; |
Module links |
Members Imported By Home Code repository Issue tracker Browse Download .car No .js archive Download source archive Download module documentation View API documentation |