JSTL(JSP Standard Tag Library) 과 EL(Expression Language)


: 커스텀 태그란 개발자가 직접 정의 할 수 있는 태그를 의미합니다.

  일반적인 태그들은 모두 각각의 기능이 있지만 그 기능들로는 자신이

  원하는 기능을 구현 하지 못할수도 있습니다. 커스텀 태그는 현재 존재하는

  태그 외에도 새로운 태그를 정의하여 자신만이 원하는 기능을 구현 할 수 있습니다.

  커스텀 태그는 필요한 기능을 라이브러리 형태로 만들어 두고 사용합니다.

  커스텀 태그를 만들어서 프로그래밍을 한다면 개발 속도 또한 빨라질 것이며

  효율적으로 개발할 수도 있습니다.

  

  JSTL 은 자카르타에서 제공하는 자주 사용되는 필요한 기능들을 모아서 제공하는

  커스텀 태그 라이브러리 입니다.

  JSTL 에 정의 된 기능을 사용해도 프로그램시에 필요한 대부분의 기능을 처리할 수

  있기 때문에 실무에서는 JSTL 의 사용빈도가 높습니다.

  

  JSTL 은 용도에 따라서 사용하는 기능이 달라지는데 크게 4가지로 나뉩니다.

  

  core, function, fmt, xml, sql 로 나누어 지는데 

  core 는 기본적인 기능을 제공합니다.

  

  

  EL 은 표현 언어를 의미합니다. EL 은 JSP 스크립트 태그<%= %> 를 대신하며

  

  JSP 값들을 좀더 편리하게 출력하기 위해서 제공되는 언어 입니다.

  

  예를 들어 <%= hellp %> 는 ${hello} 로 표현됩니다.

  

${test} => ${['test]} => ${["test"]}

${hello.test} => ${hello['test']} =>${hello["test"]}

.EL 의 내장 객체

내장 객체 의미

pageScope Page 영역에 존재하는 객체를 참조할때 사용한다

requestScope Request 영역에 존재하는 객체를 참조할때 사용한다

sessionScope Session 영역에 존재하는 객체를 참조할때 사용한다

addlocationScope Application 영역에 존재하는 객체를 참조할때 사용한다

param 파라미터 값을 얻어 올때 사용한다

paramvalues 파라미터 값을 배열로 얻어 올때 사용한다

header Header 정보를 얻어 올때 사용한다

headerValues Header 정보를 배열로 얻어 올때 사용한다

cookie 쿠키 객체를 참조할때 사용한다

initParan 컨택스트 초기화 객체를 의미한다

pageContext PageContext 객체를 참조할때 사용한다

.JSTL 의 기본 액션 - JSTL core

: JSTL core 는 JSTL 에서 기본 적인 기능(즉 컨트롤에 관련된 기능)들을

 구현해 놓은 라이브러리이다.

 예를 들어 문자열을 출력하거나, 반복문, 조건문과 같은 내용이 core 라이브러리에

 포함되어 있다.

 이 core 라이브러리를 사용하면 커스텀 태그의 장점대로 자바 코드를 사용하지

 않고도 쉽게 기본 기능을 구현 할 수 있다.

 

 형식

 

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

 

 위에서 prifix의 'c' 는 태그를 사용할때 항상 붙는 접두어가 된다.

 

 <c:out/> , <c:set/> 과 같이 'c' 가 붙게 된다.

 

 

 . 출력 태그 : <c:out>

 

 . 변수 설정 및 삭제 태그 : <c:set> , <c:remove>

 

 . 예외처리 태그 : <c:catch>

 

 . 조건처리 태그 : <c:if> , <c:choose> , <c:when> , <c:otherwise>

 

 . 반복 처리 태그 : <c:forEach> , <c:forToken>

 

 . 페이지 처리 태그 : <c:import> , <c:redirect> , <c:url> , <c:param>



1. EL 연산자 사용 Test


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

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>EL 연산자 사용 Test</title>

</head>

<body>


<h3>\${ 5+7 } = ${ 5+7 }</h3>

<h3>\${ 8-3 } = ${ 8-3 }</h3>

<h3>\${ 6*3 } = ${ 6*3 }</h3>

<h3>\${ 9/3 } = ${ 9/3 }</h3>

<h3>\${ 10%3 } = ${ 10%3 }</h3>

<h3>\${ 10==88 } = ${ 10==88 }</h3>

<h3>\${ 5!=7 } = ${ 5!=7 }</h3>

<h3>\${ 55>77 } = ${ 55>77 }</h3>

<h3>\${ 55<77 } = ${ 55<77 }</h3>

<h3>\${ 15 <= 67 } = ${ 15 <= 67 }</h3>

<h3>\${ 47>= 34 } = ${ 47>= 34 }</h3>

<h3>\${ 5+3==8 ? 8:34} = ${ 5+3==8 ? 8:34 }</h3>


</body>

</html>



2. EL 내장 객체 사용


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

    pageEncoding="UTF-8"%>

<%

request.setCharacterEncoding("utf-8");


%>

<%

String name2 = request.getParameter("name");

String tel2 = request.getParameter("tel");

String address2 = request.getParameter("address");


%>


<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>EL 내장 객체 사용 예</title>

</head>

<body>

<h3>${sessionScope.korea}</h3>

<h3>${param.name}</h3>

<h3>${param.tel}</h3>

<h3>${param.address}</h3>

<h3><%=name2 %></h3>

<h3><%=tel2 %></h3>

<h3><%=address2 %></h3>


</body>

</html>

el_2.jsp로 저장



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

    pageEncoding="UTF-8"%>

<%

session.setAttribute("korea","Session Test 2017");

%>


<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>EL 내장 객체 사용</title>

<STYLE>

table{

width: 400px;

margin: auto;

border: 1px solid gray;

text-align: center;

background-color: green;

}


</STYLE>


</head>

<body>

<form action="el_2.jsp" method="post">

<table>

<tr>

<td>이름 : </td>

<td><input type="text" name="name" value="수선화"></td>

</tr>

<tr>

<td>전화번호 : </td>

<td><input type="text" name="tel" value="010-678-3456"></td>

</tr>

<tr>

<td>주소 : </td>

<td><input type="text" name="address" value="부천"></td>

</tr>

<tr>

<td colspan="2" align="center">

<input type="submit" value="입력완료"> &nbsp;&nbsp;

<input type="reset" value="취소"> 

</td>

</tr>

</table>

</form>

</body>

</html>

입력 완료를 클릭




http://tomcat.apache.org/ < 아파치 톰캣에 접속


Taglibs 클릭


Download 클릭


3개 클릭해서 다운로드


3개 복사


firstjsp > WebContent > WEB-INF > lib에 붙여넣기



3. JSTL core 라이브러리 사용 예


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

    pageEncoding="UTF-8"%>

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

    

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>JSTL core 라이브러리 사용 예</title>

</head>

<body>


<c:set value="Hello JSTL 2017" var="test"/>


<h3>&lt;c:set&gt; 사용후 : <c:out value="${test}"/></h3>

<c:remove var="test"/>

<h3>&lt;c:remove&gt; 사용후 : <c:out value="${test}"/></h3>


<c:catch var="error" >

<%= 25 / 0 %>


</c:catch>

<h3>&lt;c:catch&gt; 로 잡아낸 오류 : <c:out value="${error}"/></h3>

<c:if test="${ 5 < 10 }">

<h3> 5 는 10 보다 작다 </h3>

</c:if>

<c:if test="${ 6 + 3 == 9 }">

<h3> 6 + 3 = 9 이다 </h3>

</c:if>

<c:choose>

<c:when test="${ 5 + 10 == 50 }">

<h3> 5 + 10 = 50 이다 </h3>

</c:when>

<c:otherwise>

<h3> 5+10 = 50 이 아니다 </h3>

</c:otherwise>

</c:choose>

</body>

</html>



4. JSTL core 라이브러리 사용 예


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

    pageEncoding="UTF-8"%>

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


<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>JSTL core 라이브러리 사용 예</title>

</head>

<body>


<c:forEach var="test" begin="1" end="33" step="3">

<b> ${test } </b>&nbsp;

</c:forEach>


<br>


<c:forTokens var="alphabet" items="a,b,c,d,e,f,g,h" delims=",">

<b>${alphabet}</b>&nbsp;

</c:forTokens>


<br>


<c:set var="data" value="수선화, 해바라기, 들국화, 매화"/>


<c:forTokens items="${data }" delims="," var="varData">

<b>${varData }</b>&nbsp;


</c:forTokens>


</body>

</html>



.JSTL 의 국제화/형식화 액션 - JSTL fmt

: JSTL fmt 란 국제화/ 형식화의 기능을 제공해 주는 JSTL 라이브러리이다.

 국제화란 다국어 내용을 처리, 형식화는 날짜와 숫자 형식등을 처리하는 것을

 의미 합니다.

 

 형식

 

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



5. JSTL fmt 라이브러리 사용 예

firstjsp > WebContent > WEB-INF > classes > 우클릭 > new > file


name=Hongkildong

say=Hello. I'm Hongkildong.

say2=My friend is {0},{1}.


test.properties로 저장



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

    pageEncoding="UTF-8"%>

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

<jsp:useBean id="date" class="java.util.Date" />

    

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>JSTL fmt 라이브러리 사용 예</title>

</head>

<body>


<fmt:setLocale value="en_US"/>

<!-- setLocale : 다국어 페이지를 사용할때

언어를 지정하는 태그

value : 어떤 언어를 사용할지 지정하는 속성

bundle : properties 확장자를 사용하는

자원화일을 읽어 오는 역활을 한다

 -->


<fmt:bundle basename="test">

<fmt:message key="name"/><br>

<fmt:message key="say"/><br>

<fmt:message key="say2">

<fmt:param value="해바라기"/>

<fmt:param value="들국화"/>

</fmt:message>

</fmt:bundle>


<p>


<fmt:formatNumber value="50000" type="currency"/><br>

<fmt:formatNumber value="0.15" type="percent"/><br>

<fmt:formatNumber value="500567300" pattern="###,###,###"/><br>


<p>

<fmt:formatDate value="${date}" type="date"/><br>

<fmt:formatDate value="${date}" type="time"/><br>

<fmt:formatDate value="${date}" type="both"/><br>


<p>

<fmt:formatDate value="${date}" type="both" timeStyle="short" dateStyle="short"/><br>

<fmt:formatDate value="${date}" type="both" timeStyle="long" dateStyle="long"/><br>


</body>

</html>



6. JSTL sql 라이브러리 사용 예


create table student_1(

num number(5),

name varchar2(20),

primary key(num));



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

    pageEncoding="UTF-8"%>

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

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

    

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>JSTL sql 라이브러리 사용 예</title>

</head>

<body>


<sql:setDataSource

var="conn"

driver="oracle.jdbc.driver.OracleDriver"

url="jdbc:oracle:thin:@localhost:1521:xe"

user="scott"

password="tiger"/>


<sql:update dataSource="${conn}">

INSERT INTO student_1 VALUES(901,'수선화')

</sql:update>

<sql:update dataSource="${conn}">

INSERT INTO student_1 VALUES(902,'수선화')

</sql:update>

<sql:update dataSource="${conn}">

INSERT INTO student_1 VALUES(903,'수선화')

</sql:update>

<sql:update dataSource="${conn}">

INSERT INTO student_1 VALUES(904,'수선화')

</sql:update>


<sql:query var="rs" dataSource="${conn}">


SELECT * FROM student_1 WHERE name=?

<sql:param>수선화</sql:param>

</sql:query>


<c:forEach var="data" items="${rs.rows }">

<c:out value="${data['num'] }"/>

<c:out value="${data.name}"/>

<br>

</c:forEach>


</body>

</html>



7. JSTL XML 라이브러리 사용 예


. JSTL 의 XML 액션


 : JSTL xml 은 XML 문서에서 자주 사용되는 기능을 태그 라이브러리로

   모아놓은 것이다.

   

   XML 에서도 데이타를 표현하거나 제어문을 사용 할때가 많은데 이런 기능들을

   직접 구현하지 않아도 JSTL 에서 바로 사용 할 수 있도록 라이브러리를 제공한다

   

   

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






http://archive.apache.org/dist/xml/xalan-j/xalan-j_2_7_1-bin.zip 다운로드


압축 해제 후 C:\java_2017\java_down\xalan-j_2_7_1-bin\xalan-j_2_7_1 경로에


xalan.jar 를 복사



firstjsp > WebContent > WEB-INF > lib에 붙여넣기



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

    pageEncoding="UTF-8"%>

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

    

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>JSTL XML 라이브러리 사용 예</title>

</head>

<body>


<x:parse var="xmldata">

<students>

<student>

<name>홍길동</name>

<age>25</age>

<gender>남</gender>

<phone>010-3456-22xx</phone>

</student>

<student>

<name>김길동</name>

<age>36</age>

<gender>남</gender>

<phone>010-6756-44xx</phone>

</student>

<student>

<name>홍길순</name>

<age>16</age>

<gender>여</gender>

<phone>없음</phone>

</student>

<student>

<name>김길순</name>

<age>23</age>

<gender>여</gender>

<phone>없음</phone>

</student>

<student>

<name>정길동</name>

<age>44</age>

<gender>남</gender>

<phone>없음</phone>

</student>

</students>

</x:parse>

<x:forEach select="$xmldata//student">

<x:if select="./name!='홍길순'">

<x:out select="./name"/>

<x:set select="./age" var="age"/>

<x:out select="$age"/>

<x:out select="./gender"/>

<x:choose>

<x:when select="./phone!='없음'">

[전화번호: <x:out select="./phone"/>]

</x:when>

<x:otherwise>

[전화가 없어요 ^.^]

</x:otherwise>

</x:choose>

<br>

</x:if>

</x:forEach>

</body>

</html>



8. EL tag 사용


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

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>EL test</title>

</head>

<body>


<h3> EL tag 사용 </h3>


<jsp:useBean id="color" class="java.util.ArrayList">

<%

color.add("red");

color.add("orange");

color.add("green");

color.add("blue");

color.add("#cccc33");

color.add("violet");

%>

</jsp:useBean>


<ul>

<font color="${color[0] }"><li> 이 색상은 ${color[0] } 색 입니다.</li></font>

<font color="${color[1] }"><li> 이 색상은 ${color[1] } 색 입니다.</li></font>

<font color="${color[2] }"><li> 이 색상은 ${color[2] } 색 입니다.</li></font>

<font color="${color[3] }"><li> 이 색상은 ${color[3] } 색 입니다.</li></font>

<font color="${color[4] }"><li> 이 색상은 ${color[4] } 색 입니다.</li></font>

<font color="${color[5] }"><li> 이 색상은 ${color[5] } 색 입니다.</li></font>

</ul>

</body>

</html>

+ Recent posts