We recommend you use Maven so you can easily run and test this web application with the Jetty plugin. You can click here to see and download the pom.xml you can use to build and run the code below. Or if you don't want to use Maven you can click here to download the full mentawai-all.jar file you can place inside your WEB-INF/lib directory. You can also download the full source code from the svn repository:
Clone repository: git clone https://github.com/MentaFramework/mentawai-examples
See sub-folder HelloMenta for reference
Start by configuring the web.xml to use Mentawai:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <!-- Override default start page --> <welcome-file-list> <welcome-file>/jsp/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>org.hellomenta.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 highlighted line above you must indicate the full name of your application manager class.
Write the action:
package org.hellomenta.action; import org.mentawai.core.BaseAction; public class HelloAction extends BaseAction { public String hi() { String msg = input.getString("msg"); if (isEmpty(msg)) { msg = "Why you did not type anything?"; } output.setValue("msg", msg); return SUCCESS; } }
Mentawai uses the Input and the Output to get information from the request and send information back as a response.
Write the application manager:
package org.hellomenta; import org.hellomenta.action.HelloAction; import org.mentawai.core.ApplicationManager; public class AppManager extends ApplicationManager { @Override public void loadActions() { action("/Hello", HelloAction.class, "hi") .on(SUCCESS, fwd("/jsp/hello.jsp")); } }
The action will respond to requests to /Hello.hi.mtw, its implementation comes from the HelloAction.class and the method that will handle the request is the hi().
Write the JSPs:
/src/main/webapp/jsp/index.jsp:
<%@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="Hello.hi.mtw" method="post"> Type something: <mtw:input type="text" name="msg" size="30" maxlength="30" /> <input type="submit" value="Go!" /> </mtw:form> </body> </html>
/src/main/webapp/jsp/hello.jsp:
<%@page pageEncoding="UTF-8" %> <%@ page contentType="text/html; charset=UTF-8"%> <%@taglib prefix="mtw" uri="http://www.mentaframework.org/tags-mtw/"%> <html> <body> <mtw:out value="msg" /> </body> </html>
Notice the Mentawai view tags to help you out with forms and everything. Because they are integrated with the framework they are much easier and cleaner than JSTL.
Run to test it:
If you are using Maven you can just do:
mvn jetty:run
Otherwise deploy and run in your choice of web container.
See it working:
Project structure: