EL (Expression Language)
JSP의 네가지 기본 객체가 제공하는 영역의 속성 사용
${expr}
로 표기
${n+1}
${param.abc}
표현식 예시
<table border="1" style="width:40%;">
<tr>
<th>표현식</th>
<th>값</th>
</tr>
<tr>
<td>\${2 + 5}</td>
<td>${2 + 5}</td>
</tr>
<tr>
<td>\${"10" + 5}</td>
<td>${"10" + 5}</td>
</tr>
<tr>
<td>\${"10" + "5"}</td>
<td>${"10" + "5"}</td>
</tr>
<tr>
<td>\${"십" + 5}</td>
<td>에러발생(EL에서 +는 연산만)</td>
</tr>
<tr>
<td>\${4/5}</td>
<td>${4/5}</td>
</tr>
<tr>
<td>\${2+=5}</td>
<td>${2+=5}(문자열)</td>
</tr>
<tr>
<td>\${"한국"+="서울"}</td>
<td>${"한국"+="서울"}</td>
</tr>
</table>
4가지 기본객체
pagecontext
, request
, session
, application
을 갖고 있다
기본객체 | 설명 |
---|---|
pageContext | JSP의 page 기본 객체와 동일 |
pageScope | pageContext 기본 객체에 저장된 속성의 <속성,값 매핑을 저장한 Map 객체 |
requestScope | request 기본 객체에 저장된 속성의 <속성,값> 매핑을 저장한 Map 객체 |
sessionScope | session 기본 객체에 저장된 속성의 <속성,값> 매핑을 저장한 Map객체 |
applicationScope | application 기본 객체에 저장된 속성의 <속성,값>매핑을 저장한 Map객체 |
param | 요청 파라미터의 <파라미터이름,값> 매핑을 저장한 Map객체 |
paramValues | 요청 정보의 <파라미터이름,값 배열> 매핑을 저장한 Map 객체 |
header | 요청 정보의 <헤더이름,값> 매핑을 저장한 Map 객체 |
headerValues | 요청 정보의 <헤더이름,값 배열> 매핑을 저장한 Map 객체 |
cookie | <쿠키 이름, Cookie> 매핑을 저장한 Map 객체 |
initParam | 초기화 파라미터의 <이름,값> 매핑을 저장한 Map 객체 |
메서드 | 반환형 | 설명 |
---|---|---|
setAttribute(String name, Object value) | void | 이름이 name인 속성의 값을 value로 지정한다. |
getAttribute(String name) | Object | 이름이 name인 속성의 값을 구한다. 존재하지 않을 경우 null을 반환한다. |
removeAttribute(String name) | void | 이름이 name인 속성을 삭제한다. |
getAttributeNames() | Enumeration | 속성의 이름 목록을 구한다. (pageContext 제외) |
<body>
<%
String str = "여행";
//JSP 4개 영역에 데이터 저장
pageContext.setAttribute("msg1", "봄");
request.setAttribute("msg2", "여름");
session.setAttribute("msg3", "가을");
application.setAttribute("msg4", "겨울");
%>
str : <%= str %> <br>
str(el) : ${str} <br>
page scope(영역) : ${pageScope.msg1},${msg1}<br>
request scope(영역) : ${requestScope.msg2},${msg2}<br>
session scope(영역) : ${sessionScope.msg3},${msg3}<br>
application scope(영역) : ${applicationScope.msg4},${msg4}
</body>
str : 여행
str(el) :
page scope(영역) : 봄,봄
request scope(영역) : 여름,여름
session scope(영역) : 가을,가을
application scope(영역) : 겨울,겨울
str(el) :
에 값이 출력 되지않은 이유는 scope(영역)에 참조 할 수 없기 때문이다.
EL 태그가 값을 가져올수 있는 영역은
pagecontext
, request
, session
, application
이므로
pageContext.setAttribute("str1", str1);
//request.setAttribute("str1", str1);
//session.setAttribute("str1", str1);
//application.setAttribute("str1", str1);
이 중 하나로 수정 하면 해결 된다.
JSTL (JavaServer Pages Standard Tag Library)
자바서버 페이지 표준 태그 라이브러리
jsp 페이지에 core 라이브러리를 사용할 수 있도록 taglib 디렉티브 명시
<% @taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
지정된 변수에 값을 할당하는 태그
<c:set var="변수명" value="설정값" target="객체" property="값" scope="범위" />
설정된 속성을 제거하는 태그
<c:remove var="변수명" scope="범위" />
지정된 값을 출력하는 태그
<c:out value="출력값" default="기본값" escapeXml="true/false" />
조건이 true이면 수행문을 수행하는 태그
<c:if test="조건" var="변수명" scope="범위" />
여러개의 when 태그에 명시한 조건을 체크하고 조건에 만족하는 수행문을 수행, 만족하는 조건이 없으면 otherwise에 명시한 수행문을 수행
<c:choose>
<c:when test="조건"></c:when>
<c:when test="조건"></c:when>
<c:otherwise></c:otherwise>
</c:choose>
수행문을 반복해서 수행
<c:forEach items="객체명" begin="시작 인덱스" end="끝 인덱스" step="증감식"
var="변수명" varStatus="상태변수" />
- items : 속성에 인덱스가 존재하는 객체를 지정하여 반복수행 할 때 사용
- begin : 수행문을 반복시킬 시작 인덱스 지정
- end : 수행문의 반복이 끝날 인덱스 지정
- step : 증감식 지정
- var : 현재 반복하고 있는 값이 저장되는 변수지정
- varStatus : 반복 상태를 지정하는 변수 지정