Hello World with Carmesi

In this post, I’ll show an example of a very minimal web application using Carmesi. It’s just a variation of “Hello, world!” that receives a parameter to concatenate in a greeting message that is displayed in a different page.

First, there have to be a HTML page to capture the parameter. This page contains a form that allows the user to capture his or her name.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form method="GET" action="greetings">
            <p>
                <label for="name">Please, introduce your name</label>
                <input type="text" name="name"/>
            </p>
            <button>Get greeting message</button>
        </form>
    </body>
</html>

The next step (and here is where Carmesi is used), a class to put the logic of the application (as this example is very very basic, the logic is captured in the controller but not to be done in an application with a more complex logic).

We have to specify a few things in the controller:

  • The url to invoke it. The form in the first step uses this url to send the parameter. This information is specified with URL annotation.
  • That the request parameter called name will be injected into a parameter of the method (RequestParameter annotation).
  • That the result will be stored in a request attribute to be displayed by the result page. This information is implicit in the declaration of the controller because the name of the method is prefixed with get (the name of the attribute is got by removing this prefix from the name of the method).  The annotation RequestAttribute could be added to the method to make it explicit.
  • The result page of this controller. This is specified using ForwardTo because we need to access to the request scope (in other cases RedirectTo could be used).
import carmesi.URL;
import carmesi.ForwardTo;
import carmesi.RequestParameter;

@URL("/greetings")
@ForwardTo("result.jsp")
public class GreetingsController {

    public String getGreetingMessage(@RequestParameter("name") String name){
        return "Hello, "+name+"!";
    }

}

And finally, the application needs a result page to display the message. The value is in the request attribute greetingMessage.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>${greetingMessage}</h1>
    </body>
</html>

Here is an screenshot of the execution of this example:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s