์ปค์Šคํ…€ ํƒœ๊ทธ๋ฅผ ๊ฐ–๊ณ  ์žˆ๋Š” JSPํŽ˜์ด์ง€๊ฐ€ ์„œ๋ธ”๋ฆฟ์œผ๋กœ ๋ณ€ํ™˜๋  ๋•Œ, ๊ทธ ํƒœ๊ทธ๋Š” tag handler๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ๊ฐ์ฒด์˜ operation์œผ๋กœ ๋ณ€ํ™˜๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ ๋‚˜์„œ JSPํŽ˜์ด์ง€์˜ ์„œ๋ธ”๋ฆฟ์ด ์‹คํ–‰๋  ๋•Œ ์›น์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๊ทธ opreation์„ ํ˜ธ์ถœํ•˜๊ฒŒ ๋˜๋Š” ๊ฑฐ์ฃ .

JSP tag extensions ๋Š” JSP์— ์ง์ ‘ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋Š” ์ƒˆ๋กœ์šด ํƒœ๊ทธ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค๋‹ˆ๋‹ค. JSP 2.0 specification์—๋Š” ์ด๋Ÿฐ ์ปค์Šคํ…€ ํƒœ๊ทธ๋ฅผ ์“ฐ๊ธฐ์œ„ํ•œ Simple Tag Handlers ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

customer tag๋ฅผ ์“ฐ๋ ค๋ฉด ๋‹จ์ˆœํžˆ SimpleTagSupport ๋ฅผ ์ƒ์†ํ•˜๊ณ   doTag()๋ฉ”์†Œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.  

 

Create "Hello" Tag ( ์‚ฌ์šฉ์ž ์ •์˜ ํƒœ๊ทธ ์ƒ์„ฑํ•˜๊ธฐ )

์•„๋ž˜์ฒ˜๋Ÿผ  <ex:Hello> ๋ฅผ ์“ฐ๊ณ ์‹ถ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ด…์‹œ๋‹ค. body ํƒœ๊ทธ๋„ ์—†์ด ๋ง์ž…๋‹ˆ๋‹ค. 

<ex:Hello />

custom JSP tag๋ฅผ ๋งŒ๋“ค๋ ค๋ฉด ํƒœ๊ทธ ํ•ธ๋“ค๋Ÿฌ Java class ๋ถ€ํ„ฐ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 

์•„๋ž˜์ฒ˜๋Ÿผ ์ž๋ฐ” ์ฝ”๋”ฉ์„ ํ•ด๋ด…์‹œ๋‹ค. 

package com.tutorialspoint; import javax.servlet.jsp.tagext.*; import javax.servlet.jsp.*; import java.io.*; public class HelloTag extends SimpleTagSupport { public void doTag() throws JspException, IOException { JspWriter out = getJspContext().getOut(); out.println("Hello Custom Tag!"); } }

์œ„ ์ฝ”๋“œ๋Š” doTag() ๋ฉ”์†Œ๋“œ์—์„œ getJspContext() method๋ฅผ ์ด์šฉํ•ด์„œ ํ˜„์žฌ JspContext object๋ฅผ ์–ป์–ด์˜ค๊ณ  "Hello Custom Tag!"๋ฉ”์‹œ์ง€๋ฅผ  JspWriter ๊ฐ์ฒด๋กœ ๋ณด๋‚ด๋Š” ๊ธฐ๋Šฅ์„ ๋„ฃ์€ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

์ด์ œ ์ด ์†Œ์ŠคํŒŒ์ผ์„ ์ปดํŒŒ์ผํ•ด์„œ classํŒŒ์ผ๋กœ ๋งŒ๋“ค๊ณ  ์ด ํด๋ž˜์Šค ํŒŒ์ผ์„ ํ™˜๊ฒฝ๋ณ€์ˆ˜ CLASSPATH์— ์ง€์ •๋˜์–ด์žˆ๋Š” ๊ณณ์— ๋ณต์‚ฌํ•ด์„œ ๋„ฃ์–ด์ฃผ์„ธ์š”. ๋งˆ์ง€๋ง‰์œผ๋กœ ์•„๋ž˜์™€ ๊ฐ™์€ ํƒœ๊ทธ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์œ„์น˜๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 <Tomcat-Installation-Directory>webapps\ROOT\WEB-INF\custom.tld

<taglib> <tlib-version>1.0</tlib-version> <jsp-version>2.0</jsp-version> <short-name>Example TLD</short-name> <tag> <name>Hello</name> <tag-class>com.tutorialspoint.HelloTag</tag-class> <body-content>empty</body-content> </tag> </taglib>

์ด์ œ JSPํŒŒ์ผ์—์„œ ์ด Hello ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•ด ๋ด…์‹œ๋‹ค.  

<%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%> <html> <head> <title>A sample custom tag</title> </head> <body> <ex:Hello/> </body> </html>

์ด์ œ ๋งŒ๋“ค์–ด์ง„ JSPํŒŒ์ผ์„ ์›น๋ธŒ๋ผ์šฐ์ €์—์„œ ์š”์ฒญํ•ด ๋ณด์„ธ์š”. ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ฌ ๊ฒƒ์ž…๋‹ˆ๋‹ค. 

Hello Custom Tag!


Accessing the Tag Body ( ํƒœ๊ทธ ๋ฐ”๋””์— ์ ‘๊ทผํ•˜๊ธฐ )

์ปค์Šคํ…€ ํƒœ๊ทธ๋„ ์ผ๋ฐ˜ HTMLํƒœ๊ทธ์ฒ˜๋Ÿผ ์‹œ์ž‘ํƒœ๊ทธ์™€ ์ข…๋ฃŒํƒœ๊ทธ ์‚ฌ์ด์— ๋ฐ”๋””๋ถ€๋ถ„์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

<ex:Hello> This is message body </ex:Hello>

์œ„์—์„œ ์ž‘์„ฑํ–ˆ๋˜ ์ž๋ฐ”์†Œ์Šค๋ฅผ ์•„๋ž˜์ฒ˜๋Ÿผ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค. 

package com.tutorialspoint; import javax.servlet.jsp.tagext.*; import javax.servlet.jsp.*; import java.io.*; public class HelloTag extends SimpleTagSupport { StringWriter sw = new StringWriter(); public void doTag() throws JspException, IOException { getJspBody().invoke(sw); getJspContext().getOut().println(sw.toString()); } }

์—ฌ๊ธฐ์„œ๋Š” StringWriter๋กœ ์ฝ์–ด๋“ค์ธ ํ›„์— ์ฝ์–ด๋“ค์ธ ๊ฐ’์„ JspWriter์—์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.

TLD ํŒŒ์ผ๋„ ์•„๋ž˜์ฒ˜๋Ÿผ ๋ณ€๊ฒฝ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

<taglib> <tlib-version>1.0</tlib-version> <jsp-version>2.0</jsp-version> <short-name>Example TLD with Body</short-name> <tag> <name>Hello</name> <tag-class>com.tutorialspoint.HelloTag</tag-class> <body-content>scriptless</body-content> </tag> </taglib>

JSPํŒŒ์ผ์€ ์•„๋ž˜์ฒ˜๋Ÿผ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.

<%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%> <html> <head> <title>A sample custom tag</title> </head> <body> <ex:Hello> This is message body </ex:Hello> </body> </html>

๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ ๋‚˜์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค.

This is message body


Custom Tag Attributes ( ์ปค์Šคํ…€ ํƒœ๊ทธ์˜ ์†์„ฑ )

 custom tags๋Š” ์†์„ฑ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ๋  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.  ์†์„ฑ๊ฐ’์„ ๋ฐ›์œผ๋ ค๋ฉด custom tag class๋Š” setter methods๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. JavaBean setter methods์™€ ๋™์ผํ•˜๊ฒŒ ๋ง์ด์ฃ . 

package com.tutorialspoint; import javax.servlet.jsp.tagext.*; import javax.servlet.jsp.*; import java.io.*; public class HelloTag extends SimpleTagSupport { private String message; public void setMessage(String msg) { this.message = msg; } StringWriter sw = new StringWriter(); public void doTag() throws JspException, IOException { if (message != null) { /* Use message from attribute */ JspWriter out = getJspContext().getOut(); out.println( message ); } else { /* use message from the body */ getJspBody().invoke(sw); getJspContext().getOut().println(sw.toString()); } } }

์†์„ฑ๋ช…์€ "message"์ด๊ณ  ์„ธํ„ฐ๋ฉ”์†Œ๋“œ๋Š” ๋‹น์—ฐํžˆ setMessage()๊ฐ€ ๋˜๊ฒ ์ฃ . ์ด์ œ ์ด ์†์„ฑ์„ TLD file์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. <attribute> ์š”์†Œ๋ฅผ ์ด์šฉํ•ด์„œ ๋ง์ด์ฃ .

<taglib> <tlib-version>1.0</tlib-version> <jsp-version>2.0</jsp-version> <short-name>Example TLD with Body</short-name> <tag> <name>Hello</name> <tag-class>com.tutorialspoint.HelloTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>message</name> </attribute> </tag> </taglib>

JSPํŒŒ์ผ์— ๋ฉ”์‹œ์ง€ ์†์„ฑ์„ ์ด์šฉํ•œ ํƒœ๊ทธ๋ฅผ ์ถ”๊ฐ€ํ•˜์„ธ์š”. 

<%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%> <html> <head> <title>A sample custom tag</title> </head> <body> <ex:Hello message="This is custom tag" /> </body> </html>

๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ ๋‚˜์˜ฌ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

This is custom tag

 

 

์†์„ฑ์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ํ”„๋กœํผํ‹ฐ๋“ค์ด ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

PropertyPurpose
nameThe name element defines the name of an attribute. Each attribute name must be unique for a particular tag.
requiredThis specifies if this attribute is required or optional. It would be false for optional.
rtexprvalueDeclares if a runtime expression value for a tag attribute is valid
typeDefines the Java class-type of this attribute. By default it is assumed as String
descriptionInformational description can be provided.
fragmentDeclares if this attribute value should be treated as a JspFragment.

 

์†์„ฑ ๊ด€๋ จ ํ”„๋กœํผํ‹ฐ๋ฅผ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

.....
    <attribute>
      <name>attribute_name</name>
      <required>false</required>
      <type>java.util.Date</type>
      <fragment>false</fragment>
    </attribute>
.....

๋‘ ๊ฐœ ์ด์ƒ์˜ ์†์„ฑ์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด TLDํŒŒ์ผ์— ์•„๋ž˜์ฒ˜๋Ÿผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

..... <attribute> <name>attribute_name1</name> <required>false</required> <type>java.util.Boolean</type> <fragment>false</fragment> </attribute> <attribute> <name>attribute_name2</name> <required>true</required> <type>java.util.Date</type> </attribute>

.....





Reference : http://www.tutorialspoint.com/jsp/jsp_custom_tags.htm