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:

Advertisements

Netbeans plugin for Carmesí

I have made a plugin for integration of Carmesí in Netbeans 7.0.

This plugin register a library for Carmesí 1.0 so you can easy add it to your project. Library includes sources and javadocs.

Plugin also adds templates for controllers, converters and JSON serializers.

As part of the integration, Carmesí is registered as a web framework (includes a configuration panel). When you select Carmesí framework, its library is automatically added to your project.

Additionally, helpset is also available in the plugin.

This plugin was tested in Netbeans 7.0 RC1 and RC2 and is working smoothly. I’m waiting for the final release to check that everything works as expected.

You can download the release candidate version of the plugin here.  The source for the plugin is in http://code.google.com/p/carmesi-netbeans-plugin/.

Carmesí 1.0

The first stable version of Carmesí is already available. Carmesí is a web framework based on MVC pattern. I was thinking of a very succinct framework that doesn’t need configuration files,  where the API was minimal and was something like a simple layer above of Servlets. Because I don’t have problems working with HTML, a MVC framework was OK for me.

With this loose idea in mind I started a cycle of design-implementation-testing of the concepts. Near in the beginning, I wondered if it was worth what I was doing, if any of the several web frameworks in Java is already doing something like I wanted to do.  I thought that Spring MVC or Struts could be candidates. As I’ve always fix myself with Servlets, JSP, HTML and javascript, never worked with any of this frameworks so I made a review of both. Struts is a top level abstraction so was very different of my idea. Then when I moved to Spring, I see some similar things. Was there any bonus on doing my own framework? Not an easy or objective question. So I started to look into the differences and was the initial idea that gave me the answer: I didn’t want an extra xml file, extra configuration, I wanted  an simple API.

So, after some months of testing the usability of the API and making sure all the unit test were passing, today I announce the availibity of Carmesí 1.0 .

Carmesí is based in annotations for a declarative approach, Servlet 3.0 for automatic and dynamic registration of the components, and an annotation proccesor for automatic generation of a file (it’s a list of annotated classes).

This is an example of the features:

@URL("/doService")
@ForwardTo("/result.jsp")
public class POJOController {
  @Inject
  private A a;

  @PostConstruct
  public void init(){
    //do init
  }

  @RequestAttribute("value") //this attribute will contain the return value
  public int add(@RequestParameter("a") int a, @RequestParameter("b") int b){
    //do logic
    //return the result
  }

}

The code above is using request parameter injection (and conversion), CDI, url to controller binding, forward to the view result.jsp, and is telling to Carmesí that the result will be in an attribute of the request.

The page of the project is in http://code.google.com/p/carmesi/. The distribution file is here, you just need to include the jar file to start using it (more info the project page). You can find documentation in the Carmesi Guide.

I hope you can give it a try.

TLD Generator

I like JSP. I know many people won’t agree and maybe they have their reasons but I feel confortable with this tecnology. Of course, I always use MVC pattern and JSP are just the view part.

I was developing a custom tag library and when I had to write the tag library descriptor (TLD) I noticed that many of the information on it it’s already contained in the tag handlers classes (method signatures) and I could provide the missing information with some meta data in the code. So I made a little annotation-based tool for generating the TLD file. I created some annotations to express the info of the tag and its attributes. An annotation processor reads this meta data and generates the TLD in a few time. If I have to add or change a tag or an attribute name, now I save some time editing the TLD file (sometimes forgetting edit the TLD file or introducing some errors).

These are examples of the annotated tags:


@Tag
public class ExampleTag extends SimpleTagSupport{
    private String a;

    @TagAttribute(isRequired=true)
    public void setA(String a) {
        this.a = a;
    }

    @Override
    public void doTag() throws JspException, IOException {
        getJspContext().getOut().print("<h1>"+a+"</h1>");
    }

}

There is support for functions too.

public class ExampleFunction {

    @Function
    public static int add(int a, int b){
        return a+b;
    }

}

I made available the tool here. In this page, you will find more details about how to use it.

I hope you find it useful.