-
jsp와 java beansJava/jdbc 2022. 5. 26. 13:50
빈즈는 form태그에서 전달한것을 쉽게 처리하기 위해 있는 것이다.
1. 빈즈 클래스 구조
class xxxBean { // 멤버변수 : 데이터베이스 테이블의 칼럼 이름과 매칭된다. private String xxx; // get, set 메서드 : 멤버변수와 매칭된다. public String getXxx() { return xxx; } public setXxx(String xxx) { this.xxx = xxx; } }2. 빈즈 액션
액션 사용 예 기능 useBean <jsp:useBean scope="page" id="cls" class="xx.MyBean" /> xx패키지의 MyBean 클래스를 cls라는 이름으로 page 번위에서 사용할 것을 선언한다. setProperty <jsp:setProperty name="cls" property="xxx" /> useBean으로 선언된 빈즈 클래스의 setxxx()메서드를 호출한다. getProperty <jsp:getProperty name="cls" property="xxx" /> useBean으로 선언된 빈즈 클래스의 getxxx() 메서드를 호출한다. 3. 빈즈 선언
<jsp:useBean id="mybean" scope="request" class="MyBean" />
액션 속성 설명 useBean id 빈즈 클래스의 이름으로 사용할 변수다. class 빈즈 클래스의 클래스 이름으로, 패키지 경로를 포함한다. scope 빈즈 클래스의 범위로 page, request, session, application이 올 수 있다. useBean 액션의 자바 코드 변환:
//<jsp:useBean id="mybean" scope="request" class="MyBean" /> MyBean My Bean = (MyBean)request.getAttribute("mybean"); if(mybean == null) { mybean = new MyBean(); request.setAttribute("mybean", mybean); }//<jsp:useBean id="mybean" scope="page" class="MyBean" /> <% MyBean mybean = new MyBean(); %>4. 빈즈 속성 설정
<jsp:setProperty name="mybean" property="username" />
<jsp:setProperty name="mybean" property="userpasswd" />
<jsp:setProperty name="mybean" property="*" />
액션 속성 설명 setProperty name 빈즈 클래스의 인스턴스 이름으로, id 값에 설정했던 변수 이름이다. property 속성 값으로 빈즈 클래스의 setXxx 메서드와 매칭될 속성 값이다. *를 지정하면 모든 setXxx에 자동으로 매칭된다. setProperty 액션의 자바 코드 변환:
//<jsp:setProperty name="mybean" property="username" /> //<jsp:setProperty name="mybean" property="userpasswd" /> <% mybean.setUsername(request.getParameter("username")); mybean.setPasswd(request.getParameter("userpasswd")); %>5. 빈즈에서 속성 데이터 가져오기
<jsp:getProperty name="mybean" property="username" />
<jsp:getProperty name="mybean" property="userpasswd" />
액션 속성 설명 getProperty name 빈즈 클래스의 인스턴스 이름으로, id 값에 설정했던 변수 이름이다. property 속성 값으로 빈즈 클래스의 getXxx 메서드와 매칭될 속성 값이다. getProperty 액션의 자바 코드 변환:
//<TD><jsp:getProperty name="mybean" property="username" /></TD></TR> <TD><%= mybean.getUsername() %></TD></TR>'Java > jdbc' 카테고리의 다른 글
jsp 내장객체 (request, response, out, session 등) (0) 2022.05.19 JSP 기본문법 (주석, 지시어, 액션, 선언과 표현식, 스크립트릿) (0) 2022.05.12 서블릿, JSP (0) 2022.05.12 각종 에러들 (0) 2022.05.10 jdbc 프로그래밍 방법 (0) 2022.04.17