1. 커스텀태그 라이브러리
1) src/main/webapp/WEB-INF/taglib/customTag.tld
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xml="http://www.w3.org/XML/1998/namespace" 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 ">
<tlib-version>0.0</tlib-version>
<short-name>customTag</short-name>
<uri>http://customTag.com</uri>
<tag>
<name>kangspan</name>
<tag-class>com.kangong.common.taglib.CustomTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>color</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.String</type>
</attribute>
<attribute>
<name>iterNum</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.Integer</type>
</attribute>
</tag>
</taglib>
2) CustomTag.java
package com.kangong.common.taglib;
import java.io.IOException;
import java.io.StringWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class CustomTag extends SimpleTagSupport{
private String color;
private int iterNum;
//doTag()를 오버라이딩 하여 커스텀 태그 처리 내용을 개발한다.
@Override
public void doTag() throws JspException, IOException {
//JspContext는 이 태그를 사용한 JSP 페이지에 대한 정보를 담고 있다.
PageContext context = (PageContext)this.getJspContext();
//태그가 호출된 JSP에 대해 요청 정보를 가져온다.
HttpServletRequest request = (HttpServletRequest)context.getRequest();
System.out.println("태그가 호출된 JSP URI : " + request.getRequestURI());
//Jsp 페이지의 결과를 브라우저로 출력할 출력스트림를 얻는다.
//JSP의 out 객체와 같음.
JspWriter out = context.getOut();
//커스텀 태그의 몸체를 담을 StringWriter
StringWriter stringWriter = new StringWriter();
//태그를 사용할때 지정한 속성값들
String color = this.getColor();
int iterNum = this.getIterNum();
//invoke()가 실행되면 StringWriter의 몸체 텍스트가 담긴다.
getJspBody().invoke(stringWriter);
String bodyText = stringWriter.toString();
out.print("<span style=\" background-color: "+ color + "; \">");
for(int i=0; i < iterNum; i++ ) {
//out으로 내용 출력
out.print("♥" + bodyText + "♥");
}
out.print("</span>");
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getIterNum() {
return iterNum;
}
public void setIterNum(int iterNum) {
this.iterNum = iterNum;
}
}
3) jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ include file="/views/include/include-header.jsp" %>
<%@ taglib prefix="kang" uri="/WEB-INF/taglib/customTag.tld" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Main</title>
</head>
<body>
Hello World!! <br>
<kang:kangspan color="yellow" iterNum="5">
ktw
</kang:kangspan>
<br>
</body>
</html>
2. JSP 태그 라이브러리
1) src/main/webapp/WEB-INF/tags/tospan.tag
<%@ tag language="java" body-content="tagdependent" pageEncoding="UTF-8"%>
<%@ attribute name="color" required="true" %>
<%@ attribute name="iterNum" required="true" type="java.lang.Integer" %>
<%
for(int i=0; i < iterNum; i++){
%>
<span style="background-color: ${color}">
♥<jsp:doBody/>♥
</span>
<%
}
%>
2) JSP
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ include file="/views/include/include-header.jsp" %>
<%@ taglib prefix="kang" uri="/WEB-INF/taglib/customTag.tld" %>
<%@ taglib prefix="kangong" tagdir="/WEB-INF/tags" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Main</title>
</head>
<body>
Hello World!! <br>
<kang:kangspan color="yellow" iterNum="5">
ktw
</kang:kangspan>
<br>
<kangong:tospan color="blueviolet" iterNum="5">
hello
</kangong:tospan>
</body>
</html>
3. JSTL Function
1) src/main/webapp/WEB-INF/taglib/customFunction.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>Out Filter Custom Tag</description>
<tlib-version>1.0</tlib-version>
<short-name>kangfunc</short-name>
<uri>/WEB-INF/tlds/customFunction.tld</uri>
<function>
<name>out</name>
<function-class>com.kangong.common.taglib.CustomFunction</function-class>
<function-signature>java.lang.String filter(java.lang.String)</function-signature>
</function>
<function>
<description>문자열에 님 붙이기</description>
<name>getTest</name>
<function-class>com.kangong.common.taglib.CustomFunction</function-class>
<function-signature>java.lang.String getTest(java.lang.String)</function-signature>
<example>
${kangfunc:getTest(str)}
</example>
</function>
</taglib>
2) Java
package com.kangong.common.taglib;
public class CustomFunction {
public static String filter(String str) {
// return str.replaceAll("\"", "\\\"").replaceAll("\r\n", "");
return str+" ktw 만세!!";
}
public static String getTest(String txt){
return txt + "님";
}
}
3)JSP
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ include file="/views/include/include-header.jsp" %>
<%@ taglib prefix="kang" uri="/WEB-INF/taglib/customTag.tld" %>
<%@ taglib prefix="kangong" tagdir="/WEB-INF/tags" %>
<%@ taglib prefix="kangfunc" uri="/WEB-INF/tlds/customFunction.tld" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Main</title>
</head>
<body>
Hello World!! <br>
<kang:kangspan color="yellow" iterNum="5">
ktw
</kang:kangspan>
<br>
<kangong:tospan color="blueviolet" iterNum="5">
hello
</kangong:tospan>
${kangfunc:out("abc")}; <br>
<br>
${kangfunc:getTest("ktw")}
</body>
</html>
'개발 > Spring Boot' 카테고리의 다른 글
12.webjars 설정 (0) | 2021.02.15 |
---|---|
10.SQL Log 설정 (0) | 2021.02.12 |
9. Annotation 설정 (0) | 2021.02.12 |
8. Spring Security 설정 (0) | 2021.02.12 |
7.aop 설정 (0) | 2021.02.11 |