Skip to content

Instantly share code, notes, and snippets.

@mattkruskamp
Created August 25, 2015 02:51
Show Gist options
  • Save mattkruskamp/7b12e646429a837d4ce3 to your computer and use it in GitHub Desktop.
Save mattkruskamp/7b12e646429a837d4ce3 to your computer and use it in GitHub Desktop.
J2EE Custom Simple Tags Part 1
<?xml version=“1.0” encoding=“iso-8859-1” ?>
<taglib xmlns=“http://java.sun.com/xml/ns/j2ee”
xmlns:xsi=“http://www.w3c.org/2001/XMLSchema-instance”
xsi:schemaLocation=“http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd”
version=“2.0”>
<tlib-version>1.2</tlib-version>
<uri>myTags</uri>
<tag>
<description>Custom tags</description>
<name>Square</name>
<tag-class>com.cyberkruz.test.NewMath</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>num</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
package com.cyberkruz.test;
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class NewMath extends SimpleTagSupport {
private Integer num;
/**
* Method overridden which is called
* by the jsp.
*/
public void doTag() throws JspException, IOException {
// Gets the jsp context and prints the
// number squared.
this.getJspContext().getOut().print(this.num * this.num);
}
/**
* Sets a number that our custom
* tag squares.
* @param num The number to which
* we want to square.
*/
public void setNum(Integer num) {
this.num = num;
}
}
<%@page language=“java”
contentType=“text/html; charset=ISO-8859-1”
pageEncoding=“ISO-8859-1”%>
<%@ taglib=“” prefix=“math” uri=“myTags” %>
<html>
<head>
<title>SimpleTest</title>
<meta http-equiv=“Content-Type” content=“text/html; charset=ISO-8859-1”>
</head>
<body>
10 squared is: <math:Square num=“10” />.
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment