티스토리 뷰

정리 노트/JSP

8주차-5일

eyoadgkn 2023. 12. 1. 11:57

setProperty 액션 태그

setProperty 랙션 태그는 useBean 액션 태그와 함께 자바빈즈의 setter() 메서드에 접근해 자바빈즈의 멤버 변수인 프로퍼티의 값을 저장하는 태그입니다. setProperty 태그는 폼 페이지로부터 전달되는 요청 파라미터의 값을 직접 저장하거나 자바빈즈의 프로퍼티로 변경해 값을 저장을 할 수 있습니다.
<jsp:setProperty name="자바빈즈 식별이름" property="프로퍼티 이름" value="값"/>

<%@ 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="jspbook2.Person" scope="request"/>
	<%
		person.setId(1234567);
		person.setName("홍길동");
	%>
	<p> 아이디 : <%=person.getId() %>
	<p> 이 름 : <%=person.getName() %>

	<jsp:include page="useBean03.jsp"/>
</body>
</html>

 

id를 person으로 설정합니다.
Person 클래스에 해당 값들을 넣고 get()메서드를 통해 해당 값들을 불러옵니다.
include를 통한 해당 페이지를 가지고 옵니다.

 

<%@ 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="jspbook2.Person" scope="request" />
	<jsp:setProperty name="person" property="id" value="123456"/>
	<jsp:setProperty name="person" property="name" value="홍길동"/>
	
	<p> 아이디 : <% out.println(person.getId()); %>
	<p> 이 름 : <% out.println(person.getName()); %>
	
</body>
</html>

 

자바빈즈로 person 클래스를 사용하도록 useBean 액션 태그를 작성합니다.
useBean 액션 태그의 id 속성 값을 통해 자바빈즈 person의 프로퍼티 id와 name에 값을 저장하도록 setProperty액션 태그를 작성합니다.
useBean 액션 태그의 id속성값을 통해 person 클래스의 getId(),getName() 메서드를 호출해 반환값을 out 내장 객체 변수를 이용해 출력하도록 스크립틀릿 태그에 작성합니다.

 

 

getProperty액션 태그는 useBean 액션 태그와 함께 자바빈즈의 Getter() 메서드에 접근해 자바빈즈의 멤버 변수인 property의 값을 가져오는 태그로 사용 방법은

<jsp:getProperty name="자바빈즈 식별이름" property="프로퍼티 이름"/>

name은 useBean 태그에 id 속성 값으로 설정된 자바빈즈를 식별하기위한 이름입니다.
property는 자바빈즈의 프로퍼티 이름입니다.

<%@ 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="jspbook2.Person" scope="request"/>
	<p> 아이디 : <jsp:getProperty name="person" property="id"/>
	<p>  이 름 : <jsp:getProperty name="person" property="name"/>
	
</body>
</html>

 

자바빈즈로 person 클래스를 사용하도록 useBean 액션 태그를 작성합니다.
useBean 액션 태그의 id 속성 값을 통해 자바빈즈 person의 프로퍼티 id와 name에 값을 가져와서 출력하도록 getProperty 액션태그를 작성합니다.
<%@ 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="jspbook2.Person"/>
	<jsp:setProperty property="id" name="person" value="11111"/>
	<jsp:setProperty property="name" name="person" value="홍길동"/>
	<p> 아이디 : <jsp:getProperty property="id" name="person"/>
	<p> 이 름 : <jsp:getProperty property="name" name="person"/>
</body>
</html>

 

 

'정리 노트 > JSP' 카테고리의 다른 글

9주차-2일  (0) 2023.12.05
9주차-1일  (0) 2023.12.04
8주차-4일  (0) 2023.11.30
8주차-3일 스크립트 태그,디렉티브 태그  (0) 2023.11.29
8주차-2일  (0) 2023.11.28
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/09   »
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
글 보관함