728x90
반응형
자바빈즈 액션 태그의 기능과 사용법
자바빈즈
- 동적 콘텐츠 개발을 위해 자바 코드를 사용하여 자바 클래스로 로직을
작성하는 방법 - JSP 페이지에서 화면을 표현하기 위한 계산식이나 자료의 처리를 담당하는
자바코드를 따로 분리하여 작성하는 것 - JSP 페이지가 HTML과 같이 쉽고 간단한 코드만으로 구성
자바빈즈를 작성할 때 규칙
- 자바 클래스는 java.io.Serializable 인터페이스를 구현해야 함
- 인수가 없는 기본 생성자가 있어야 함
- 모든 멤버 변수인 프로퍼티는 private 접근 지정자로 설정해야 함
- 모든 멤버 변수인 프로퍼티는 getter/setter( ) 메소드가 존재해야 함
- getter( ) 메소드는 멤버 변수에 저장된 값을 가져올 수 있는 메소드이고,
- setter( ) 메소드는 멤버 변수에 값을 저 장할 수 있는 메소드임
useBean 액션 태그
- JSP 페이지에서 자바빈즈를 사용하기 위해 실제 자바 클래스를 선언하고초기화하는 태그
- id 속성과 scope 속성을 바탕으로 자바빈즈의 객체를 검색하고, 객체가 발견되지 않으면 빈 객체를 생성
자바빈즈 Person을 생성하고 useBean 액션 태그에 Person 클래스를 사용하여 아이디와 이름 출력
java class
package ch04;
public class Person {
private int id = 20181104;
private String name = "홍길순";
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
<%@ 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 id="person" class="ch04.Person" scope="request"/>
<p> 아이디 : <%=person.getId() %></p>
<p> 이 름 : <%=person.getName() %></p>
</body>
</html>
- 출력 -
위에 있는 코드 활용
<%@ 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 id="person" class="ch04.Person" scope="request"/>
<p> 아이디 : <%=person.getId() %></p>
<p> 이 름 : <%=person.getName() %></p>
<%
person.setId(20182005);
person.setName("홍길동");
%>
<jsp:include page="useBean03.jsp" />
</body>
</html>
- 출력 -
setProperty 액션 태그
- useBean 액션 태그와 함께 자바빈즈의 setter( ) 메소드에 접근하여 자바빈즈의 멤버 변수인 프로퍼티의 값을 저장하는 태그
- 폼 페이지로부터 전달되는 요청 파라미터의 값을 직접 저장하거나 자바빈즈의 프로퍼티로 변경하여 값을 저장할 수 있음
- 모든 자바빈즈 프로퍼티 이름과 동일하게 요청 파라미터를 설정할 수 있음
- 요청 파라미터 이름과 자바빈즈의 프로퍼티 이름이 일치하는 경우:
- 요청 파라미터 이름과 자바빈즈의 프로퍼티 이름이 일치하지 않는 경우:
- 요청 파라미터 이름과 자바빈즈의 프로퍼티 이름이 모두 일치하는 경우:
setProperty 액션 태그에 자바빈즈 Person으로 아이디와 이름을 설정하여 출력
<%@ 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 id="person" class="ch04.Person" scope="request" />
<jsp:setProperty name="person" property="id" value="20182005"/>
<jsp:setProperty name="person" property="name" value="홍길동" />
<p> 아이디 : <% out.println(person.getId()); %>
<p> 이 름 : <% out.println(person.getName()); %>
</body>
</html>
- 출력 -
getProperty 액션 태그
- useBean 액션 태그와 함께 자바빈즈의 getter( ) 메소드에 접근하여 자바빈즈의 멤버 변수인 프로퍼티의 값을 가져오는 태그
getProperty 액션 태그에 자바빈즈 Person을 이용하여 아이디와 이름을 가져와 출력
<%@ 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 id="person" class="ch04.Person" scope="request"/>
<p> 아이디 : <jsp:getProperty name="person" property="id" />
<p> 이 름 : <jsp:getProperty name="person" property="name" />
</body>
</html>
- 출력 -
getProperty 액션 태그에 자바빈즈 Person을 이용하여 아이디와 이름을 전달받아 출력
<%@ 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 id="person" class="ch04.Person" scope="request"/>
<jsp:setProperty name="person" property="id" value="20182005" />
<jsp:setProperty name="person" property="name" value="홍길동" />
<p> 아이디 : <jsp:getProperty name="person" property="id" />
<p> 이 름 : <jsp:getProperty name="person" property="name" />
</body>
</html>
- 출력 -
728x90
반응형
'JSP' 카테고리의 다른 글
JSP 웹 프로그래밍 - 내장 객체 02 (0) | 2023.02.28 |
---|---|
JSP 웹 프로그래밍 - 내장 객체 01 (2) | 2023.02.28 |
JSP 웹 프로그래밍 - 액션 태그 01 (0) | 2023.02.27 |
JSP 웹 프로그래밍 - 디렉티브 태그 (0) | 2023.02.27 |
JSP 웹 프로그래밍 - 웹 쇼핑몰 만들기 01 (시작 페이지, 한글 출력 및 페이지 모듈화, 상품 목록 표시, 상품 상세 정보 표시) (0) | 2023.02.27 |