※ xyz.itwill.custom 패키지에 HelloBodyTag.java 클래스 생성
📃HelloBodyTag.java
package xyz.itwill.custom; // import java.io.IOException; // import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.TagSupport; // //태그 속성과 태그 내용이 있는 커스텀 태그의 클래스 public class HelloBodyTag extends TagSupport{ private static final long serialVersionUID = 1L; // //커스텀 태그의 속성값을 저장하기 위한 필드 //→ 태그 속성이 필수인 경우 생성자를 이용하여 필드 초기화 작업 생략 private boolean test; // public boolean isTest() { return test; } // public void setTest(boolean test) { this.test = test; } @Override public int doStartTag() throws JspException { try { if(test) { pageContext.getOut().println("<h3>"); } else { pageContext.getOut().println("<p>"); } } catch (IOException e) { throw new JspException(e.getMessage()); } return EVAL_BODY_INCLUDE;//태그 내용을 포함하여 전달해라 } @Override public int doEndTag() throws JspException { try { if(test) { pageContext.getOut().println("님, 안녕하세요.</h3>"); } else { pageContext.getOut().println("님, 반갑습니다.</p>"); } } catch (IOException e) { throw new JspException(e.getMessage()); } return EVAL_PAGE; } }
※ WEB-INF/tld 폴더에 custom.tld 파일 생성(tld 파일 만드는법 [MVC 4-5 참고])
※ [MVC 4-8] tld 파일에서 추가 작성📃custom.tld
<?xml version="1.0" encoding="UTF-8"?> <taglib version="2.1" xmlns="Http://java.sun.com/xml/ns/javaee" 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/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd "> <description>단순한 형태의 커스텀 태그 구현</description> <tlib-version>1.0</tlib-version> <short-name>simple</short-name> <uri>http://www.itwill.xyz/mvc/custom</uri> <!-- --> <!-- HelloTag.java에 사용하는 tag --> <!-- tag : 커스텀 태그를 등록하기 위한 엘리먼트 --> <tag> <!-- name : 커스텀 태그의 이름을 등록하기 위한 엘리먼트 --> <name>hello</name> <!-- tag-class : 커스텀 태그 사용시 생성될 객체의 태그 클래스를 설정하기 위한 엘리먼트 --> <tag-class>xyz.itwill.custom.HelloTag</tag-class> <!-- body-content : 커스텀 태그에서 사용 가능한 태그내용을 설정하기 위한 엘리먼트 --> <!-- → empty : 태그내용이 없는 커스텀 태그를 설정하기 위한 엘리먼트 값 --> <body-content>empty</body-content> </tag> <!-- --> <!-- HelloMessageTag.java에 사용하는 tag --> <tag> <name>helloMessage</name> <tag-class>xyz.itwill.custom.HelloMessageTag</tag-class> <body-content>empty</body-content> <!-- attribute : 태그 속성을 등록하기 위한 엘리먼트 --> <attribute> <!-- name : 태그 속성명을 설정하는 엘리먼트 - 태그 클래스의 필드명과 같은 이름으로 작성 --> <name>name</name> <!-- required : 커스텀 태그의 속성에 대한 필수 여부를 설정하기 위한 엘리먼트 --> <!-- → false : 선택 속성(기본), true : 필수 속성(무조건 써야함) --> <required>true</required> </attribute> </tag> <!-- --> <!-- HelloBodyTag.java에 사용하는 tag --> <tag> <name>helloBody</name> <tag-class>xyz.itwill.custom.HelloBodyTag</tag-class> <!-- body-content 엘리먼트 값으로 [JSP]를 설정하면 태그내용으로 JSP 명령 사용 가능 --> <body-content>JSP</body-content> <attribute> <name>test</name> <required>true</required> </attribute> </tag> </taglib>
※ webapp/custom 폴더에 hello_body_tag.jsp 생성
📃hello_body_tag.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="simple" uri="http://www.itwill.xyz/mvc/custom" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>MVC</title> </head> <body> <h1>Custom Tag - AnyAttribute And NoBody</h1> <hr> <simple:helloBody test="true">홍길동</simple:helloBody> <simple:helloBody test="false">임꺽정</simple:helloBody> <% String name="전우치"; request.setAttribute("name", "일지매"); %> <simple:helloBody test="true"><%=name %></simple:helloBody> <simple:helloBody test="false">${name }</simple:helloBody> <% boolean sw=true; request.setAttribute("sw", false); %> <simple:helloBody test="<%=sw %>">장길산</simple:helloBody> <simple:helloBody test="${sw }">홍경래</simple:helloBody><%-- 에러발생 → custom.tld에 <rtexprvalue>true로 설정해야함 --%> </body> </html>