스프링 태그 라이브러리 사용하기

1. pom.xml 추가

<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>

2. java 파일 작성

public class Title extends SimpleTagSupport{

private String title;

public void setTitle(String title) {
this.title = title;
}

@Override
public void doTag() throws JspException, IOException {
// TODO Auto-generated method stub
if(title == null) {
title = “”;
}
String str = “<h3>”+title+”</h3>”;
JspWriter out = getJspContext().getOut();
out.println(str);
super.doTag();
}
}

3. tld 작성

/WEB-INF/tlds 폴더 밑으로 작성함

custom.tld

<?xml version=”1.0″ encoding=”UTF-8″?>
<taglib xmlns=”http://java.sun.com/xml/ns/j2ee”
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”
version=”2.0″>

<description>User Custom Tag Library</description>
<tlib-version>1.0</tlib-version>
<short-name>custom</short-name>
<tag>
<name>title</name>
<tag-class>com.ers.common.Title</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>title</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>

4. web.xml 추가

제일 하단에 jsp-config 추가

<jsp-config>
<taglib>
<taglib-uri>/WEB-INF/tlds/custom.tld</taglib-uri>
<taglib-location>/WEB-INF/tlds/custom.tld</taglib-location>
</taglib>
</jsp-config>

5. 사용하기

<%@ taglib prefix=”custom” uri=”/WE<custom:title title=”마이페이지” />

<custom:title title=”마이페이지” />

와 같이 사용 가능..

0 글이 마음에 드셨다면 하트 꾸욱~

댓글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다