본문 바로가기
IT/JSP

[JSP] - 32. EL(Expression Language) 표현식 문법 및 사용방법

by 차이나는 개발자 2020. 8. 10.
728x90
반응형

#EL(Expression Language) 표현식 문법 및 사용방법

 

 

#데이터 추출 표현식

${ }

 

#EL의 범위
pageScope
requestScope
sessionScope
applicationScope

 

예제 1)

 

1. forward.jsp

<%@page import="kr.ac.green.Dummy"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import="java.util.*" %>    
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<%
		request.setAttribute("emptyArr", new Vector<String>().toArray());
	
		Dummy d = new Dummy();
		d.setNum(100); 
		d.setTitle("newTitle");
	
		Map<String, String> map = new Hashtable<String, String>();
		map.put("key1", "value1");
		map.put("key2", "value2");
		map.put("key3", "value3");
		map.put("key4", "value4");
		
		Vector<String> list = new Vector<String>();
		list.add("a");
		list.add("b");
		list.add("c");
		list.add("d");
		
		request.setAttribute("map", map);
		request.setAttribute("list", list);
		request.setAttribute("arr", new int[]{1,2,3,4,5});
		request.setAttribute("dummy", d);
		request.setAttribute("some", "other");
		session.setAttribute("yourName", "james");
	%>
	<jsp:forward page="el_ex.jsp">
		<jsp:param value="paramValue" name="paramName"/>
	</jsp:forward>
</body>
</html>

 

 

2. el_ex.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@page import="kr.ac.green.Dummy"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<%--
		EL의 범위
		pageScope
		requestScope
		sessionScope
		applicationScope
		
		EL vs <%= %>차이(값이 null일 경우)
		표현식은 null
		EL은 아무것도 안나온다.
		
		getter가 있어야 한다.
	 --%>
	<%
		Dummy d = (Dummy)request.getAttribute("dummy");
	%>	

	<!-- 비어있는지 여부 -->
	${empty emptyArr } 
	<hr> 
	연산 : 
	<%= 1+2+3 %> : ${1+2+3 } : ${10 > 11 }
	<hr>
	map : ${map["key2"] } : ${map.key3 }
	<hr>
	<!-- 리스트나 배열은 대괄호 --> 
	list : ${list[0] } 
	<hr>
	기존 parameter : <%= request.getParameter("paramName") %>
	<%= request.getParameter("paramSome") %> 
	<hr>
	EL parameter : ${param.paramName } : ${param.paramSome }
	<hr>
	기존방식 : <%= request.getAttribute("some").toString() %>
	: <%= session.getAttribute("yourName").toString() %>
	: <%= application.getAttribute("myName") %>
	: <%= d.getNum() %> : <%= d.getTitle() %>
	<hr>
	EL : ${ requestScope.some } : ${ sessionScope.yourName } : ${ applicationScope.myName }
	: ${ requestScope.dummy.num } : ${ requestScope.dummy.title }
	<hr>
	EL other : ${ some } : ${ yourName } : ${ myName }
	: ${ dummy.num } : ${ dummy.title }
	다른 방법 : ${dummy["num"] } : ${dummy["title"] }
	<hr>
	배열 : ${ arr[2] }
</body>
</html>

 

 

3. Dummy.java

package kr.ac.green;

public class Dummy {
	private int num;
	private String title;
	
	public Dummy() {
	}

	public Dummy(int num, String title) {
		super();
		this.num = num;
		this.title = title;
	}

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
	
	
}

 

 

 

728x90
반응형

댓글