[JSP/Servlet] Expression Language 정의 및 간단한 확인 예제
Expression Language - jsp에서 출력을 위해 제공되는 언어, jsp 2.0에서 추가된 기능
- 구문 : ${출력내용} --> 속성 또는 속성의 property 출력
- 출력
+ 문자열 : '값', "값"
+ 숫자 : 정수, 실수
+ 논리 : true / false
+ null --> 출력 X
- 사용
+ 사칙연산 : +, -, *, /(div)
+ 비교연산 : > (gt)
>= (ge)
< (lt)
<= (le)
== (eq)
!= (ne)
+ empty 값 <-- 여기서 값은 문자열, 조회한 값
- 문자열 -> 글자수 0개 일때 true, 1개 이상일때 false
- 조회한 값 -> true(null)
+ 논리연산 : && -> and
|| -> or
! -> not
+ 삼항연산자 : ${ 조건 ? 값1 : 값2 }
Customer VO.java
package vo;
import java.io.Serializable;
public class Customer implements Serializable{
private String name;
private String email;
private int age;
private double weight;
private boolean marriage;
public Customer(){}
public Customer(String name, String email, int age, double weight, boolean marriage) {
super();
this.name = name;
this.email = email;
this.age = age;
this.weight = weight;
this.marriage = marriage;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public boolean isMarriage() {
return marriage;
}
public void setMarriage(boolean marriage) {
this.marriage = marriage;
}
@Override
public String toString() {
return "Customer [name=" + name + ", email=" + email + ", age=" + age + ", weight=" + weight + ", marriage="
+ marriage + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((email == null) ? 0 : email.hashCode());
result = prime * result + (marriage ? 1231 : 1237);
result = prime * result + ((name == null) ? 0 : name.hashCode());
long temp;
temp = Double.doubleToLongBits(weight);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Customer other = (Customer) obj;
if (age != other.age)
return false;
if (email == null) {
if (other.email != null)
return false;
} else if (!email.equals(other.email))
return false;
if (marriage != other.marriage)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (Double.doubleToLongBits(weight) != Double.doubleToLongBits(other.weight))
return false;
return true;
}
}
Customer 객체를 받아와서 Expression Language를 이용해서 출력해보는 예제
<%@page import="vo.Customer"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%
Customer customer = new Customer("박영희","park@a.com",30,60.7,false);
request.setAttribute("customer", customer);
Customer cust = new Customer("이승헌","lee@a.com",27,79.4,true);
session.setAttribute("customer", cust);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%-- getAttribute("customer").getName() --%>
이름 : ${requestScope.customer.name }<br>
이메일 : ${requestScope.customer.email}<br>
나이 : ${requestScope.customer.age }<br> <!-- getAttribute("customer").getAge() -->
몸무게 : ${requestScope.customer.weight }kg<br>
결혼여부 : ${requestScope.customer.marriage?"기혼":"미혼"}
<p>
${customer } <%--getAttribute("customer") --%><br>
${sessionScope.customer }
<%--getAttribute("result") => null --%>
${result } <!-- 출력할 내용이 null일경우는 출력을 하지 않는다
출력대상을 찾아가는 도중 null이 나온경우 연산을 멈추고 아무것도 출력하지 않는다-->
</p>
<hr>
<h1>Session Scope에서 조회한 고객 정보</h1>
이름 : ${sessionScope.customer.name }<br>
이메일 : ${sessionScope.customer.email}<br>
나이 : ${sessionScope.customer.age }<br> <!-- getAttribute("customer").getAge() -->
몸무게 : ${sessionScope.customer.weight }kg<br>
결혼여부 : ${sessionScope.customer.marriage?"기혼":"미혼"}
</body>
</html>
Expression Language를 이용해 배열, ArrayList, Map으로 출력해보기
<%@page import="java.util.HashMap"%>
<%@page import="vo.Customer"%>
<%@page import="java.util.ArrayList"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%
String[]names = {"김민수", "이영철", "박영희"};
request.setAttribute("names", names);
ArrayList<Customer> customerList = new ArrayList<>();
customerList.add(new Customer("이고객","lee@a.com",30,70.6,true));
customerList.add(new Customer("김고객","kim@a.com",20,60.6,false));
customerList.add(new Customer("최고객","choi@a.com",32,80.6,true));
request.setAttribute("list", customerList);
HashMap map = new HashMap();
map.put("pId","p-111");
map.put("product name","노트북");
map.put("price",3000000);
map.put("cust",new Customer("최고객","choi@a.com",31,63.5,true));
session.setAttribute("info", map);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>EL - Map 출력 -key값으로 조회하므로 .연산자와 []연산자 모두 사용가능</h2>
제품 ID : ${sessionScope.info.pId }<br>
제품 이름 : ${sessionScope.info["product name"]}<br><%--식별자 규칙에 어긋나는 문자가 있을 경우 [] 표기법을 사용해야한다. --%>
제품 가격 : ${sessionScope.info.price }원<br>
<%-- Map에 VO를 받아서 출력 --%>
구매한 사람 : ${sessionScope.info.cust.name }<br>
구매한 사람 email : ${sessionScope.info.cust.email }<br>
구매한 사람 나이 : ${sessionScope.info.cust.age }세<br>
구매한 사람 몸무게 : ${sessionScope.info.cust.weight }kg <br>
구매한 사람 결혼여부 : ${sessionScope.info.cust.marriage?"기혼":"미혼" }
<hr>
<h2>EL - List 출력</h2>
이름 : ${requestScope.list[0].name }<br>
이메일 : ${requestScope["list"][0]["email"]}
<table border="1" width="500px">
<thead>
<tr>
<td>이름</td>
<td>이메일</td>
<td>나이</td>
<td>몸무게</td>
<td>결혼여부</td>
</thead>
<tbody>
<tr> <!-- 고객 1명의 정보 -->
<td>${requestScope.list[0].name }</td>
<td>${requestScope.list[0].email}</td>
<td>${requestScope.list[0].age }세</td>
<td>${requestScope.list[0].weight }kg</td>
<td>${requestScope.list[0].marriage?"기혼":"미혼" }</td>
</tr>
<tr>
<td>${requestScope.list[1].name}</td>
<td>${requestScope.list[1].email}</td>
<td>${requestScope.list[1].age}세</td>
<td>${requestScope.list[1].weight}kg</td>
<td>${requestScope.list[1].marriage?"기혼":"미혼"}</td>
</tr>
<tr>
<td>${requestScope.list[2].name }</td>
<td>${requestScope.list[2].email}</td>
<td>${requestScope.list[2].age }세</td>
<td>${requestScope.list[2].weight }kg</td>
<td>${requestScope.list[2].marriage?"기혼":"미혼" }</td>
</tr>
</tbody>
</table>
<hr>
<h2>EL - 배열출력</h2>
<ul>
<li>${requestScope.names[0] }</li> <%-- 배열의 index로 접근 하는 경우는 []연산자(표기법) 사용 --%>
<li>${requestScope.names["1"]}</li>
<li>${requestScope.names[2] }</li>
</ul>
</body>
</html>