Jsp&Servlet

표준 액션 태그 이외에 개발자의 커스텀 태그 만들기

살구르 2017. 1. 31. 11:30

1. XML 태그

1)표준 액션 태그 : 표준(jsp :)

<태그 속성=> : 태그는 (태그라이브러리:태그이름) ex) jsp:forward

forward : RequestDispatcher.forward()와 동일

<jsp:forward page=“exam03.jsp”></jsp:forward>

=> 이 때 forwarding 하고자 하는 page를 동적으로 할당할 수 있다.(exam12.jsp)

 

include : RequestDispatcher.include() 와 동일

<jsp:include page=“”></jsp:include> (exam04.jsp)

=> 이 때 including 하고자 하는 page를 동적으로 할당할 수 있다.

 

========bean 처리 태그

useBean

getProperty

setProperty

=====================

2)커스텀 태그 : 개발자가 만들어 사용하는 태그

-JSTL :

-개발자

-태그 핸들러 클래스 작성(.class)

-TLD 파일 작성(xml 파일)

 

2. 커스텀 태그 작성 방법

1)태그 핸들러 클래스 작성(.class)

2)TLD 파일 작성(xml 파일),Tag Library Descriptor jsp태그<==>class태그

()

-WEB-INF 밑에 tlds 폴더 생성, 그 안에 myTag.tld파일을 만들어 보자

-xml 파일 생성 => 파일명 : myTag.tld

-create XML file from a DTD file

-select XML catalog entry : Sun, Inc // DTD Jsp Tag library 1.2 // EN

3)web.xmlTLD 파일 등록 tld파일 <==>taglib-uri

<!-- tld 파일 등록 -->

<jsp-config>

<taglib>

<taglib-uri>http://myCustomTag.com</taglib-uri> <!-- 사용자 지정 -->

<taglib-location>/WEB-INF/tlds/myTag.tld</taglib-location>

</taglib>

</jsp-config>

 

4)JSP 파일에서 사용 전 TLD 파일 선언<%@ taglib uri="http://myCustomTag.com" prefix="my" %>

[실습]

com.sk.customTag.MyCustomTag.java

WEB-INF/tlds/myTag.tld

web.xml

exam16.jsp

 




[실습]

//com.sk.customTag.MyCustomTag.java

package com.sk.customTag;


import javax.servlet.jsp.JspException;

import javax.servlet.jsp.tagext.TagSupport;


public class MyCustomTag extends TagSupport {

// 시작 태그 시 실행

@Override

public int doStartTag() throws JspException {

System.out.println("doStartTag() 실행됨");

return super.doStartTag();

}

// 끝 태그 시 실행

@Override

public int doEndTag() throws JspException {

System.out.println("doEndTag() 실행됨");

return super.doEndTag();

}

}


///WEB-INF/tlds/myTag.tld
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd" >
<taglib>
  <tlib-version>1.0</tlib-version>
  <jsp-version>1.2</jsp-version>
  <short-name>myCustomTag</short-name>
  <tag>
    <name>first</name>
    <tag-class>com.sk.customTag.MyCustomTag</tag-class>
  <body-content>empty</body-content> <!--  body 없는 태그 : empty  --> 
  </tag>
</taglib>

//web.xml
 <!-- tld 파일 등록 -->
  <jsp-config>
  <taglib>
  <taglib-uri>http://myCustomTag.com</taglib-uri> <!-- 사용자 지정 -->
  <taglib-location>/WEB-INF/tlds/myTag.tld</taglib-location>
  </taglib>  
  </jsp-config>


//exam16.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://myCustomTag.com" prefix="my" %>
<my:first/>