프로그래밍/JSP Servlet

JSP/Servlet – 애트리뷰트를 지원하는 태그 파일 만들기

가카리 2016. 2. 18. 21:15
반응형

 

JSP/Servlet – 애트리뷰트를 지원하는 태그 파일 만들기

 


2016/02/14 - [프로그래밍/JSP/Servlet] - JSP/Servlet – 태그 파일을 이용해서 커스텀 액션 만드는 방법



<util:newLine color="blue" size="20" />

 

위처럼 애트리뷰트를 가지는 커스텀액션을 만들기 위해서는 attribute 지시자를 사용해야 합니다.

 

     <%@attribute name="애트리뷰트 이름" %>

 

태그파일로 전달된 애트리뷰트는 다음과 같이 사용합니다.

 

    <%= 애트리뷰트 이름 %> or ${애트리뷰트 이름 }

 

type 애트리뷰트를 통해서 거기에 원하는 데이터 타입을 지정할 수 있습니다.

 

    <%@attribute name="애트리뷰트 이름" type="java.lang.Integer" %>

 

WEB-INF폴더 아래에 tags라는 폴더를 만들고 태그파일을 생성합니다.

 

newLine.tag

 

<%@tag body-content="empty" %>

<%@attribute name="color" %>

<%@attribute name="size" type="java.lang.Integer" %>

<FONT color=${color }>

<%

    for(int cnt = 0; cnt < size; cnt++)

        out.print("-");

%>

</FONT><BR>

 

TagTest.jsp

 

<%@ page language="java" contentType="text/html; charset=EUC-KR"

pageEncoding="EUC-KR"%>

<%@taglib prefix="util" tagdir="/WEB-INF/tags" %>

<!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=EUC-KR">

<title>Insert title here</title>

</head>

<body>

    <util:newLine color="blue" size="20"/>

    테스트<BR>

    <util:newLine color="red" size="25"/>

</body>

</html>

 

 

실행 화면

다음과 같이 size와 color옵션을 적용한 대로 잘 나오네요.

반응형