본문 바로가기
JAVA/JSP, JSTL, EL

EL - 클래스와 jsp연결후 EL사용법

by 설총이 2018. 7. 24.

1. 자바 클래스중에서 getInfo()라는 getter가 존재하는 상태로 jsp 실행



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package el;
 
import java.util.HashMap;
import java.util.Map;
 
public class Thermometer {
    private Map<String, Double> locationCelsiusMap 
        = new HashMap<String, Double>();
 
    public void setCelsius(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";
    }
}
cs



2. request setAttribute의 key값을 t , value값을 thermometer로 둠으로써,

thermometer의 메서드를 t.XXX로 호출할 수 있다.


3. 맨 아래 ${t.info} 가 가능한 이유를 잘 기억해두자



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page import = "el.Thermometer" %>
<%
    Thermometer thermometer = new Thermometer();
// import한 후, 자바클래스로 만든 thermometer new 객체 생성.
    request.setAttribute("t", thermometer);
/* String값으로 t, Obejct값을 저장할수있는 thermometer 이므로, 
클래스객체의저장이 가능하다 */
%>
<!DOCTYPE html>
<html>
<head>
<title>온도 변환 예제</title>
</head>
<body>
    ${t.setCelsius('서울',27.3) }
    서울 온도 : 섭씨 ${t.getCelsius('서울')}도/화씨 ${t.getFahrenheit('서울')}
    <br/>
    
 
    정보 : ${t.info }
    <!-- 
    t를 찾아봤더니 List도, Array도, Map도 아니므로,
    일반 객체임을 알 수 있는데 info라는 변수는 존재하지않는데도 실행이 된다.. 
    그 이유는 getter가 존재한다면 getXxxx의 메서드를 .xxxx로 찾아갈 수 있게된다.
    매개변수가 없는 getter인 getInfo를 호출. 
    1. getInfo()가 없으면 실행될 수 없고,
    2. 매개변수가 있으면 못찾아감을 기억하자 !! 
    -->
</body>
</html>
cs