Create a "Hello World" web application

Let's build a "Hello World" application with Scooter framework.

Project creation

Let's name our application "greeting" and follow the instructions in startup guide to create the project.

> java -jar tools/create.jar greeting
> java -jar tools/server.jar greeting

Now we can browse the site with this url: http://localhost:8080/greeting.

Greeting Home Page

Introducing Scooter framework

This is the default home page for greeting.

The first thing you may have noticed is the welcome message displayed in your current locale language. Scooter supports i18n out-of-box.

Scooter comes with some built-in tools to manage the site.

  • Site info: view application environment information
  • Site manager: manage deployed files and folders (add/view/edit/replace/copy/delete/rename)
  • Browse routes: view all routes supported by this site
  • Browse databases: see what you have in your data store

In this "Hello World" example, we are going to create a controller and our requests will be processed by the default route.

Writing a controller

Now let's create a HelloController. This is easy because a controller is just a POJO. Type the following code and save it under src/greeting/controllers/ directory.

package greeting.controllers;

public class HelloController {
    public String index() {
        return "html=><h1>Hello World from Scooter</h1>";
    }
}

Now we should be able to access it from our browser. Just type controller name hello slash action name index like http://localhost:8080/greeting/hello/index in the browser's address box and hit return, we can immediately see the hello world message screen.

Html Return Page

With Scooter, you don't need to restart the web server after code change; you don't need to compile the code; you don't need to deploy the application; you don't need to fight with any xml configuration files. Scooter automatically compiles your code and loads it.

What we are doing here follows the so called "Convention over Configuration" principle.

  request url: http://localhost:8080/hello/index
  maps to default route: $controller/$action

  By (default naming) convention, we obtain
  ==> controller class: HelloController.java
  ==> action method: index()

Scooter allows return messages of different types. If you change the string "html=>" to "text=>" in HellowController.java, you can see a pure text message with no html rendering on the browser.

Text Return Page

This class can return an xml message too. If you use the following return code in the class, you will see an xml message in the browser.

package greeting.controllers;

public class HelloController {
    public String index() {
        return "xml=><?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
               "<book><title>Java Programming</title><price>$50</price></book>";
    }
}
XML Return Page

Generating a controller

Scooter has capability to generate many code. Now let's generate a welcome controller. The controller is going to have an action called sayit. Type the following in a command console.

    java -jar tools/generate.jar controller welcome sayit
Code Generator Page

As you can see from console screen, Scooter generated a welcome controller and a view for the sayit action. Now let's access the generated sayit action page from this url: http://localhost:8080/greeting/welcome/sayit.

Default View Page

You can see a browser screen with the generated view. The view screen tells us where the generated view file is located.

Customizing a controller and view

The generated controller is also a POJO. It extends a super class which has many convenient helper methods. It contains a dummy sayit method.

Let's implement this action method with the following code:

public class WelcomeController extends ApplicationController {
    /**
     * sayit method
     */
    public String sayit() {
        flash("notice", "Successfully found the message");
        setViewData("content", "Java programming is fun!");
        return null;
    }
}

The flash method will send the next request a notice message. The storeToRequest method simply links a message with a key in servlet's request scope so that it can be retrieved by a view.

In the sayit.jsp view file, we display the message with this code:

    The message we found is <%=request.getAttribute("content")%>

Now after saving code change, we can click the refresh button of the browser and see our changes.

Custom View Page

Final words

Through out the whole demo, we did not restart web server. We did not compile anything nor deploy anything. We simply type the source code and click the refresh button of a browser.

Java programming with Scooter is fun. That's all for this demo. I hope you enjoy Scooter framework's type-and-click development style.

You may take a look at the next demo where we build a web-based blog engine in 15 minutes. Besides the no-compilation-no-deployment feature, you can see the power of validation, ActiveRecord and associations of Scooter framework.