Java Bean
DTO(Data Transfer Object)
DTO๋ ๊ณ์ธต ๊ฐ ๋ฐ์ดํฐ ๊ตํ์ ์ํด ์ฌ์ฉํ๋ ๊ฐ์ฒด๋ค. ์ฌ๊ธฐ์ ๊ณ์ธต์ View-Controller-Service-DAO์ ๊ฐ์ ๊ณ์ธต์ ์๋ฏธ
๋ฐ์ดํฐ๋ฅผ ๋ด์ private ์์ฑ๊ณผ ๊ทธ ์์ฑ์ ์ ๊ทผํ ์ ์๋ Getter, Setter ๋ฉ์๋๋ก ๊ตฌ์ฑ๋์ด์๋ค.
VO(Value Object)์ ํผ์ฉ๋์ด ์ฐ์ด๋, VO๋ ๋ด๋ถ ์์ฑ๊ฐ์ ๋ณ๊ฒฝํ ์ ์๋(imuutable) Read-Only์ ์๋ฏธ์ ํน์ฑ์ ๊ฐ์ง ๊ฐ์ฒด์ด๋ค. ์ฆ, ๋ณ๊ฒฝ์์ด ๊ฐ์ผ๋ก ์ทจ๊ธํ ๊ฐ์ฒด๋ฅผ ๋งํ๋ค.
JavaBean(=Bean) ์ด๋?
๋ฐ๋ณต์ ์ธ ์์ ์ ํจ์จ์ ์ผ๋ก ํ๊ธฐ ์ํด์ ์ฌ์ฉํ๋ค.
Bean ์ด๋ JAVA ์ธ์ด์ ๋ฐ์ดํฐ(์์ฑ)๊ณผ ๊ธฐ๋ฅ(๋ฉ์๋)๋ก ์ด๋ฃจ์ด์ง ํด๋์ค์ด๋ค.
Default ์์ฑ์ : ํ๋ผ๋ฏธํฐ๊ฐ ์๋ Default ์์ฑ์๋ฅผ ๊ฐ๊ณ ์์ด์ผํ๋ค.
Property : ์๋ฐ๋น์ด ๋ ธ์ถํ๋ ์ด๋ฆ์ ๊ฐ์ง ์์ฑ์ Property๋ผ๊ณ ํ๋ฉฐ, Property๋ set์ผ๋ก ์์ํ๋ ์์ ์ ๋ฉ์๋(setter)์ get์ผ๋ก ์์ํ๋ ์ ๊ทผ์ ๋ฉ์๋(getter)๋ฅผ ์ด์ฉํด ์์ ๋๋ ์กฐํํ ์ ์๋ค.
DTO์ Java Beans์ ๊ด๊ณ์ ๋ํด์ DTO์ ํ์์ผ๋ก Java Beans๋ฅผ ๋ฐ๋ฅด๊ณ ์๋ค๊ณ ์๊ฐํ๋ฉด ๋๋ค.
Spring์์ ์ง์นญํ๋ Bean์ด๋, Spring์ IoC Container(=DI Container)๋ฅผ ํตํด ๊ด๋ฆฌ(์์ฑ, ์ ์ด)๋๋ ๊ฐ์ฒด๋ฅผ ๋งํ๋ฉฐ, ์ด๋ Spring IoC Container์์ ์์ธํ ๋ณผ ์ ์๋ค.
Bean ๋ง๋ค๊ธฐ
๋น์ ๋ง๋ ๋ค๋ ๊ฒ์ ๋ฐ์ดํฐ ๊ฐ์ฒด๋ฅผ ๋ง๋ค๊ธฐ ์ํ ํด๋์ค๋ฅผ ๋ง๋๋ ๊ฒ์ด๋ค. ( getter, setter )
package example;
public class Student {
private String name;
private int age;
private int grade;
public Student() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
}
Bean ์ฌ์ฉํ๊ธฐ
๊ด๋ จ ์ก์ ํ๊ทธ(useBean, getProperty, setProperty)๋ก ์ฃผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ๋ฐ์ดํธํ๊ณ , ์ป์ด์ค๋ ์ญํ ์ ํ๋ค.
useBean
ํน์ Bean์ ์ฌ์ฉํ๋ค๊ณ ๋ช ์ํ ๋ ์ฌ์ฉํ๋ค.
<jsp:useBean id = "๋น์ด๋ฆ" class="ํจํค์ง๋ช
์ ํฌํจํ ํด๋์ค๋ช
" scope="์ค์ฝํ ๋ฒ์"/>
<jsp:useBean id = "student" class="example.Student" scope="page"/>
Scope ๋ฒ์
scope
์ค๋ช
page
์์ฑ๋ ํ์ด์ง ๋ด์์๋ง ์ฌ์ฉ ๊ฐ๋ฅ
request
์์ฒญ๋ ํ์ด์ง ๋ด์์๋ง ์ฌ์ฉ ๊ฐ๋ฅ
session
์น ๋ธ๋ผ์ฐ์ ์๋ช ์ฃผ๊ธฐ์ ๋์ผํ๊ฒ ์ฌ์ฉ ๊ฐ๋ฅ
application
์น ์ดํ๋ฆฌ์ผ์ด์ ์๋ช ์ฃผ๊ธฐ์ ๋์ผํ๊ฒ ์ฌ์ฉ๊ฐ๋ฅ
setProperty
๋ฐ์ดํฐ ๊ฐ์ ์ค์ ํ ๋ ์ฌ์ฉํ๋ค.(setter)
<jsp:setProperty name="๋น ์ด๋ฆ" property="์์ฑ ์ด๋ฆ" value ="์์ฑ ๊ฐ" />
<jsp:setProperty name="student" property="name" value ="ํ๊ธธ๋" />
getProperty
๋ฐ์ดํฐ ๊ฐ์ ๊ฐ์ ธ์ฌ ๋ ์ฌ์ฉํ๋ค.(getter)
<jsp:getProperty name="๋น ์ด๋ฆ" property="์์ฑ ์ด๋ฆ" />
<jsp:getProperty name="student" property="name" />
์์
Java Class
package example;
public class Student {
private String name;
private int age;
private int grade;
public Student() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
}
jsp๋ก ๊ตฌํํ๊ธฐ
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<jsp:useBean id="student" class="example.Student" scope="page" />
<!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>
<jsp:setProperty name="student" property="name" value="๋ฐ์"/>
<jsp:setProperty name="student" property="age" value="13"/>
<jsp:setProperty name="student" property="grade" value="6"/>
์ด๋ฆ : <jsp:getProperty name="student" property="name" /><br />
๋์ด : <jsp:getProperty name="student" property="age" /><br />
ํ๋
: <jsp:getProperty name="student" property="grade" /><br />
</body>
</html>
Last updated
Was this helpful?