Mentawai allows you to easily create your own custom tags. Here we explain how to create display and conditional tags.
Write the tag:
public class NumberOfFriendsTag extends PrintTag {
public String getStringToPrint() throws JspException {
Output output = action.getOutput();
User user = (User) output.getValue("user");
int nFriends = user.getNumberOfFriends();
if (nFriends == 1) {
return "You have 1 friend!";
} else if (nFriends == 0) {
return "You have no friends at all! Don't code too much!";
} else {
return "You have " + nFriends + " friends!";
}
}
}
TIP: You can also create a tag that supports a body and use the getBody() method.
Notice that because you extended from PrintTag, you have access to the following protected instance variables:
protected ServletContext application;
protected HttpSession session;
protected HttpServletRequest req;
protected HttpServletResponse res;
protected Action action;
protected Locale loc;
NOTE: If you access the JSP page directly the action will be null, but you can still access the other variables.
Create the TLD file:
Now to use your tag, you need a TLD file. This is a Servlet API requirement and there's nothing we can do about it, so save the XML below in a file called taglib.tld and place it inside the /WEB-INF/tld directory:
<?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>myTags</short-name>
<display-name>myTags</display-name>
<tag>
<name>showNumberOfFriends</name>
<tag-class>com.mysite.tag.NumberOfFriendsTag</tag-class>
<body-content>empty</body-content>
<display-name></display-name>
</tag>
Use your tag:
<%@ taglib uri="/WEB-INF/tld/taglib.tld" prefix="m" %> <html><body> <h1>Hello there!</h1> <h3><m:showNumberOfFriends /></h3> </body></html>
Write the tag:
public class IsComBr extends ConditionalTag {
public boolean testCondition() throws JspException {
String host = req.getRequestURL().toString();
return host.contains(".com.br");
}
}
Notice that because you extended from ConditionalTag, you have access to the following protected instance variables:
protected ServletContext application;
protected HttpSession session;
protected HttpServletRequest req;
protected HttpServletResponse res;
protected Action action;
protected Locale loc;
NOTE: If you access the JSP page directly the action will be null, but you can still access the other variables.
Create the TLD file:
Now to use your tag, you need a TLD file. This is a Servlet API requirement and there's nothing we can do about it, so save the XML below in a file called taglib.tld and place it inside the /WEB-INF/tld directory:
<?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>myTags</short-name>
<display-name>myTags</display-name>
<tag>
<name>isComBr</name>
<tag-class>com.mysite.tag.IsComBr</tag-class>
<body-content>JSP</body-content>
<display-name></display-name>
<attribute>
<name>negate</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
<type>boolean</type>
</attribute>
</tag>
TIP: The ConditionalTag already includes a negate attribute that you can configure in the TLD to support else for your tag.
Use your tag:
<%@ taglib uri="/WEB-INF/tld/taglib.tld" prefix="m" %> <m:isComBr> <!-- Show or do something here related to the .com.br site --> </m:isComBr> <m:isComBr negate="true"> <!-- Show or do something here related to all other sites --> </m:isComBr>