본문 바로가기

JAVA/JSP & Servlet

표준 액션(standard action) : useBean (다형성)

 

3개의 자바빈을 생성

 

 - ProductInfo.java

 - BookInfo.java

 - ClothingInfo.java

 

 

 

 

 

ProductInfo.java

package mall;

 

abstract public class ProductInfo {

        private String code;

        private String name;

        private int price;

       

        public String getCode() {

               return code;

        }

        public void setCode(String code) {

               this.code = code;

        }

        public String getName() {

               return name;

        }

        public void setName(String name) {

               this.name = name;

        }

        public int getPrice() {

               return price;

        }

        public void setPrice(int price) {

               this.price = price;

        }

 

}

 

 

BookInfo.java

package mall;

 

public class BookInfo extends ProductInfo {

        private short page;

        private String writer;

       

        public short getPage() {

               return page;

        }

        public void setPage(short page) {

               this.page = page;

        }

        public String getWriter() {

               return writer;

        }

        public void setWriter(String writer) {

               this.writer = writer;

        }

}

 

 

 

ClothingInfo.java

package mall;

 

public class ClothingInfo extends ProductInfo {

       

        private char size;

        private String color;

       

        public void setSize(char size){

               this.size = size;

        }

 

        public String getColor() {

               return color;

        }

 

        public void setColor(String color) {

               this.color = color;

        }

 

        public char getSize() {

               return size;

        }

}

 

 

 

 


 

 

 

3종류의 jsp 페이지 생성

 

- ProductInfo.jsp

- BookInfo.jsp

- ClothingInfo.jsp

 

 

ProductInfo.jsp : 부모

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

 

<!-- 사용하는 자바빈의 부모가 abstract(추상class) 경우 type="" -->

<!-- 사용하는 자바빈의 부모가 일반class 경우 class="" -->

 

<jsp:useBean id="pinfo" type="mall.ProductInfo" scope="request" />

 

코드 : <jsp:getProperty name="pinfo" property="code" /><br/>

제품명 : <jsp:getProperty name="pinfo" property="name" /><br/>

가격 : <jsp:getProperty name="pinfo" property="price" /><br/>

 

 

 

 

BookInfo.jsp : 실행

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<jsp:useBean id="pinfo" class="mall.BookInfo" scope="request">

        <jsp:setProperty name="pinfo" property="code" value="50001" />

        <jsp:setProperty name="pinfo" property="name" value="의뢰인" />

        <jsp:setProperty name="pinfo" property="price" value="9000" />

        <jsp:setProperty name="pinfo" property="writer" value=" 그리샴" />

        <jsp:setProperty name="pinfo" property="page" value="704" />

</jsp:useBean>

 

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>표준액션 : useBean -다형성-</title>

</head>

<body>

책정보가 저장되었습니다 <br><br>

===============================<br>

<h3>제품 정보</h3>

<jsp:include page="ProductInfo.jsp" />

 

</body>

</html>

 

 

 

 

 

 

 

ClothingInfo.jsp : 실행

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<jsp:useBean id="pinfo" class="mall.ClothingInfo" scope="request">

        <jsp:setProperty name="pinfo" property="code" value="70002" />

        <jsp:setProperty name="pinfo" property="name" value="목폴라 " />

        <jsp:setProperty name="pinfo" property="price" value="10000" />

        <jsp:setProperty name="pinfo" property="size" value="M" />

        <jsp:setProperty name="pinfo" property="color" value="베이지" />

</jsp:useBean>

 

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>표준액션 : useBean -다형성-</title>

</head>

<body>

의류정보가 저장되었습니다 <br><br>

===============================<br>

<h3>제품 정보</h3>

<jsp:include page="ProductInfo.jsp" />

 

 

</body>

</html>