ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • JSP JavaBeans
    JSP 프로그래밍 2019. 12. 28. 19:07

    먼저 JavaBeans 란..

     

    자바 클래스 중 , 자바 빈즈 규약에 맞게 작성된 클래스이며,

    멤버변수와 getter/setter 메소드로 이루어져 있다.

    값을 저장하는 Value Object (VO) 로 활용된다.

    JSP 페이지 안에서는 자바빈즈를 사용할 수 있도록 하는

    액션태그와 페이지를 활용할수 있도록 하는 액션태그가 있다.

     

     

    useBean 액션태그 번째,

     

    형식 : <jsp:useBean id="Bean 이름" scope="범위" class="Bean 의 저장위치" />

    여기서 id 는 객체 인스턴스를 식별하는 이름이고

    scope 는 객체 참조의 유효범위(default : page) 이며,

    class 는 완전한 형태의 클래스 이름이다.

    액션태그 형식을 자바코드로 생각해보면 아래와 같다.

    <jsp:useBean id="ap" class="mypackage.MyClass"/>

    ==> mypackage.MyClass ap = new mypackage.MyClass();

     

     

    setProperty 액션태그 번째,

     

    setProperty 액션태그는 Bean 의

    값을 설정하는 태그이다.

    형식: <jsp:setProperty name="Bean 이름" property="Property 이름" value="저장할 값" />

    여기서 name 은 <jsp:useBean> 태그에 정의된 Bean 인스턴스 이름이고

    property 는 값을 설정하고자 하는 빈 속성의 이름, "*" 설정시 모든 것을 설정한다.

    value 는 Bean 속성에 설정할 값이다.

     

     

    getProperty 액션태그 번째,

     

    getProperty 액션태그는 Bean 의 속성값을 얻는데 사용한다.

    형식 : <jsp:getProperty name = "Bean 이름" property="property 이름" />

    여기서 name 은 속성을 얻고자 하는 Bean 인스턴스의 이름이고,

    property 는 얻고자하는 속성의 이름이다.

     

     

    바로 예제를 통해 알아보자 번째,

     

    /*
    위에서 설명한 JavaBeans 규약에 맞는,
    Value Object (VO) 를 먼저 만들자.
    위치는 src/sample/SimpleData.java
    */
    public class SimpleData {                    
        private String message;                  
                                                 
        public String getMessage() {             
            return message;                      
        }                                        
                                                 
        public void setMessage(String message) { 
            this.message = message;              
        }                                        
    }                                            

     

    <!-- web/beans/simpleForm -->
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <body>
    <h1>Simple JavaBeans Program..!</h1>
    <hr color="red"><br><br>
    <form method="post" action="simpleBean.jsp">
        Message : <input type="text" name="message">
        <input type="submit" value="전송">
    </form>
    </body>
    </html>
    
    
    <!-- web/beans/simpleBean.jsp -->
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%
        request.setCharacterEncoding("UTF-8");
        //String message = request.getParameter("message");
    %>
    
    <jsp:useBean id="msg" class="sample.SimpleData"/>
    // ==> sample.SimpleData msg = new sample.SimpleData();
    <jsp:setProperty name="msg" property="message"/>
    // ==> msg.setMessage();
    
    <html>
    <head>
        <title>Simple Beans result</title>
    
    </head>
    <body>
    <h1>Simple Beans Result..</h1>
    <hr color="red"><br><br>
    <font size="5">
        메세지 : <jsp:getProperty name="msg" property="message"/>
        <%--<%=message%> --%>
    </font>
    </body>
    </html>

     

    'JSP 프로그래밍' 카테고리의 다른 글

    JSP Action Tag (include)  (0) 2019.12.28
    JSP Action Tag (forward)  (0) 2019.12.28
    JSP 내장객체  (0) 2019.12.26
    JSP 지시어 , 에러처리  (0) 2019.12.23
    JSP 시작하기  (0) 2019.12.23

    댓글

Designed by Tistory.