JSTL(JSP Standard Tag Library) 태그를 사용하면 JSP에서 반복문, 상태제어 등 여러가지 기능을 할 수 있다.
하지만 때로는 JSTL에서 제공해주는 태크로는 충분하지 않을때가 있는데 이때 커스텀태그를 생성할 수 있다. 커스텀 태그라고 특별한것은 아니고 JSTL 또한 커스텀태그의 일종인데 자주 사용하는 라이브러리를 모아놓은게 JSTL이다
JSP 커프텀 태그(Coustom tag)
예를들어 숫자를 천단위로 콤마를 찍는다고 가정합니다.
전체소스코드는 github에 있습니다.
https://github.com/sehoone/customJspTag
사용법
<mytags:monneyFormat number="123123.574" format="#,###.00"/>
구성
소스코드
JAVA로 커스텀태그 로직을 만듭니다.
NumberFormat.java
package customTag; import java.io.IOException; import java.text.DecimalFormat; import javax.servlet.jsp.JspException; import javax.servlet.jsp.SkipPageException; import javax.servlet.jsp.tagext.SimpleTagSupport; public class NumberFormat extends SimpleTagSupport { private String format; private String number; 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("input Number :" + number); System.out.println("input Format :" + format); try {
//포맷팅할 숫자 double amount = Double.parseDouble(number); //포맷 형식( EX. #,###.00 ) DecimalFormat formatter = new DecimalFormat(format);
String formattedNumber = formatter.format(amount);
//출력 getJspContext().getOut().write(formattedNumber);
} catch (Exception e) { e.printStackTrace(); throw new SkipPageException("Exception formatting " + number + " format " + format); } } } |
tld(tag library description) 에 사용할 태그명 및 입력값을 설정
numberFormat.tld
<?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 Formatter Custom Tag</description> <tlib-version>2.1</tlib-version> <short-name>mytags</short-name> <tag> <!-- 태크이름 --> <name>monneyFormat</name> <!-- 태크 클래스 --> <tag-class>customTag.NumberFormat</tag-class> <body-content>tagdependent</body-content>
<!-- 입력 파라메터 --> <attribute> <name>format</name> <required>true</required> </attribute>
<attribute> <name>number</name> <required>true</required> </attribute>
</tag> </taglib> |
tld파일의 경로를 설정
만약에 WEB-INF 바로 아래에 두는경우에는 web.xml에 선언하지 않아도 됩니다.
web.xml
<?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>Archetype Created Web Application</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
<jsp-config> <taglib> <taglib-uri>numberformatter</taglib-uri> <taglib-location>/WEB-INF/tlds/numberformatter.tld</taglib-location> </taglib> </jsp-config> </web-app> |
JSP 에 커스텀 태크를 사용합니다
index.jsp
<%@ 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>Custom Tag Example</title> <!-- 테그 경로 설정 --> <%@ taglib uri="WEB-INF/tlds/numberFormat.tld" prefix="mytags" %> </head> <body> <h2>customTag Test</h2> <mytags:monneyFormat number="123123.574" format="#,###.00"/><br><br> </body> </html> |