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?