Using Mentawai with Codenvy

Create a project using our Factory button:


Mentawai is a full-stack, action-based, MVC open-source Java web framework born in June 2005. Since then It has been loyal to its philosophy which is to be a high-level web development solution without any XML or Annotations, using a centralized programmatic configuration to wire up the web stack. It embraces the KISS principle through high levels of abstraction. If you are not able to do or understand something related to the framework, the framework is the one to blame, not you.

One of the main characteristics of Mentawai is its full-stack approach since its inception, in other words, you won’t be needing any other framework to implement all the diverse features of most web applications. Everything is abstracted and given by the framework out-of-the-box. This is the same approach used by other frameworks such as Ruby on Rails, Play and Riffe.

By favoring a programmatic approach to configuration Mentawai eliminates XML and Annotations programming, very common to the majority of Java frameworks. The framework was the first Java web framework to follow this path back in 2005.

Create a Project

Login to your codenvy workspace and create a New WAR Project say “mentawai”. Project file structure is shown below :

GetImage (9)

Add Dependencies

Specify the below dependencies in pom.xml which are prerequisites. Make sure these jars are included under Maven Dependencies.

GetImage (10)

Pom.xml :

<dependencies>
<dependency>
<groupId>me.soliveirajr</groupId>
<artifactId>mentawai</artifactId>
<version>2.5.4</version>
</dependency>
</dependencies>

Create Action Class

In this example, we create an Action class to greet the User.
Mentawai uses the Input and the Output to get information from the request and send information back as a response.

Code Logic :

package com.codenvy.action;
import org.mentawai.core.BaseAction;

public class GreetAction extends BaseAction {
public String sayHello() {
String msg = input.getString("msg");
if (isEmpty(msg)) {
msg = "World!";
}
output.setValue("msg", msg);
return SUCCESS;
}
}

Create ApplicationManager Class :
In this example, we create a ApplicationManager Class to load actions and serve requests and responses.
The action will respond to requests to /Greet.sayHello.mtw, its implementation comes from the GreetAction.class and the method that will handle the request is the sayHello().

Code Logic :

package com.codenvy;

import com.codenvy.action.GreetAction;
import org.mentawai.core.ApplicationManager;

public class AppManager extends ApplicationManager {

@Override
public void loadActions() {
action("/Greet", GreetAction.class, "sayHello").on(SUCCESS, fwd("/hello.jsp"));

}
}

Create index.jsp

Sample index page for the web application giving a sample text field to enter a name.

<%@page pageEncoding="UTF-8" %>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@taglib prefix="mtw" uri="http://www.mentaframework.org/tags-mtw/"%>
<html>
<body>
<mtw:form action="Greet.sayHello.mtw" method="post">
Enter your name: <mtw:input type="text" name="msg" size="30" maxlength="30" />
<input type="submit" value="Go!" />
</mtw:form>
</body>
</html>

Create hello.jsp :

Upon successfully response, this page is called and message is displayed to User.

<%@page pageEncoding="UTF-8" %>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@taglib prefix="mtw" uri="http://www.mentaframework.org/tags-mtw/"%>
<html>
<body>
Hello <mtw:out value="msg" />
</body>
</html>
Create web.xml:
<web-app>
<display-name>Mentawai Web Framework</display-name>

<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>

<!-- The Mentawai controller -->
<servlet>
<servlet-name>Controller</servlet-name>
<servlet-class>org.mentawai.core.Controller</servlet-class>
<init-param>
<param-name>applicationManager</param-name>
<param-value>com.codenvy.AppManager</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<!-- You must choose an extension to indicate a mentawai action -->
<servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern>*.mtw</url-pattern>
</servlet-mapping>
</web-app>

In the web.xml one must indicate the full name of application manager class. In this case [ com.codenvy.AppManager ]

Build and Run the application

Upon successful building the application and running, it displays.

GetImage (11)

GetImage (12)