
carmesi
Carmesí
Carmesí is a web framework for making easy the implementation of controller components in web applications using MVC architecture.
The main features of Carmesí are:
- Declarative approach.
- Injection of method parameters.
- JSON support.
- No need for verbose configuration files. A simple file is automatically generated by the annotation proccesor included in Carmesí.
- Support of CDI injection in the controller classes.
Requirements:
- Servlet Container or Application Server supporting Servlet 3.0.
- If you want to use CDI, it must be supported by your container/server.
- An external JSON API, if you want to use JSON.
A simple controller with Carmesí
This controller class generates a random number. The display of the random number is handled by the view page.jsp.
``` @URL(“/randomNumber”) @ForwardTo(“/page.jsp”) public class SimpleController{ private Random random=new Random();
public int getNumber(HttpServletRequest request){
request.setAttribute(“number”, random.nextInt());
}
} ```
Content of page.jsp:
...
<body>
<h1>${number}</h1>
</body>
...
Injecting parameters from request
You can pass request parameters in your method. Conversion of String to business object is supported by Carmesí.
``` @URL("/c") @ForwardTo("/c.jsp") public class ControllerC {
public void doOperation(@RequestParameter("age") int age){
}
} ```
Returning objects as JSON
You can send the JSON representation of the return value to the client.
``` @URL(“/getObject”) public class SomeController{
@ToJSON
public SomeType getObject(){
return someObject;
}
} ```
Setting the return value in a scope
You can annotate your method with RequestAttribute, SessionAttribute or ApplicationAttribute and the return value will be set in the specific context with the specified name.
``` @URL(“/someurl”) @ForwardTo(“/page.jsp”) public class SomeController{
@SessionAttribute(“someAttribute”)
public SomeType getController(){
return someObject;
}
} ```
CDI support
You can inject fields or methods in the controller. PostConstruct and PreDestroy callbacks are also available.
``` @URL("/doService") @ForwardTo("/result.jsp") public class POJOController { @EJB private Bean ejb;
@Inject
private A a;
@PostConstruct
public void init(){
}
@RequestAttribute("value") //this attribute holds the result
public int add(@RequestParameter("a") int a, @RequestParameter("b") int b){
//do logic
//return the result
}
} ```
Netbeans support
A plugin exists for integration of Carmesi in Netbeans. When installing, Carmesi will appear in the list of frameworks for web applications. Plugin also includes templates for controllers, converters and JSON serializers.
More documentation
For more details and examples of the functionality of Carmesí see Carmesi guide and examples page. You can download it here.