Java Bean
정의
웹 어플레이케이션 개발시 MVC2 패턴으로 많이들 설계한다. (Model, controller, View)
이때 Model에서 Dto 클래스에 개발자가 필요한 데이터들을 모아서 객체 타입으로 변환하고 사용한다.
조금 다르지만 이와 유사하게 사용되는 것이 Java Bean이다.
개발자가 사용하고 싶은 데이터들을 모아 클래스화 시키고 그 클래스에 데이터를 넣어 활용하기 위한 기술이다.
사용법
1. bean 클래스를 생성한 후 내가 사용할 데이터들 필드로 선언하고 getter, setter를 작성한다.
2. Jsp 파일에서 useBean 액션태그를 이용해 내가 사용할 데이터가 들어있는 bean 클래스를 사용하겠다고 선언한다.
3. setProperty, getProperty 액션태그를 이용해 선언한 JavaBean을 이용한다.
주의점
1. 속성값에 jsp태그를 이용하면 500에러가 발생한다.
1 | <jsp:setProperty property="myid" name="myBean" value="<%=request.getParameter("myid") %>"/> | cs |
속성값에 직접 값을 넣으면 값이 잘 들어간다.
1 2 | <jsp:setProperty property="myid" name="myBean" value="값을 넣어 보았다."/> <jsp:getProperty property="myid" name="myBean"/> | cs |
2. 빈 클래스의 생성자가 디폴트가 아닌 매개변수를 받는 생성자로 오버라이딩 했을경우 useBean 액션태그가 객체생성을
하지못해서 에러가 난다.
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 32 33 34 35 36 | package joinBean; public class JoinBean { private String myid; private String password; private String email; private String myname; private String Rrn; private String birth_year; private String birth_month; private String birth_day; // public JoinBean(String myid, String password, String email, String name, String rrn, String birth_year, // String birth_month, String birth_day) { // super(); // this.myid = myid; // this.password = password; // this.email = email; // this.name = name; // Rrn = rrn; // this.birth_year = birth_year; // this.birth_month = birth_month; // this.birth_day = birth_day; // } public String getMyname() { return myname; } public void setMyname(String myname) { this.myname = myname; } | cs |
3. set하고 싶은 Element들의 name속성값과 빈 클래스의 필드 명이 같을 경우 *을 이용해 한번에 데이터를 넣을 수 있다.
1 | <jsp:setProperty property="*" name="myBean"/> | cs |
4. 필드명의 첫 번째 또는 두 번째가 대문자일 경우 에러가 발생한다.
Java의 클래스에서 필드명의 setter / getter 생성시 set 또는 get 뒤에 필드명의 첫글자를 대문자로 잡아준다.
1 2 3 4 5 6 7 8 9 10 | private String myname; public String getMyname() { return myname; } public void setMyname(String myname) { this.myname = myname; } | cs |
그런데 만약 필드명의 첫 번째가 대문자일 경우
1 2 3 4 | private Myname; public void setMyname(){} public void getMyname(){} | cs |
필드명이 myname일 경우와 setter와 getter의 식별자가 같아진다.
자바빈에서 해당 빈 클래스를 이용할때 이는 모호성을 발생시키기 때문에 에러가 발생하는 것 같다.
그리고 두 번째 문자가 대문자일 경우는 setter / getter 호출시 필드명을 잘못 인지하게 된다.
1 2 3 4 | private mYname; public void setMYname(){} public void getMYname(){} | cs |
소스 코드
Validation.html : 회원가입 폼
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | <!DOCTYPE html> <html lang="ko" dir="ltr"> <head> <meta charset="utf-8"> <title></title> </head> <script type="text/javascript"> var idCheck =/^[a-zA-Z0-9]{4,12}$/; var pwCheck =/^[a-zA-Z0-9]{4,12}$/; var mailCheck = /^[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*@[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*.[a-zA-Z]{2,3}$/i; var RrnCheck2 =/^[0-9]{13}$/; // 숫자만 13자리 입력가능 var RrnArray = new Array(); // 주민번호 담을 어레이 //유효성 검사 function validate() { if(idValidate()&&pw1Validate()&&pw2Validate()&&mailValidate()&&RrnValidate()){ window.alert("method returns true"); return true; }else{ window.alert("method returns false"); return false; } } //아이디 유효성 검사 function idValidate(){ var myid = document.getElementById("myid"); //window.alert("안녕"); var myid = document.getElementById("myid"); var idOK = idCheck.exec(myid.value); if(idOK){ window.alert("아이디 정상입니다.") return true; }else{ window.alert("ID 형식이 틀립니다."); return false; } } //비밀번호 유효성 검사 function pw1Validate(){ var pw1 = pwCheck.exec(password.value); if(pw1&& myid.value!=password.value){ window.alert("비번1 정상입니다.") return true; }else{ window.alert("Passwd 형식이 틀립니다."); return false; } } //비밀번호 확인 유효성 검사 function pw2Validate(){ var pw2OK = pwCheck.exec(confirmpassword.value); if(pw2OK&&password.value==confirmpassword.value){ window.alert("비번2 정상입니다.") return true; }else{ window.alert("Passwd 형식이 틀립니다."); return false; } } //이메일 확인 유효성 검사 function mailValidate(){ var mailOK = mailCheck.exec(email.value); if(mailOK){ window.alert("이메일 정상입니다.") return true; }else{ window.alert("이메일 형식이 틀립니다."); return false; } } // 집주소 유효성검사 function homeAddressValidate(){ var objAddress = document.getElementById("address"); var addressOk = homeAddressCheck.exec(objAddress.value); if(addressOk){ alert("집주소 정상입니다.") return true; }else{ alert("집주소 형식이 틀립니다."); return false; } } //주민등록번호 유효성 검사 function RrnValidate(){ window.alert("rrn method is called"); var RrnOK = RrnCheck2.exec(Rrn.value); // 숫자만 들어왔을때 true window.alert(Rrn.value); window.alert(RrnOK); //숫자만 13자리 들어왔을때 if(RrnOK){ var RrnCheck = document.getElementById("Rrn"); //바인딩 // 주민번호 입력한거 배열에 담아주기 for(var i=0; i< RrnCheck.value.length; i++){ RrnArray[i] = RrnCheck.value.charAt(i); } // 주민번호 유효성 계산해주기 var tempSum=0; for(var i=0; i<6 ; i++){ tempSum += RrnArray[i] * (2+i); } for(var i=0; i<6; i++){ if(i>=2){ tempSum += RrnArray[i+6] * i; } else{ tempSum += RrnArray[i+6] * (8+i); } } //유효성 검사 결과 if((11-(tempSum%11))%10 != RrnArray[12]){ window.alert("올바른 주민번호가 아닙니다."); RrnArray=""; return false; } else{ window.alert("올바른 주민등록번호 입니다."); // 생일에 주민번호 입력한 앞 6자리 넣어주기 var birthYear = document.getElementById("birth_year"); var birthMonth = document.getElementById("birth_month"); var birthDay = document.getElementById("birth_day"); // 년도 설정 if(RrnArray[0]>2){ birth_year.value = "19"+RrnArray[0]+RrnArray[1]; }else{ birth_year.value = "20"+RrnArray[0]+RrnArray[1]; } // 월 설정 if(RrnArray[2]==0){ birth_month.value = RrnArray[3]; }else{ birth_month.value = RrnArray[2]+RrnArray[3]; } // 일 설정 if(RrnArray[4]==0){ birth_day.value = RrnArray[5]; }else{ birth_day.value = RrnArray[4]+RrnArray[5]; } return true; } }else{ window.alert("주민등록번호 형식이 틀립니다."); return false; } } </script> <!-- HTML BODY-------------------------------------------------------------- --> <body> <form action="joinCheck.jsp" method="get" onsubmit="return validate()"> <center> <table border="1" cellspacing="0" id="myTable"> <tr align="center"> <td colspan="2" bgcolor="00FA9A">회원기본정보</td> </tr> <tr> <td align="center" bgcolor="90EE90">아이디:</td> <td><input type="text" id="myid" name="myid">4~12자의 영문 대소문자와 숫자로만 입력</td> </tr> <tr> <td align="center" bgcolor="90EE90">비밀번호:</td> <td><input type="text" id="password" name="password">4~12자의 영문 대소문자와 숫자로만 입력</td> </tr> <tr> <td align="center" bgcolor="90EE90">비밀번호 확인:</td> <td><input type="text" id="confirmpassword" name="confirmpassword"></td> </tr> <tr> <td align="center" bgcolor="90EE90">메일주소:</td> <td><input type="text" size="25" id="email" name="email">예)id@domain.com </td> </tr> <tr> <td align="center" bgcolor="90EE90">이름:</td> <td><input type="text" size="25" id="name" name="name">4~12자의 영문 대소문자와 숫자로만 입력</td> </tr> <tr align="center"> <td colspan="2" bgcolor="00FA9A">개인 신상 정보</td> </tr> <tr> <td align="center" bgcolor="90EE90">주소:</td> <td colspan="2"><input type="text" name=""class="postcodify_postcode5" value="" size="90" /> <button id="postcodify_search_button">검색</button> <br /> <input type="text" name="" class="postcodify_address" value="" size="90" /><br /> <input type="text" name="" class="postcodify_details" value="" size="90" id="address" /><br /> <input type="text" name="" class="postcodify_extra_info" value=""size="90" /><br /></td> <script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <script src="//d1p7wdleee1q2z.cloudfront.net/post/search.min.js"></script> <script> $(function() { $("#postcodify_search_button").postcodifyPopUp(); }); </script> </tr> <tr> <td align="center" bgcolor="90EE90">주민등록번호:</td> <td><input type="text" value="" size="25" id="Rrn" name="Rrn">예)1234561234567 </td> </tr> <tr> <td align="center" bgcolor="90EE90">생일:</td> <td><input type="text" size="10" id="birth_year" name="birth_year">년 <select id="birth_month" name="birth_month"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> <option value="11">11</option> <option value="12">12</option> </select>월 <select id="birth_day" name="birth_day"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> <option value="11">11</option> <option value="12">12</option> <option value="13">13</option> <option value="14">14</option> <option value="15">15</option> <option value="16">16</option> <option value="17">17</option> <option value="18">18</option> <option value="19">19</option> <option value="20">20</option> <option value="21">21</option> <option value="22">22</option> <option value="23">23</option> <option value="24">24</option> <option value="25">25</option> <option value="26">26</option> <option value="27">27</option> <option value="28">28</option> <option value="29">29</option> <option value="30">30</option> <option value="31">31</option> </select>일 </td> </tr> <tr> <td align="center" bgcolor="90EE90">관심분야:</td> <td><input type="checkbox" name="intereted" value="컴퓨터">컴퓨터 <input type="checkbox" name="intereted" value="인터넷">인터넷 <input type="checkbox" name="intereted" value="여행">여행 <input type="checkbox" name="intereted" value="영화감상">영화감상 <input type="checkbox" name="intereted" value="음악감상">음악감상 </td> </tr> <tr> <td align="center" bgcolor="90EE90">자기소개:</td> <td><textarea rows="8" cols="60" id="introduce"></textarea> </td> </tr> </table> </center> <br> <center> <input type="submit" value="회원 가입"> <input type="reset" value="다시 입력"> </center> </form> </body> </html> | cs |
JoinBean.java : 자바빈 클래스
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | package joinBean; public class JoinBean { private String myid; private String password; private String email; private String name; private String Rrn; //필드명의 첫번째가 대문자면 안된다. private String birth_year; private String birth_month; private String birth_day; private String[] intereted; // public JoinBean(String myid, String password, String email, String name, String rrn, String birth_year, // String birth_month, String birth_day) { // super(); // this.myid = myid; // this.password = password; // this.email = email; // this.name = name; // Rrn = rrn; // this.birth_year = birth_year; // this.birth_month = birth_month; // this.birth_day = birth_day; // } public String[] getIntereted() { return intereted; } public void setIntereted(String[] intereted) { this.intereted = intereted; } /*public String getIntereted() { return intereted[0]; } public void setIntereted(String[] intereted) { this.intereted = intereted; }*/ public String getName() { return name; } public void setName(String name) { this.name = name; } public String getMyid() { return myid; } public String getRrn() { return Rrn; } public void setRrn(String rrn) { Rrn = rrn; } public void setMyid(String myid) { this.myid = myid; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getBirth_year() { return birth_year; } public void setBirth_year(String birth_year) { this.birth_year = birth_year; } public String getBirth_month() { return birth_month; } public void setBirth_month(String birth_month) { this.birth_month = birth_month; } public String getBirth_day() { return birth_day; } public void setBirth_day(String birth_day) { this.birth_day = birth_day; } } | cs |
joinCheck.jsp : 자바빈을 이용할 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 30 31 32 33 34 35 36 37 38 39 40 41 | <%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <jsp:useBean id="myBean" class="joinBean.JoinBean" scope="page"></jsp:useBean> <!DOCTYPE html> <html> <head> <meta charset="EUC-KR"> <title>Insert title here</title> </head> <body> <jsp:setProperty property="*" name="myBean"/> 아이디 : <jsp:getProperty property="myid" name="myBean"/></br> 비밀번호 : <jsp:getProperty property="password" name="myBean"/></br> 이메일 : <jsp:getProperty property="email" name="myBean"/></br> 이름 : <jsp:getProperty property="name" name="myBean"/></br> 태어난 년 : <jsp:getProperty property="birth_year" name="myBean"/></br> 태어난 달 : <jsp:getProperty property="birth_month" name="myBean"/></br> 태어난 일 : <jsp:getProperty property="birth_day" name="myBean"/></br> <%/* 흥미 : <jsp:getProperty property="intereted" name="myBean"/> */ %> 취미 :<% String[] checkbox = myBean.getIntereted(); for(String i : checkbox){ out.print(i); } %> </body> </html> | cs |
'Web' 카테고리의 다른 글
JSP와 Servlet 이란? (0) | 2018.11.01 |
---|