본문 바로가기
05_Server (04. JSP 프로그래밍 구현)

Formatting Library, Function Library

by moca7 2024. 10. 8.

 

 

ㅁ JSTL의 대표적인 라이브러리 종류

- jstl 내에서도 라이브러리 종류가 나뉜다. 태그를 제공하는 라이브러리들이다.

- Core Library : 변수와 제어문 등의 로직과 관련된 문법 제공 (매우 중요)

- Formatting Library : 숫자, 날짜 및 시간 데이터의 출력 형식관련 문법 제공

- Function Library : EL 구문 내에서 사용 가능한 함수 제공

 

- JSTL은 EL 구문과 함께 쓰는거라 세트로 불려진다. EL/JSTL

 

 

 

 

ㅁ index.jsp

 

 
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>


    <h3>EL</h3>
    <a href="<%= request.getContextPath() %>/el.do">1) EL 기본 구문</a> <br>
    <a href="<%= request.getContextPath() %>/elOperator.do">2) EL 연산자</a>


    <h3>JSP Action Tag</h3>

    <h4>1. 표준 액션 태그 (Standard Action Tag)</h4>
    <a href="<%= request.getContextPath() %>/02_standardAction/include.jsp">1) jsp:include</a> <br>
    <a href="<%= request.getContextPath() %>/02_standardAction/forward.jsp">2) jsp:forward</a>

    <h4>2. 커스텀 액션 태그 (JSTL)</h4>
    <a href="<%= request.getContextPath() %>/03_customAction/core.jsp">1) JSTL Core Library ****</a><br>
    <a href="${ pageContext.request.contextPath }/03_customAction/fmt.jsp">2) JSTL Formatting Library</a><br>
    <a href="">3) JSTL Function Library</a><br>


</body>
</html>
 

 

 

 

ㅁ fmt.jsp

 

 
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>    
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>    

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

    <h2>JSTL Formatting Library</h2>

    <h3>1. 숫자데이터 포맷(형식) 지정</h3>

</body>
</html>
 

 

 

- 상단에 taglib 지시어로 선언해야 한다.

- 코어 라이브러리도 쓸거라 taglib 지시어로 선언해 둔다. 

- 사용할 라이브러리들을 전부 taglib 지시어로 선언해둬야 한다. 

 

 

 

 

 

 

ㅁ <fmt:formatNumber>

 

- 숫자데이터의 출력형식을 지정하는 구문

- 통화기호, 백분율 등 쓰임에 맞게 지정 가능

 

<fmt:formatNumber value="" [groupingUsed=""] [type=""] [currencySymbol=""]/>

 

 

- value : 출력시킬 숫자 데이터 (숫자 아닌 값 제시시 NumberFormatException 발생) (내부적으로 parsing 변환 시킴)

- groupingUsed : 세자리마다 구분자(,) 표시 여부 지정           // true - 구분자 O 생략시 기본값,  false - 구분자 X

- type : 출력형식 지정                                                              // percent - 백분율 (%),  currency - 통화기호 (\) 

- currencySymbol : 통화기호문자 지정. 생략시 현재 로케일(지역)에 맞는 통화기호.

 

 

 

 
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>    
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>    

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

    <h2>JSTL Formatting Library</h2>

    <h3>1. 숫자데이터 포맷(형식) 지정</h3>

    <c:set var="num1" value="123456789"></c:set>
    <c:set var="num2" value="0.75" />
    <c:set var="num3" value="50000" />

    num1값(그냥출력) : ${ num1 } <br>
    num1값(구분자O) : <fmt:formatNumber value="${ num1 }" /> <br>

    num2값(백분율) : <fmt:formatNumber value="${ num2 }" type="percent" /> <br>

    num3값(원화 통화기호, 구분자X) : <fmt:formatNumber value="${ num3 }" type="currency" groupingUsed="false" /> <br>
    num3값(달러 통화기호, 구분자O) : <fmt:formatNumber value="${ num3 }" type="currency" currencySymbol="$" /> <br>



</body>
</html>
 

 

 

 

 

 

==============================================================================

 

 

ㅁ <fmt:formatDate>

- 날짜 및 시간 데이터의 출력형식을 지정하는 구문

- 기본적으로 제공하는 포맷의 종류가 다양함. 본인이 원하는 형식으로 지정해서 출력도 가능하다.

 

<fmt:formatDate value="" [type=""] [dateStyle=""] [timeStyle=""] [pattern=""] />

 

- value : 출력시킬 날짜 및 시간데이터 (단, 반드시 java.util.Date 객체여야 함)

- type : 날짜 및 시간 중 출력할 타입을 지정   //   date(날짜만, 기본값), time(시간만), both

- dateStyle, timeStyle : 기본적으로 제공하고 있는 날짜 및 시간 스타일을 지정   // full, long, medium(기본값), short

- pattern : 기본적으로 제공하는 스타일 중에 원하는 스타일이 없을 경우 따로 패턴 제시 가능

 

 

 

 

 

 
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>    
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>    

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

    <h2>JSTL Formatting Library</h2>
 

    <c:set var="current" value="<%= new java.util.Date() %>" />

    <ul>
        <li>현재 날짜 : <fmt:formatDate value="${ current }" /> </li>
        <li>현재 시간 : <fmt:formatDate value="${ current }" type="time" /> </li>
        <li>현재 날짜 및 시간 : <fmt:formatDate value="${ current }" type="both" /> </li>
        <li>medium : <fmt:formatDate value="${ current }" type="both" dateStyle="medium" timeStyle="medium"/> </li>
        <li>long : <fmt:formatDate value="${ current }" type="both" dateStyle="long" timeStyle="long"/> </li>
        <li>full : <fmt:formatDate value="${ current }" type="both" dateStyle="full" timeStyle="full"/> </li>
        <li>short : <fmt:formatDate value="${ current }" type="both" dateStyle="short" timeStyle="short"/> </li>
        <li>내패턴 : <fmt:formatDate value="${ current }" type="both" pattern="yyyy-MM-dd(E) hh:mm:ss(a)" /> </li>
    </ul>



</body>
</html>
 
 

 

 

- 날짜와 시간 스타일을 다르게 줘도 된다. 

- 괄호안의 E는 요일, a는 오전/오후를 의미한다. 

- 소문자 hh는 12시간 형식이다. HH는 24시간 형식이다.

 

 

 

 

===========================================================================

 

 

ㅁ Function Library

 

 

 

ㅁ index.jsp

 

 
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>



    <h3>EL</h3>
    <a href="<%= request.getContextPath() %>/el.do">1) EL 기본 구문</a> <br>
    <a href="<%= request.getContextPath() %>/elOperator.do">2) EL 연산자</a>


    <h3>JSP Action Tag</h3>

    <h4>1. 표준 액션 태그 (Standard Action Tag)</h4>
    <a href="<%= request.getContextPath() %>/02_standardAction/include.jsp">1) jsp:include</a> <br>
    <a href="<%= request.getContextPath() %>/02_standardAction/forward.jsp">2) jsp:forward</a>

    <h4>2. 커스텀 액션 태그 (JSTL)</h4>
    <a href="<%= request.getContextPath() %>/03_customAction/core.jsp">1) JSTL Core Library ****</a><br>
    <a href="${ pageContext.request.contextPath }/03_customAction/fmt.jsp">2) JSTL Formatting Library</a><br>
    <a href="${ pageContext.request.contextPath }/03_customAction/fn.jsp">3) JSTL Function Library</a><br>


</body>
</html>
 

 

 

 

 

ㅁ fn.jsp

 

 
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

    <h2>JSTL Functions Library</h2>

</body>
</html>
 

 

- functions library는 태그로 작성하는게 아니고 el 구문 내에 쓰는 라이브러리다.

- 사용을 위해서는 taglib 지시어가 필요하다.

- 여기서도 변수 세팅을 위해서 코어 라이브러리를 사용한다.

 

- EL이든 JSTL이든 자바코드를 모르는 사람을 위한 것이다. 

 

 

 

 

ㅁ 

 

 
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

    <h2>JSTL Functions Library</h2>

    <c:set var="str" value="How Are You?" />

    str값 : ${ str } <br><br>

    문자열의 길이 : ${ str.length() } 또는 ${ fn:length(str) } <br>

    모두 대문자로 출력 : ${ str.toUpperCase() } 또는 ${ fn:toUpperCase(str) } <br>
    모두 소문자로 출력 : ${ str.toLowerCase() } 또는 ${ fn:toLowerCase(str) } <br><br>

    Are의 시작인덱스 : ${ str.indexOf("Are") } 또는 ${ fn:indexOf(str, "Are") } <br>
    Are => Were 변경 : ${ str.replace("Are", "Were") } 또는 ${ fn:replace(str, "Are", "Were") } <br>
    문자열 추출 : ${ str.substring(1, 5) } 또는 ${ fn:substring(str, 1, 5) } <br><br>



    <c:if test="${ fn:contains(str, 'Are') }">
        <b>포함되어있음</b> <br>
    </c:if>

    <c:if test="${ fn:containsIgnoreCase(str, 'are') }">
        <b>포함되어있음</b> <br>
    </c:if>


</body>
</html>
 

 

 

- 사실 EL 구문 내에서도 자바에서의 메소드 호출 가능하긴 하다. 그래도 최대한 안쓰는 방향으로 한다.

- 문자열뿐 아니라 자바의 list, 배열도 fn:length() 함수 가능하다.

list의 size() 또는 배열의 length가 출력된다.

 

- EL 내에서 문자열에 쌍따옴표, 홑따옴표 상관 없다.

 

 

- <c:if> 태그의 조건식 안에는 EL 구문을 써야 한다.

 

 

'05_Server (04. JSP 프로그래밍 구현)' 카테고리의 다른 글

JSP Action Tag  (3) 2024.10.07
[EL&JSTL] EL  (2) 2024.10.07
[AJAX] 1. ajax를 이용한 비동기식 요청  (0) 2024.09.29
실제 프로젝트 작업  (1) 2024.09.22
JSP/SERVLET 정리  (2) 2024.09.21