[jsp] java beans 자바 빈즈 태그로 객체 생성하기

seulki·2022년 10월 19일
0

jsp

목록 보기
34/51
post-custom-banner

🎈 자바 빈즈 생성하기

package com.koit.web.beans;

public class MemberBean {
	private String name = "";
	private int age = 0;
	
	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;
	}
}


🎈 jsp action 태그로 빈즈 객체 생성하기

  • MemberBean 이라는 클래스 객체 생성하기

자바 : MemberBean member = new Member();
jsp : <jsp:useBean class="패키지명.MemberBean" id="member" />



🎈jsp action 태그로setter 생성하기

자바 : member.setName("자바학생");
jsp : <jsp:setProperty name="member" property="name" value="자바학생" />

  • name = MemberBean 객체이름
  • property = 변수이름
  • value = 저장하고자 하는 값


🎈jsp action 태그로 getter 생성하기

자바 : member.getName();
jsp : <jsp:getProperty name="member" property="name" />



🎈 bean_test.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>
</head>
<body>
	<!-- 객체선언  -->
	<jsp:useBean class="com.koit.web.beans.MemberBean"  id="member" />
	
	<jsp:setProperty property="name" name="member" value="김자바"/>
	<jsp:setProperty property="age" name="member" value="22"/>
	
	<table border="1">
		<tr>
			<th>이름</th>
			<th>나이</th>
		</tr>
		<tr>
			<td><jsp:getProperty property="name" name="member"/></td>
			<td><jsp:getProperty property="age" name="member"/></td>
		</tr>
	</table>
</body>
</html>

profile
웹 개발자 공부 중
post-custom-banner

0개의 댓글