Thursday, March 6, 2014

Membuat Custom Tag di JSP

Pendahuluan


Untuk membuat Custom Tag di JSP, ikuti langkah-langkah dibawah ini:

Membuat Tag Handler

Buatlah kelas NumberFormatTag.java seperti di bawah ini:

package com.mbj.jsp.customtag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import java.io.IOException;
import java.text.DecimalFormat;


public class NumberFormatTag extends SimpleTagSupport {

    private String format;
    private String number;

    public NumberFormatTag() {
    }

    public void setFormat(String format) {
        this.format = format;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    @Override
    public void doTag() throws JspException, IOException {
        System.out.println("Number is:" + number);
        System.out.println("Format is:" + format);
        try {
            double amount = Double.parseDouble(number);
            DecimalFormat formatter = new DecimalFormat(format);
            String formattedNumber = formatter.format(amount);
            getJspContext().getOut().write(formattedNumber);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}



Membuat File TLD


Buatlah file mbjtld.tld seperti di bawah ini:

<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
    version="2.0">
<description>Number Format Tag</description>
<tlib-version>2.1</tlib-version>
<short-name>mbjtag</short-name>
<uri>http://www.mbj.web.id/jsp/tlds/
mbjtag</uri>
<tag>
    <name>formatNumber</name>
    <tag-class>com.mbj.jsp.customtag.NumberFormatTag</tag-class>
    <body-content>empty</body-content>
    <attribute>
    <name>format</name>
    <required>true</required>
    </attribute>
    <attribute>
    <name>number</name>
    <required>true</required>
    </attribute>
</tag>
</taglib>


Letakkan file mbjtld.tld di folder WEB-INF/tld

Membuat File Document Descriptor (web.xml)


Setelah membuat TLD, saatnya TLD tadi kita tambahkan di file web.xml, seperti di bawah ini:

<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>JSP Custom Tags</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <jsp-config>
  <taglib>
    <taglib-uri>http://
mbj.web.id/jsp/tlds/mbjtag</taglib-uri>
    <taglib-location>/WEB-INF/tld/mbjtld.tld</taglib-location>
  </taglib>
  </jsp-config>
</web-app>


Membuat Halaman JSP


Buatlah file JSP numberformat.jsp seperti di bawah ini:

<%@ page language="java" contentType="text/html; charset=US-ASCII"
    pageEncoding="US-ASCII"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Contoh Custom Tag</title>
<%@ taglib uri="http://mbj.web.id/jsp/tlds/mbjtag" prefix="mbj"%>
</head>
<body>

<h2>Contoh Number Fomatting menggunakan Custom Tag</h2>

<mbj:formatNumber number="51203.20" format="#,###.00"/><br><br>

<mbj:formatNumber number="1234.567" format="$# ###.00"/><br><br>

</body>
</html>


Download Source Code


Silahkan download source codenya disini.

Penutup


Sekian pembahasan tutorial kali ini, semoga bermanfaat.

0 comments:

Post a Comment