JSP의 기본 문법을 보완하기 위해 만들어진 표현 언어.
서버에서 오는 데이터를 받아 사용할 때 사용 (4개의 영역에 들어있는 변수에만 사용 가능)
${ }형태 (JS의 백틱처럼 사용, JQuery와 같은 $사용)
${num1} + ${num2} = ${num1+num2}
EL 문법 무시 지시자 : <%@ page isELIgnored = "true" %>
EL 문법 무시 web.xml 설정 :
<web-app>
<jsp-config>
<jsp-property-group>
<url-pattern>/ignoredEl/*</url-pattern>
<el-ignored>true</el-ignored>
</jsp-property-group>
</jsp-config>
</web-app>
● 수치연산자
+ : 덧셈, - : 뺄셈, * : 곱셈, / 또는 div : 나눗셈, % 또는 mod : 나머지
${null + 1} = ${0+1}, ${"10" + 1} = ${10 + 1}
● 비교연산자 (l : little, g : greater)
== 또는 eq, != 또는 ne, < 또는 lt, > 또는 gt, <= 또는 le, <= 또는 ge,
● 논리연산자
&& 또는 and, || 또는 or, ! 또는 not
● empty 연산자
${empty param.name}
● EL의 기본객체
pageContext | page 객체와 동일 |
pageScope | pageContext 객체에 저장된 속성의 <이름, 값> Map 객체 = pageContext.getAttribute(이름) |
requestScope | request 객체에 저장된 속성의 <이름, 값> Map 객체 = request.getAttribute(이름) |
sessionScope | session 객체에 저장된 속성의 <이름, 값> Map 객체 = session.getAttribute(이름) |
applicationScope | application 객체에 저장된 속성의 <이름값> Map 객체 = application.getAttribute(이름) |
param | 요청 파라미터의 <이름, 값> Map 객체 = request.getParameter(이름) |
paramValues | 요청 파라미터의 <이름, 값 배열> Map 객체 = request.getParameterValues(이름) |
header | 요청정보의 헤더를 저장한 Map 객체 |
cookie | 쿠키정보를 저장한 Map 객체 |
ex) Scope
- 같은 변수 사용시 우선순위 : page > request > session > application
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
pageContext.setAttribute("p1", "page scope value");
request.setAttribute("r1", "request scope value");
session.setAttribute("s1", "session scope value");
application.setAttribute("a1", "application scope value");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%=pageContext.getAttribute("p1")%> = ${pageScope.p1}<br> <!-- page scope value -->
<%=request.getAttribute("r1")%> = ${requestScope.r1}<br> <!-- request scope value -->
<%=session.getAttribute("s1")%> = ${sessionScope.s1}<br> <!-- session scope value -->
<%=application.getAttribute("a1")%> = ${applicationScope.a1}<br> <!-- application scope value -->
</body>
</html>
ex) param
<html>
<body>
<form method="post" action="getServlet">
name : <input name="name">
birthday : <input name="birthday">
hobby : 독서 <input type="checkbox" name="hobby" value="독서">
여행 <input type="checkbox" name="hobby" value="여행">
게임 <input type="checkbox" name="hobby" value="게임">
운동 <input type="checkbox" name="hobby" value="운동">
<input type="submit" value="제출하기">
</form>
</body>
</html>
<html>
<body>
name : ${param.name}
birthday : ${param['birthday']}
hobby : ${paramValues.hobby[0]}
${paramValues.hobby[1]}
${paramValues.hobby[2]}
${paramValues.hobby[3]}
</body>
</html>
● EL을 이용한 자바빈즈
${bean이름.변수이름}
-> 변수이름을 사용하면 그 프로퍼티의 매개변수가 없는 getter를 호출하는 것과 같음. ex) bean.name = bean.getName()
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<jsp:useBean id="bean" class="ELBean.ELBean" />
<jsp:setProperty property="siteName" name="bean"/>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>EL의 Beans</h2>
빈즈액션 태그<br>
사이트명 : <jsp:getProperty property="siteName" name="bean"/><p>
EL Beans<br>
사이트명 : ${bean.siteName} = ${bean.getSiteName()}
</body>
</html>
● EL을 이용한 클래스/인스턴스 호출
- value에 인스턴스를 넣어서 key값을 이용해 value 인스턴스의 메서드에 접근함 ex) key.메서드()
<%@page import="el.Themometer"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
Themometer thermometer = new Themometer();
request.setAttribute("t", thermometer);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>온도 변환 예제</title>
</head>
<body>
${t.selCelsius("서울", 27.3)}
서울 온도 : 섭씨 ${t.getCelsius("서울")}도 / 화씨 ${t.getFahrenheit("서울")}
<br>
정보 : ${t.info} <!-- t.info = t.getInfo() -->
</body>
</html>
package el;
import java.util.HashMap;
import java.util.Map;
public class Themometer {
private Map<String, Double> locationCelsiusMap = new HashMap<String, Double>();
public void selCelsius(String location, Double value) {
locationCelsiusMap.put(location, value);
}
public Double getCelsius(String location) {
return locationCelsiusMap.get(location);
}
public Double getFahrenheit(String location) {
Double celsius = getCelsius(location);
if (celsius == null) {
return null;
}
return celsius.doubleValue() * 1.8 + 32.0;
}
public String getInfo() {
return "온도계 변환기 1.1";
}
}
'Web > JSP&Servlet' 카테고리의 다른 글
Scope (0) | 2020.04.23 |
---|---|
JSTL(JSP Standard Tag Library) (0) | 2020.04.23 |
Cookie와 Session (0) | 2020.04.08 |
[JSP]지시자 (0) | 2020.04.08 |
[Servlet]생명 주기 (0) | 2020.04.08 |