728x90

목차

  1. EL(Expression Language)
  2. EL표현식_매개변수 받기
  3. EL표현식과 map&list

1. EL(Expression Language)

* JSP에서 사용되는 표현식을 더 간결하게 사용하기 위한 표현 언어
* JSP 내장 객체인 4개의 Scope(page, request, session, application)에 존재하는 데이터만 EL표현식으로 사용가능. 생략이 가능하다. (영역참조 순서 page > request > session > application)
* 스크립트릿(scriptlet)(<% %>)에서 변수를 선언하고 ${ } 로 html body태그 내에서 호출 가능
* ' ${ } ' 괄호 안에서 산술연산자, 비교연산자, 삼항연산자, 논리연산자를 사용할 수 있다. 

pageScope 현재 페이지에서만 EL표기법으로 사용가능
requestScope 페이지가 닫히면 종료된다. 최대 두개의 페이지에서 값을 공유할 수 있음. 지역변수 개념과 유사
sessionScope 브라우저가 완전히 종료되기 전까지 스코프의 변수가 살아있다. 하나의 웹브라우저 내에서 값을 공유. 전역변수 개념과 유사
applicationScope 하나의 웹 프로젝트에서 값을 공유

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<%	
	String el = "ELTest!";	
	pageContext.setAttribute("el", el);
    
	pageContext.setAttribute("el2", "pageScope");
	request.setAttribute("rel", "requestScope");
	session.setAttribute("sel", "sessionScope");
%>        

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>Insert title here</title>
	</head>	
	<body>
		JSP표현식 : <%= el %><br><br>		
		pageScope : ${ pageScope.el2 }<br>	
		생략식 : ${ el } <br>
		생략식 : ${ el2 } <br><br>
		requestScope : ${ requestScope.rel }<br>
		생략식 : ${ rel }<br><br>
		sessionScope : ${ sessionScope.sel }<br>
		생략식 : ${ sel }<br><br>	
	</body>
</html>

2. EL표현식_매개변수 받기

* ${ param. *** } 으로 넘어온 매개변수를 받을 수 있다.

<body>
	parameter : ${ param.parameter }
<body>

 


3. EL표현식과 map&list

* map, list는 Scope영역 중 한군데는 담겨져 있어야 EL표현식을 통해 body에 출력 가능

<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="vo.PersonVO"%>
<%@page import="java.util.HashMap"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<%
	//EL표기법을 통한 map구조 출력
	HashMap<String, Integer> map = new HashMap<>();
	map.put("test1", 1);
	request.setAttribute("myMap", map);
	
	//VO에 담긴 값을 EL을 통해 출력하기
	PersonVO vo1 = new PersonVO("사나짱", 23);
	PersonVO vo2 = new PersonVO("미나리", 23);
	request.setAttribute("vo1", vo1);
	request.setAttribute("vo2", vo2);
	
	//VO에 담긴 값을 배열에 넣고 EL을 통해 출력하기
	List<PersonVO> arr = new ArrayList<>();
	arr.add(vo1);
	arr.add(vo2);
	request.setAttribute("list", arr);
%>

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>Insert title here</title>
	</head>
	<body>
	 	test1 : ${ requestScope.myMap.get("test1") }<br>
		test1(생략) : ${ myMap.test1 }<br>
		<hr>
		 이름 : ${ vo1.name }<br>
		 나이 : ${ vo1.age }<br>
		<hr>
		<%= arr.get(0).getName() %>
		<%= arr.get(0).getAge() %>
		<hr>
		${ list[1].name }<br> 
		${ list[1].age }<br>	 
	</body>
</html>

 

728x90

+ Recent posts