JSP/Servlet – 태그 클래스를 이용해서 커스텀 액션의 본체 내용을 바꿔보자
이번에는 getJspBody()메소드를 이용해서 커스텀 액션의 본체를 가져온 다음 내용을 바꿔서 출력을 해봅시다.
JspFragment body = getJspBody();//커스텀 액션의 본체를 가져옵니다.
다음 StringWriter 클래스를 선언합니다. StringReader 클래스와 StringWriter 클래스는 문자열을 스트림에 기록하거나 읽어낼 때 사용하는 클래스입니다.
즉 목표지점이 string형의 데이터가 되는 것입니다. (스트림에 대한 내용 소설같은 자바9 자바스트림 참조)
StringWriter writer = new StringWriter();//객체 생성
body.invoke(write);//본체의 내용을 StringWriter 객체안으로 출력합니다.
이와 같이 해준다음 toString 메소드를 이용해서 String 객체로 변환 시켜주면 본체의 내용을 조작할 수 있습니다.
위와 같이 예제를 구성합니다.
ReplaceTag.java
package tool;
import java.io.IOException;
import java.io.StringWriter;
import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class ReplaceTag extends SimpleTagSupport{
private String oldWord, newWord;
//애트리뷰트 oldWord를 받아서 멤버변수에 저장함
public void setOldWord(String oldWord){
this.oldWord = oldWord;
}
//애트리뷰트 newWord를 받아서 멤버변수에 저장함
public void setNewWord(String newWord){
this.newWord = newWord;
}
@Override
public void doTag() throws JspException, IOException {
// TODO Auto-generated method stub
JspContext context = getJspContext();
JspWriter out = context.getOut();//출력을 위한 것
JspFragment body = getJspBody();//커스텀 액션의 본체를 가져옴
StringWriter writer = new StringWriter();//문자열을 조작하기 위한 선언
body.invoke(writer);//출력에 사용될 출력 스트림을 넘겨줌
String str = writer.toString();
String newStr = str.replaceAll(oldWord, newWord);
out.print(newStr);//출력
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>replace</name>
<tag-class>tool.ReplaceTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>oldWord</name>
<type>java.lang.String</type>
</attribute>
<attribute>
<name>newWord</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_body3</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>
ChangeTest.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:replace oldWord="자동차" newWord="비행기">
자동차를 비행기로 바꿔보자<BR>
</tool:replace>
</body>
</html>
실행 화면
자동차가 비행기로 바뀐 것을 확인 할 수 있습니다.
'프로그래밍 > JSP Servlet' 카테고리의 다른 글
JSP/Servlet - 필터 클래스의 init 메소드와 destroy 메소드 활용 (0) | 2016.03.16 |
---|---|
JSP/Servlet – 필터(Filter)의 이해 (0) | 2016.03.14 |
JSP/Servlet – 커스텀 액션안에 또 다른 커스텀 액션 포함하기 (0) | 2016.03.06 |
JSP/Servlet – 태그 클래스를 이용해서 변수 지원 커스텀 액션 만들기 (0) | 2016.03.01 |
JSP/Servlet - 태그 클래스를 이용한 본체가 있는 커스텀 액션 만들기 (0) | 2016.02.29 |
JSP/Servlet – 동적 애트리뷰트를 지원하는 태그 클래스 만들기 (0) | 2016.02.23 |
JSP/Servlet – 태그클래스를 이용해서 애트리뷰트가 있는 커스텀 액션 만들기 (1) | 2016.02.23 |
JSP/Servlet – SimpleTagSupport 클래스를 이용해서 태그 클래스 작성하기 (0) | 2016.02.22 |