Mentawai Web Framework

Display Tags

Mentawai handles the view layer through display tags. If the consequence of the action is FORWARD, all the action information is available in the view layer, so that it can be displayed by JSP tags.

If you prefer, you can also use JSTL, JSP EL and Velocity in your view layer.

Below we show several examples:

<!-- displaying an object from the action output -->
User: <mtw:out value="user"/><!-- toString() is called -->
                  
<!-- displaying attributes of a bean in the action output -->
<mtw:bean value="user">
Name: <mtw:out value="name"/><br>
Email: <mtw:out value="email"/><br>
</mtw:bean>                

<!-- displaying attributes of a nested bean in the action output -->
<mtw:bean value="user">
<mtw:bean value="name">
First Name: <mtw:out value="firstName"/><br>
Last Name: <mtw:out value="lastName"/><br>
</mtw:bean>
Email: <mtw:out value="email"/><br>
</mtw:bean>

<!-- displaying a list of objects in the action output -->
<mtw:list value="users">
My list of users:
<mtw:isEmpty>
You don't havce anyone in your list!
</mtw:isEmpty>
<mtw:loop>
User: <mtw:out /><br><!-- toString() is called -->
</mtw:loop>
</mtw:list>

<!-- displaying a list of attributes from objects in the action output -->
<mtw:list value="users">
My list of users:
<mtw:isEmpty>
You don't have anyone in your list!
</mtw:isEmpty>
<mtw:loop>
Name: <mtw:out value="name"/><br>
Email: <mtw:out value="email"/><br>
</mtw:loop>
</mtw:list>

<!-- integration with data lists -->
<mtw:bean value="user">
Name: <mtw:out value="name"/><br>
Email: <mtw:out value="email"/><br>
<!-- the gender value must be an int or an int array -->
<!-- otherwise an exception is thrown -->
Gender: <mtw:out value="gender" list="genders"/><br>
</mtw:bean>

<!-- using the mentawai if tag -->
<mtw:bean value="user">
Name: <mtw:out value="name"/><br>
Email: <mtw:out value="email"/><br>
<!-- Looks for isAdmin() -->
<!-- If not found looks for hasAdmin() -->
<!-- If not found looks for getAdmin() -->
<mtw:if test="admim"><!-- or <mtw:if test="admin" value="true"/> -->
(ADMIN!)<br>
</mtw:if>
<mtw:if test="status" value="2">
Your status is bad! Check your balance please!
</mtw:if>
<mtw:if test="city" value="Atlanta">
How is the weather in Atlanta?
</mtw:if>
</mtw:bean>

<!-- all mentawai conditional tags implement else through the negate attribute -->
<mtw:list value="users">
My list of users:
<mtw:isEmpty negate="true">
Congratulations! You have some users!
</mtw:isEmpty>
<mtw:loop>
User: <mtw:out /><br>
<mtw:if test="admim" negate="true">
(NOT AN ADMIN!)<br>
</mtw:if>
</mtw:loop>
</mtw:list>
            


Integration with JSTL and Expression Language

You can also program your view layer using JSTL (JSP Stanndard Tag Library) and EL (Expression Language). Mentawai display tags are fully compatible with the JSTL and EL. Check the examples below:

<!-- displaying an object from the action output -->
User: <c:out value="${user}"/>
                  
<!-- displaying object attributes from the action output -->
Name: <c:out value="${user.name}"/>
Email: <c:out value="${user.email}"/>
                  
<!-- displaying attributes of a bean in the action output -->
<mtw:bean value="user"><!-- same as <mtw:bean value="user" var="user"> -->
Name: <mtw:out value="name"/><br>
Email: <c:out value="${user.email}"/><br>
</mtw:bean>

<!-- using the var attribute -->
<mtw:bean value="user" var="theUser" >
Name: <mtw:out value="name"/><br>
Email: <c:out value="${theUser.email}"/><br>
</mtw:bean>

<!-- using the loop counter and var attribute -->
<mtw:list value="users"><!-- same as <mtw:list value="users" var="users" /> -->
My list of users:
<mtw:isEmpty>
You don't have anyone in your list!
</mtw:isEmpty>
<mtw:loop var="user"><!-- same as <mtw:loop var="user" counter="counter" /> -->
Line number: <c:out value="${counter}" />
<c:if test="${counter % 2 == 0}">(even)</c:if>
<c:if test="${counter % 2 == 1}">(odd)</c:if>
<br>
Name: <mtw:out value="name"/><br>
Email: <c:out value="${user.email}"/><br>
</mtw:loop>
</mtw:list>

Notice that the tags <mtw:bean>, <mtw:list> and <mtw:loop> have the optional var attribute. By using the var attribute you can tell the Mentawai display tag to place the object in the page context, so that the JSTL tag or EL can access it. The <mtw:loop> has also an special variable called counter, which counts, beginning from zero, the number of iterations.



Custom Display Tags

The more logic you place in your JSP pages, the worse it is for your web designer. Therefore sometimes it is better to put all the display logic inside a custom tag, so everything is hidden from your JSP page.

Mentawai makes it very easy to create your own custom display tags through the abstract class org.mentawai.tag.util.PrintFromContextTag. Below is an example:

public class UserAvatarImg extends PrintFromContextTag {
    
    private String klass = null;
    private String height= null;
    private String width = null;
    private String border = null;
    
    public void setKlass(String klass) { this.klass = klass; }
    public void setHeight(String height) { this.height = height; }
    public void setWidth(String width) { this.width = width; }
    public void setBorder(String border) { this.border = border; }
    
    private String calculateURL(User user) {
        // do something crazy here...
    }

    public String getStringToPrint(Object context) throws JspException {
        if (!(context instanceof User)) {
            throw new JspException("UserAvatar not inclosed by an User context!");
        }
        User user = (User) context;
        StringBuffer sb = new StringBuffer(150);
        sb.append("<img src=\"");
        sb.append(calculateURL(user));
        sb.append("\"");
        if (klass != null) sb.append(" class=\"").append(klass).append("\"");
        if (height != null) sb.append(" height=\"").append(height).append("\"");
        if (width != null) sb.append(" width=\"").append(width).append("\"");
        if (border != null) sb.append(" border=\"").append(border).append("\"");
        sb.append(" />");
        return sb.toString();
    }
}
    

Now you have to create a tag library descriptor (tld file) to define your tag. You should place this file in your application's /WEB-INF/tld directory. Below is an example of the user.tld file for the above tag class:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE taglib
        PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
        "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
    <tlib-version>1.0</tlib-version>
    <jsp-version>1.2</jsp-version>
    <short-name>user</short-name>
    <uri>/user</uri>
    <display-name>user</display-name>
	
    <tag>
      <name>avatarImg</name>
      <tag-class>examples.customtag.UserAvatarImg</tag-class>
      <body-content>empty</body-content>
      <display-name></display-name>
	  <attribute>
        <name>klass</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
        <type>java.lang.String</type>
      </attribute>
	  <attribute>
        <name>border</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
        <type>java.lang.String</type>
      </attribute>  
	  <attribute>
        <name>width</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
        <type>java.lang.String</type>
      </attribute> 
	  <attribute>
        <name>height</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
        <type>java.lang.String</type>
      </attribute>      
    </tag>	    
</tablib>
        

Notice that you should place all custom tags for a bean in the same tld file. Now to access your tag from a JSP page you can do this:

<%@ taglib uri="/WEB-INF/tld/user.tld" prefix="user" %>
<!-- displaying attributes of a bean in the action output -->
<mtw:bean value="user">
Name: <mtw:out value="name"/><br>
Email: <mtw:out value="email"/><br>
Avatar: <user:avatarImg klass="myStyle" height="100" width="100" /><br>
</mtw:bean>