IT 전용글/JSP

CustomTag (Not Struts CustomTag) 설정방법.

회상형인간 2009. 1. 30. 01:26


1. (보통) /WEB-INF/tld/  폴더에 tld 파일을 생성해야한다.
    쏘스 예시)
         

 <tag>
  <name>message</name>
  <tagclass>simple.HelloWorldMessage</tagclass>
  <bodycontent>empty</bodycontent>
  // 특이점. attribute 에 메세지란 키값으로 넘어온 값을 처리한다.
  <attribute>
   <name>message</name>
   <required>true</required>
  </attribute>
 </tag>

</taglib>


 


 

2. 클래스 파일을 생성한다.

클래스 파일은 TagSupport 를 슈퍼 클래스로 상속 받아야한다.

상속 받은후  필요에 따라서

doStartTag()  ,doBodyTag() , doEndTag()   메쏘드들을 오버라이딩 해서 사용한다.

쏘스 예시)

private String message;
 
 
 
 public String getMessage() {
  return message;
 }

 public void setMessage(String message) {
  this.message = message;
 }

@Override
 public int doStartTag() throws JspException {
  // TODO Auto-generated method stub
  JspWriter out = this.pageContext.getOut();
  
  try {
   out.println(this.message+"님 안녕하셔요 ");
  } catch (IOException e) {
   throw new JspException(e.getMessage());
  }
  
  
  return this.SKIP_BODY;
 }
 
 @Override
 public int doEndTag() throws JspException {
  // TODO Auto-generated method stub
  
  return this.EVAL_PAGE;
 }

3. 사용하려는 페이지 상단에

<%@taglib prefix="ljkd" uri="http://www.choongang.co.kr/jsp/simpleTag" %>

해당 코드를 삽입후

<ljkd:message message="이종근" />

를 삽입한다

실행 결과 = 이종근님 안녕하세요 가 된다.


실제로 커스텀 태그를 배운 이유의 대부분은 스트럿츠 커스텀 태그도 이와 비슷한 방식으로 구현되어있기 때문이다.













 

'IT 전용글 > JSP' 카테고리의 다른 글

JSP 멀티 업로드  (0) 2009.02.10
Exception 처리 방식  (0) 2009.01.30
국제화 .  (0) 2009.01.30
JSTL , CustomTag , 국제화  (0) 2009.01.29
JSP 요청 헤더 값 구하는 방법..  (0) 2009.01.28