JSP/Servlet – 태그클래스를 이용해서 애트리뷰트가 있는 커스텀 액션 만들기
태그클래스를 이용해서 애트리뷰트 값을 받으려면 메소드를 따로 선언해야 합니다.
- 반드시 public으로 선언
- 맨 앞에 set을 붙이고 애트리뷰트 이름의 첫문자를 대문자로
- 파라미터 변수를 선언
public void setSize(Integer size){
}
먼저 classes 폴더를 만들고 그 안에 tool 폴더를 만듭니다. 그 다음 태그클래스를 구현하기 위한 NewLineTag.java 파일을 만듭니다.
그리고 TLD파일을 만들어서 커스텀 액션을 등록합니다. web.xml파일에는 TLD파일의 URI와 경로를 명시해줍니다. 마지막으로 TagTest.jsp파일을 만들어서
커스텀 액션을 테스트합니다.
NewLineTag.java
package tool;
import java.io.IOException;
import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class NewLineTag extends SimpleTagSupport{
private int size;
private String color;
//size 애트리뷰트를 받음
public void setSize(Integer size){
this.size = size;
}
//color 애트리뷰르를 받음
public void setColor(String color){
this.color = color;
}
@Override
public void doTag() throws JspException, IOException {
JspContext context = getJspContext();
JspWriter out = context.getOut();
//폰트의 색을 color로 지정후 size 만큼 *을 출력
out.println("<FONT color=" + color + ">");
for(int cnt = 0; cnt < size; cnt++){
out.print("*");
}
out.println("</FONT><BR>");
return;
}
}
tools.tld
<taglib xmlns="http://java.sun.com/xml/ns/javaee" version="2.1">
<tlib-version>1.0</tlib-version>
<short-name>tool</short-name>
<tag>
<name>newLine</name>
<tag-class>tool.NewLineTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>size</name>
<type>java.lang.Integer</type>
</attribute>
<attribute>
<name>color</name>
<type>java.lang.String</type>
</attribute>
</tag>
</taglib>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ch10_jsp_tag_attribute2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<jsp-config>
<taglib>
<taglib-uri>/taglibs/tools.tld</taglib-uri>
<taglib-location>/WEB-INF/tlds/tools.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
TagTest.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@taglib prefix="tool" uri="/taglibs/tools.tld" %>
<!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>
<tool:newLine color="red" size="20"/>
테스트<BR>
<tool:newLine color="blue" size="15"/><BR>
</body>
</html>
실행 화면
제가 명시한 size 와 color대로 커스텀 액션이 제대로 나옴을 알 수 있습니다.
'프로그래밍 > JSP Servlet' 카테고리의 다른 글
JSP/Servlet – 태그 클래스를 이용해서 변수 지원 커스텀 액션 만들기 (0) | 2016.03.01 |
---|---|
JSP/Servlet – 태그 클래스를 이용해서 커스텀 액션의 본체 내용을 바꿔보자 (0) | 2016.03.01 |
JSP/Servlet - 태그 클래스를 이용한 본체가 있는 커스텀 액션 만들기 (0) | 2016.02.29 |
JSP/Servlet – 동적 애트리뷰트를 지원하는 태그 클래스 만들기 (0) | 2016.02.23 |
JSP/Servlet – SimpleTagSupport 클래스를 이용해서 태그 클래스 작성하기 (0) | 2016.02.22 |
JSP/Servlet – 커스텀 액션에 변수를 사용해보자 (0) | 2016.02.22 |
JSP/Servlet – 커스텀 액션에 body를 추가해보자 (0) | 2016.02.21 |
JSP/Servlet – 동적 애트리뷰트를 지원하는 태그 파일 만들기 (0) | 2016.02.18 |