[MVC 3-7] EL - Map

์ž„์Šนํ˜„ยท2023๋…„ 1์›” 25์ผ
0

MVC

๋ชฉ๋ก ๋ณด๊ธฐ
9/27

๐Ÿง์˜ˆ์ œ-1

๐Ÿ“ƒhewon_map.jsp

<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	Map<String, String> hewonMap=new HashMap<String, String>();
	hewonMap.put("first.name", "ํ™");
	hewonMap.put("second.name", "๊ธธ๋™");
	//
	request.setAttribute("hewonMap", hewonMap);
	//
	request.getRequestDispatcher("hewon_map_el.jsp").forward(request, response);
%>

๐Ÿ“ƒhewon_map_el.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>MVC</title>
</head>
<body>
	<h1>EL - Map</h1>
	<hr>
	<%-- ์Šค์ฝ”ํ”„ ์†์„ฑ๊ฐ’์œผ๋กœ Map ๊ฐ์ฒด๊ฐ€ ์ €์žฅ๋œ ๊ฒฝ์šฐ ๋งตํ‚ค์— EL ํ‘œํ˜„์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ๋ถ€์ ์ ˆํ•œ ๋ฌธ์ž๊ฐ€ ์กด์žฌํ•  ๊ฒฝ์šฐ . ์—ฐ์‚ฐ์ž๋กœ ๋งตํ‚ค๋ฅผ ํ‘œํ˜„ํ•˜์—ฌ ๋งต๊ฐ’์„ ์ œ๊ณต๋ฐ›์•„ ์ถœ๋ ฅ ๋ถˆ๊ฐ€๋Šฅ --%>
	<%-- <p>์ด๋ฆ„ = ${hewonMap.first.name } ${hewonMap.second.name }</p> --%>
	<%-- --%>
	<%-- ์Šค์ฝ”ํ”„ ์†์„ฑ๊ฐ’์œผ๋กœ Map ๊ฐ์ฒด๊ฐ€ ์ €์žฅ๋œ ๊ฒฝ์šฐ ๋งตํ‚ค์— EL ํ‘œํ˜„์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ๋ถ€์ ์ ˆํ•œ ๋ฌธ์ž๊ฐ€ ์กด์žฌํ•  ๊ฒฝ์šฐ [] ์—ฐ์‚ฐ์ž๋กœ ๋งตํ‚ค๋ฅผ ํ‘œํ˜„ํ•˜์—ฌ ๋งต๊ฐ’์„ ์ œ๊ณต๋ฐ›์•„ ์ถœ๋ ฅ --%>
	<p>์ด๋ฆ„ = ${hewonMap["first.name"] } ${hewonMap["second.name"] }</p>
</body>
</html>

๐Ÿง์˜ˆ์ œ-2

๐Ÿ“ƒfruits_map.jsp

<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	Map<String, String> fruitsMap=new HashMap<String, String>();
	fruitsMap.put("one", "๋”ธ๊ธฐ");
	fruitsMap.put("two", "๋ณต์ˆญ์•„");
	fruitsMap.put("three", "์‚ฌ๊ณผ");
	//
	request.setAttribute("fruitsMap", fruitsMap);
	request.setAttribute("choice", "two");
	//
	request.getRequestDispatcher("fruits_map_el.jsp").forward(request, response);
%>

๐Ÿ“ƒfruits_map_el.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>MVC</title>
</head>
<body>
	<h1>EL - Map</h1>
	<hr>
	<p>๊ณผ์ผ-1 = ${fruitsMap.one }</p>
	<p>๊ณผ์ผ-2 = ${fruitsMap.two }</p>
	<p>๊ณผ์ผ-3 = ${fruitsMap.three }</p>
	<hr>
	<p>์ข‹์•„ํ•˜๋Š” ๊ณผ์ผ = ${fruitsMap.two  }</p>
	<p>์ข‹์•„ํ•˜๋Š” ๊ณผ์ผ = ${fruitsMap["two"] }</p>
	<%-- ์Šค์ฝ”ํ”„ ์†์„ฑ๊ฐ’์ด Map ๊ฐ์ฒด์ธ ๊ฒฝ์šฐ ๋งตํ‚ค๋ฅผ ์ด์šฉํ•˜์—ฌ ๋งต๊ฐ’์„ ์ œ๊ณต๋ฐ›์•„ ์ถœ๋ ฅ ๊ฐ€๋Šฅ --%>
	<%-- โ†’ ๋งตํ‚ค์— ํ•ด๋‹นํ•˜๋Š” ๋งต๊ฐ’์ด ์—†๋Š” ๊ฒฝ์šฐ EL ๋ฏธ์‹คํ–‰ --%>
	<%-- <p>์ข‹์•„ํ•˜๋Š” ๊ณผ์ผ = ${fruitsMap.choice }</p> --%>
	<%-- EL ํ‘œํ˜„์‹์—์„œ [] ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•˜์—ฌ ๋‹ค๋ฅธ ์Šค์ฝ”ํ”„ ์†์„ฑ๊ฐ’์„ ์ œ๊ณต๋ฐ›์•„ ๋งตํ‚ค๋กœ ํ‘œํ˜„ํ•˜์—ฌ ๋งต๊ฐ’ ์ถœ๋ ฅ ๊ฐ€๋Šฅ --%>
	<p>์ข‹์•„ํ•˜๋Š” ๊ณผ์ผ = ${fruitsMap[choice] }</p>
</body>
</html>

0๊ฐœ์˜ ๋Œ“๊ธ€