[JSP 2-3] taglib Directive

์ž„์Šนํ˜„ยท2022๋…„ 12์›” 7์ผ
0

JSP

๋ชฉ๋ก ๋ณด๊ธฐ
6/20

๐Ÿงtaglib Directive

โ—ˆ 1.taglib.jsp ํŒŒ์ผ ๋งŒ๋“ค๊ธฐ

๐ŸŽจtaglib Directive

๐Ÿ“Œ JSP ๋ฌธ์„œ์— ํƒœ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํŒŒ์ผ(TLD)์„ ๋ถˆ๋Ÿฌ์™€ ํฌํ•จํ•˜์—ฌ ์ปค์Šคํ…€ ํƒœ๊ทธ๋ฅผ ์ œ๊ณตํ•˜๋Š” ์ง€์‹œ์–ด
๐Ÿ“Œ ํƒœ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํŒŒ์ผ : ํƒœ๊ทธ ํด๋ž˜์Šค๋ฅผ ์ปค์Šคํ…€ ํƒœ๊ทธ๋กœ ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ•œ XML ํŒŒ์ผ

๐ŸŽจ์ปค์Šคํ…€ ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํƒœํฌ ํด๋ž˜์Šค๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœํ•˜์—ฌ

๐Ÿ”ŠCustomTag.java ํ˜ธ์ถœํ•˜์—ฌ ์‚ฌ์šฉ

<custom:display/>
<custom:display/>
<custom:display/>

๐ŸŽจ taglib Directive๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํƒœ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํŒŒ์ผ(TLD)์„ ๋ถˆ๋Ÿฌ์™€ JSP ๋ฌธ์„œ์— ํฌํ•จ

๐Ÿ“Œ prefix ์†์„ฑ : ์ปค์Šคํ…€ ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ๋„ค์ž„์ŠคํŽ˜์ด์Šค(namespace)๋ฅผ ์†์„ฑ๊ฐ’์œผ๋กœ ์„ค์ •
๐Ÿ“Œ uri ์†์„ฑ : ํƒœ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํŒŒ์ผ(TLD)์˜ ์‹๋ณ„์ž(uri ์—˜๋ฆฌ๋จผํŠธ๊ฐ’)๋ฅผ ์†์„ฑ๊ฐ’์œผ๋กœ ์„ค์ •
โ†’ ""์‚ฌ์ด์— [ctrl]+[space] ๋ˆŒ๋Ÿฌ์„œ ์ž๋™์™„์„ฑ ๊ฐ€๋Šฅ

<%@taglib prefix="custom" uri="http://www.itwill.xyz/custom"%>

๐Ÿ“ƒtaglib.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%-- taglib Directive๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํƒœ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํŒŒ์ผ(TLD)์„ ๋ถˆ๋Ÿฌ์™€ JSP ๋ฌธ์„œ์— ํฌํ•จ --%>
<%-- prefix ์†์„ฑ : ์ปค์Šคํ…€ ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ๋„ค์ž„์ŠคํŽ˜์ด์Šค(namespace)๋ฅผ ์†์„ฑ๊ฐ’์œผ๋กœ ์„ค์ • --%>
<%-- uri ์†์„ฑ : ํƒœ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํŒŒ์ผ(TLD)์˜ ์‹๋ณ„์ž(uri ์—˜๋ฆฌ๋จผํŠธ๊ฐ’)๋ฅผ ์†์„ฑ๊ฐ’์œผ๋กœ ์„ค์ • --%>
<%-- โ†’ ""์‚ฌ์ด์— [ctrl]+[space] ๋ˆŒ๋Ÿฌ์„œ ์ž๋™์™„์„ฑ ๊ฐ€๋Šฅ --%>
<%@taglib prefix="custom" uri="http://www.itwill.xyz/custom"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSP</title>
</head>
<body>
	<h1>taglib Directive</h1>
	<hr>
	<p>JSP ๋ฌธ์„œ์— ํƒœ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํŒŒ์ผ(TLD)์„ ๋ถˆ๋Ÿฌ์™€ ํฌํ•จํ•˜์—ฌ ์ปค์Šคํ…€ ํƒœ๊ทธ๋ฅผ ์ œ๊ณตํ•˜๋Š” ์ง€์‹œ์–ด</p>
	<p>ํƒœ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํŒŒ์ผ : ํƒœ๊ทธ ํด๋ž˜์Šค๋ฅผ ์ปค์Šคํ…€ ํƒœ๊ทธ๋กœ ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ•œ XML ํŒŒ์ผ</p>
	<hr>
	<%-- ์ปค์Šคํ…€ ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํƒœํฌ ํด๋ž˜์Šค๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœํ•˜์—ฌ   --%>
	<custom:display/>
	<custom:display/>
	<custom:display/>
</body>
</html>

โ—ˆ 2.xyz.itwill.custom ํŒจํ‚ค์ง€ ์ƒ์„ฑ โ†’ CustomTag ํด๋ž˜์Šค ์ƒ์„ฑ

๐Ÿ“Œ ํƒœ๊ทธ ํด๋ž˜์Šค : JSP ๋ฌธ์„œ์—์„œ ์ปค์Šคํ…€ ํƒœ๊ทธ ์‚ฌ์šฉ์‹œ ์‹คํ–‰๋˜๊ธฐ ์œ„ํ•œ ํด๋ž˜์Šค
โ†’ TagSupport ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์•„ ์ž‘์„ฑ

๐Ÿ“ƒCustomTag.java

package xyz.itwill.custom;
//
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
//
//ํƒœ๊ทธ ํด๋ž˜์Šค : JSP ๋ฌธ์„œ์—์„œ ์ปค์Šคํ…€ ํƒœ๊ทธ ์‚ฌ์šฉ์‹œ ์‹คํ–‰๋˜๊ธฐ ์œ„ํ•œ ํด๋ž˜์Šค
//โ†’ TagSupport ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์•„ ์ž‘์„ฑ
public class CustomTag extends TagSupport {
	private static final long serialVersionUID = 1L;
	//
	//JSP ๋ฌธ์„œ์—์„œ ์ปค์Šคํ…€ ํƒœ๊ทธ์˜ ์‹œ์ž‘ํƒœ๊ทธ ์‚ฌ์šฉ์‹œ ์ž๋™ ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์†Œ๋“œ
	@Override
	public int doStartTag() throws JspException {
		try {
			pageContext.getOut().println("<h3>์ปค์Šคํ…€ ํƒœ๊ทธ ์‚ฌ์šฉ</h3>");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return super.doStartTag();
	}
}

โ—ˆ 3.WEB-INF ํด๋”์— tld ํŒŒ์ผ ์ƒ์„ฑ

  1. WEB-INF ์˜ค๋ฅธ์ชฝ ํด๋ฆญ โ†’ NEW โ†’ Other
  2. XML โ†’ XML File ์„ ํƒ โ†’ Next
  3. File name : custom.tld(์ด๋ฆ„์€ ์‚ฌ์šฉ์ž ์ง€์ •) โ†’ Next
  4. ์ฒดํฌํ•ญ๋ชฉ ํ™•์ธ โ†’ Next
  5. ์„ ํƒฑํ•ญ๋ชฉ ํ™•์ธ โ†’ Next
  6. j2ee ์„ ํƒ โ†’ Edit
  7. Prefix์— ์ด๋ฆ„ ์‚ญ์ œ โ†’ OK
  8. j2ee์—์„œ no prefix ๋ณ€๊ฒฝ๋œ๊ฑฐ ํ™•์ธ โ†’ Finish

๐Ÿ“ƒcustom.tld

<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xml="http://www.w3.org/XML/1998/namespace" 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 ">
  <tlib-version>0.0</tlib-version>
  <short-name>NMTOKEN</short-name>
  <uri>http://www.itwill.xyz/custom</uri>
  <tag>
  	<name>display</name>
  	<tag-class>xyz.itwill.custom.CustomTag</tag-class>
  	<body-content>empty</body-content>
  </tag>
</taglib>

0๊ฐœ์˜ ๋Œ“๊ธ€