jspBean
Bean = 데이터를 모아서 한번에 전송/출력하는 것을 의미한다.
Spring에서는 VO:Value Object,
MyBatis에서는 DTO:Data Transfer Object,
JPA에서는 Entity...
만든 업체에 따라 데이터 모으는 방법은 같지만 이름만 다르다!
특히 jspBeans는..
JSP 페이지가 복잡한 자바 코드로 구성되는 것을 가능한 피하고,
HTML 등 간단한 코드만으로 구성하도록 하기 위해서 사용한다.
Bean을 사용하기 위해서는 우선 Bean 클래스를 작성해 주어야 하는데, 이 때 일정한 규칙이 있다.
> 1. 정보를 저장하는 변수는 모두 private으로 선언한다.
> 2. private으로 선언된 변수의 getter/setter를 만든다.
> 3. getter/setter는 public 메서드로 선언한다.
아래와 같은 테이블이 있다고 해 보자.
모든 컬럼이 다 읽기와 쓰기를 다 지원할 필요는 없고, 읽기 또는 쓰기 하나만 지원할 수도 있다.
그런 특성에 따라 getter와 setter 메서드를 생성해 준다.
Bean과 관련된 jsp 액션태그
<jsp:useBean> : 객체 메모리 할당
<jsp:useBean id="mb" class=dao.MemberBean">
↳ MemberBean mb = new MemberBean() 과 동일하다.
<jsp:setProperty> : setter
<jsp:setProperty name="객체명" property="변수" value="값"/>
객체명이 반드시 동일해야 한다!
<jsp:setProperty name="bean" property="name" value="kim"/>
↳ mb.setName("kim") 이랑 동일
<jsp:setProperty name="bean" property="*"/> 이렇게 하면 그냥 모든 값을 다 불러와서 다 넣으라는 뜻!
<jsp:getProperty> : getter
<jsp:getProperty name="객체명" property="name">
↳ <%=mb.getName()%>과 같다.
getProperty는 많이 쓰지는 않는다. 출력용임.
서로 관련된 데이터끼리는 다수의 Bean/VO를 하나의 DAO에 통합해서 사용할 수 있다. = Service
하지만 따로 만드는 걸 권장한다. → Bean/VO를 하나씩 갖다쓸 때 번거로워지기 때문
이제 실제로 사용해 보자. 우선 Member라는 정보를 담는 MemberBean을 작성한다.
MemberBean.java
package dao;
public class MemberBean {
private String name, address, tel, sex;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
사용자로부터 회원 정보를 받아 post 방식으로 output.jsp에게 보내주는 페이지를 작성한다.
input.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="table.css">
<style type="text/css">
.container{
margin-top: 50px;
width: 100%;/*윈도우 전체 사용*/
}
.table_content{
margin: 0px auto;
width: 450px;
}
h1{
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<h1>회원 정보</h1>
<form method=post action="output1.jsp">
<%-- <form method=post action="output2.jsp"> -->
<table class="table_content">
<tr>
<th width=30%>이름</th>
<td width=70%>
<input type="text" name=name size=20>
</td>
</tr>
<tr>
<th width=30%>성별</th>
<td width=70%>
<input type="radio" name=sex value="남자" checked="checked">남자
<input type="radio" name=sex value="여자">여자
</td>
</tr>
<tr>
<th width=30%>나이</th>
<td width=70%>
<input type=number name=age min="20" max="50">
</td>
</tr>
<tr>
<th width=30%>주소</th>
<td width=70%>
<input type=text name=address size=25>
</td>
</tr>
<tr>
<th width=30%>전화번호</th>
<td width=70%>
<input type=text name=tel size=20>
</td>
</tr>
<tr>
<td colspan="2" align=center>
<button>전송</button>
</tr>
</table>
</form>
</div>
</body>
</html>
input에서 작성하고 전송한 데이터는 output에서 출력해 준다.
이 때 input에서 보내 준 데이터를 받는 방식은 두 가지가 있다.
output1.jsp (jsp액션태그 없이 받아 오는 방법)
<%@page import="dao.*" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("UTF-8"); //한 글 꼭!
MemberBean mb = new MemberBean();
String name = request.getParameter("name");
String sex = request.getParameter("sex");
String age = request.getParameter("age");
String address = request.getParameter("address");
String tel = request.getParameter("tel");
mb.setName(name);
mb.setSex(sex);
mb.setAge(Integer.parseInt(age));
mb.setAddress(address);
mb.setTel(tel);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
이름:<%=mb.getName() %>
성별:<%=mb.getSex() %>
나이:<%=mb.getAge() %>
주소:<%=mb.getAddress() %>
전화번호:<%=mb.getTel() %>
</body>
</html>
output2.jsp (jsp액션 태그를 사용하는 법)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="dao.*"%>
<%
request.setCharacterEncoding("UTF-8");
%>
<jsp:useBean id="mb" class="dao.MemberBean">
<jsp:setProperty name="mb" property="*"/>
</jsp:useBean>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
이름:<%=mb.getName() %> <br>
성별:<%=mb.getSex() %> <br>
나이:<%=mb.getAge() %> <br>
주소:<%=mb.getAddress() %> <br>
전화번호:<%=mb.getTel() %> <br>
</body>
</html>
<jsp:useBean> 을 사용하면 굉장히 코드가 간편해진다!
'부트캠프(END) > Web' 카테고리의 다른 글
JSP : 내장 객체 / session (0) | 2022.07.21 |
---|---|
JSP : 내장 객체 / application + cookie (0) | 2022.07.20 |
JSP : 액션 태그 (0) | 2022.07.19 |