SSENI's
search
sseni
말하는 감자에서 자라기
Today
Yesterday
[웹서버프로그래밍] 2022.05.10 커스텀 태그, JSTL 태그
# 커스텀 태그
커스텀 태그 라이브러리를 사용했을 때 >
1. 비즈니스 로직으로부터 화면 표현을 분리할 수 있음
2. 비즈니스 로직의 캡슐화
3. 보다 완벽한 MVC 패턴 구현 가능
# 태그의 기본 구조
<H2>custom tag test</H2>
<form name=“form1” method=“post”>
<input type=“text” name=“item1”>
</form>
1. 태그
기본적으로, Pair로 되어있음 (input 제외)
2. 속성
<form> 태그 내에 있는 name 과 method
<input> 태그 내에 있는 type 과 name 등이 속성에 해당
3. 태그 바디
시작 태그와 종료 태그 사이에 있는 내용
# taglib 지시어
<%@ taglib uri=“/WEB-INF/tld/MsgTag.tld” prefix=“mytag” %>
커스텀 태그를 사용하기 위해 원하는 jsp에 taglib 지시어 기술
uri : tld(tag library descripter) 파일 위치 지정
prefix : 한 페이지에 커스텀 태그를 사용할 경우 prefix를 이용하면 혼동을 피할 수 있음

[실습1] 커스텀 태그 테스트
- printTagTest.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib tagdir="/WEB-INF/tags" prefix="mytag" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ch10 : 태그 파일 예제-PirntTagTest</title>
</head>
<body>
<div align="center">
<H2>ch10 : 태그 파일 예제-PrintTagTest</H2>
<HR>
<I><mytag:print/></I>
</div>
</body>
</html>
- print.tag
<%@ tag language="java" pageEncoding="UTF-8"%>
커스텀 태그 출력 메시지: Hello!!

[실습2] 커스텀 태그 : 복합 태그 구현
- item.tag
<%@ tag body-content="scriptless" pageEncoding="UTF-8"%>
<%@ attribute name="bgcolor" %>
<%@ attribute name="border" %>
<jsp:useBean id="product" class="jspbook.Product"/>
<H2><jsp:doBody/></H2>
<table border="${border}" bgcolor="${bgcolor}" width="150">
<%
for(String item : product.getProductList()) {
out.println("<tr><td>"+item+"</td></tr>");
}
%>
</table>
- ItemTagTest.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/tld/ItemTag.tld" prefix="mytag" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ch10 : 태그파일 예제-ItemTagTest</title>
</head>
<body>
<div align="center">
<H2>ch10 : 태그파일 예제-ItemTagTest</H2>
<HR>
<mytag:item border="3" bgcolor="yellow">상품목록</mytag:item>
</div>
</body>
</html>
- ListItem.tag
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="mytag" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"">
<title>ch10 : 태그파일 예제-ListItemt</title>
</head>
<body>
<div align="center">
<H2>ch10 : 태그파일 예제-ListItem</H2>
<HR>
<mytag:item border="10" bgcolor="red">재고현황</mytag:item>
</div>
</body>
</html>
- Product.java
package jspbook;
public class Product {
// 상품 목록을 보관할 배열
private String[] productList = {"item1","item2","item3","item4","item5"};
// 웹 테스트를 위한 변수값
private int num1 = 10;
private int num2 = 20;
public int getNum1() {
return num1;
}
public int getNum2() {
return num2;
}
public String[] getProductList() {
return productList;
}
}
- ItemTagHandler.java
package jspbook;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class ItemTagHandler extends SimpleTagSupport {
// 태그 속성 처리를 위한 멤버변수
private String bgcolor;
private String border;
// 시작 태그를 만나면 호출되는 메서드
public void doTag() throws IOException, JspException {
JspWriter out = getJspContext().getOut();
// 태그 바디 처리
JspFragment body = getJspBody();
// 스트링버퍼를 이용해 상품정보 출력을 위한 태그 구성
Product product = new Product();
StringBuffer msg = new StringBuffer();
if(body != null){
out.println("<H2>");
body.invoke(null);
out.println("</H2>");
}
msg.append("<table border=")
.append(border)
.append(" bgcolor=")
.append(bgcolor)
.append(" width=150>");
out.println(msg.toString());
// 상품 목록 출력
for(String item : product.getProductList()) {
out.println("<tr><td>"+item+"</td></tr>");
}
out.println("</table>");
}
public String getBgcolor() {
return bgcolor;
}
public void setBgcolor(String bgcolor) {
this.bgcolor = bgcolor;
}
public String getBorder() {
return border;
}
public void setBorder(String border) {
this.border = border;
}
}


[실습3] 커스텀 태그 예제 MsgTagTest
- MsgTagSimpleTest.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ taglib uri="/WEB-INF/tld/MsgTagSimple.tld" prefix="mytag" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>ch10 : 커스텀 태그 예제-MsgTagTest</title>
</head>
<body>
<div align="center">
<H2>ch10 : 커스텀 태그 예제-MsgTagTest</H2>
<HR>
<I><mytag:print/></I>
</div>
</body>
</html>
- MsgTagSimpleHandler.java
package jspbook;
import java.io.IOException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class MsgTagSimpleHandler extends SimpleTagSupport {
// 시작 태그를 만나면 호출되는 메서드
public void doTag() throws IOException {
JspWriter out = getJspContext().getOut();
out.println("커스텀 태그 출력 메시지: Hello!!");
}
}

** javax.servlet 오류 시 jakarta로 수정
[실습4] catch
- catch.jsp
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>ch11 : catch</title>
</head>
<body bgcolor="#FFFFFF">
<h3><c:catch></h3>
<c:catch var="errMsg">
<%=9/0 %>
</c:catch>
error message : ${errMsg}
</body>
</html>

++ for
| [웹서버프로그래밍] 2022.05.24 리스너와 필터, Django 셋업 (0) | 2022.05.24 |
|---|---|
| [웹서버프로그래밍] 2022.05.17 JSTL 태그 실습, DB 커넥션 풀과 트랜잭션, 초기화 매개변수 (0) | 2022.05.17 |
| [웹서버프로그래밍] 2022.05.03 database connections, 표현 언어 (0) | 2022.05.03 |
| [웹서버프로그래밍] 2022.04.19 데이터베이스와 JDBC (0) | 2022.04.19 |
| [웹서버프로그래밍] 2022.04.12 Beans 클래스, mysql 연동 (0) | 2022.04.12 |