728x90

목차

  1. JSTL(JSP Standard Tag Liberary)
  2. JSTL 설정
  3. JSTL 사용
    • a. core_forEach/if
    • b. fmt

1. JSTL(JSP Standard Tag Liberary)

* 연산이나 if, for 등을 jsp에서 간편하게 사용할 수 있도록 만든 라이브러리
* 가독성이 높아진다.


2.JSTL 설정

* apache-tomcat-8.5.59 > webapps > examples > WEB-INF > lib 에 있는
taglibs-standard-impl-1.2.5.jar 과 taglibs-standard-spec-1.2.5.jar을 복사 해 apache-tomcat-8.5.59 > lib에 붙히기 

taglibs-standard-impl-1.2.5.jar
0.20MB
taglibs-standard-spec-1.2.5.jar
0.04MB


3. JSTL 사용

* <%@ %>를 만들고 ctrl+space를 사용해 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 완성한다. 

a. core : if, forEach 등과 같은 제어문을 사용가능 하게 하는 라이브러리

<c : forEach var="i" begin="1" end="5" step="1">
  // var : 한바퀴 반복할 때 마다 변화되는 begin 값을 갱신
  // begin  : 시작값
  // end : 끝 값
  // step : 증가 값
</c : forEach>

<c : forEach var="v" items="${ array }" varStatus="cnt">
   <li> cnt:${ cnt.index } / count:${ cnt.count } / ${v} </li>
   // ${ cnt.index } : idex를 0번부터 html에 출력
   // ${ cnt.count} : 순번을 1번부터 카운팅해 html에 출력
</c : forEach>

a-1. forEach

<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<%
	List<String> arrayTest = new ArrayList<>();
	arrayTest.add("arr1");
	arrayTest.add("arr2");
	arrayTest.add("arr3");	
	request.setAttribute("arr", arrayTest);
%>    
    
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>Insert title here</title>
	</head>
	<body>
		<ul>
			<c:forEach var="v" items="${ arr }" varStatus="cnt">
				<li> idx : ${cnt.index} / ${v}</li>			
			</c:forEach>
		</ul>
	</body>
</html>

 

 


<c : if test=" ${ condition }">
</c:if>

a-2. for

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>Insert title here</title>
	</head>
	<body>
		<ul>			
 			<c:forEach var="i" begin="1" end="5">
				<c:if test="${i mod 2 eq 0}">
					<li><font color=red>짝수 : ${i} </font></li>
				</c:if>
				
				<c:if test="${i mod 2 eq 1}">
					<li><font color=blue>홀수 : ${i} </font></li>
				</c:if>				
			</c:forEach>			
		</ul>
	</body>
</html>

 

 


b. fmt(format) : 날짜나 숫자형식의 포맷을 변경하는 라이브러리

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

<%
  D
ate today = new Date();
  request.setAttribute("today", today);

  int money = 1000000000;
  request.setAttribute("money", money);
%>

<body>
  <fmt:formatDate value="${ today }" pattern="YYYY년 MM월 dd일"/> 
  <fmt:formatNumber value="${ money }"/>
</body>

 


 

728x90

+ Recent posts