놀코에 오신 것을 환영합니다.

놀아보자 코드랑

KOSTA/WEB

21.06.23 - JSP element(2), jackson 라이브러리

놀코 2021. 6. 23. 18:17

깃허브 참고

https://github.com/sblee1031/Kosta/tree/main/ORACLE_DB_THEOTY/Oracle_DB_project/myweb

 

 

* JSP Action Tag 사용.

웹 프로젝트시 왼쪽 구조로 구현한다. / 우: 틀린 설계

 

JSP Action Tag 사용 적극 권장

 

useBean 태그는 사용 빈도 낮음. -> setProperty, getProperty로 사용 됨..

useBean, setProperty, getProperty 사용 안함.


자바빈(javaBean) - 자바컴포넌트(component)

 

* 조건

1. public 생성자

2. public 매개변수 없는 생성자 존재

3. 프러퍼티용 멤버변수는 public이어서는 안됨.

4.프러퍼티용 멤버변수전용 public getter, setter가 있어야함.

DTO.java클래스를 컴포넌트화 하여 재사용성을 높임


useBean.jsp

<%@page import="com.day.dto.OrderInfo"%>
<%@page import="com.day.dto.Customer"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%// 서블릿에서 할일
request.setAttribute("resultInt", 1);
request.setAttribute("resultCustomer", new Customer("id1","p1","n1"));

%>

<%//이동된 jsp에서 할일
//요청객체에서 (이름: "resultInt")값 얻기
//int resultIntValue = (Integer)request.getAttribute("resultInt"); //오토박싱, 언박싱
Integer resultIntegerValue = (Integer)request.getAttribute("resultInt");
if(resultIntegerValue != null){
	resultIntegerValue = resultIntegerValue.intValue();
}
%>
<%--//요청객체의 속성(이름:"resultCustomer")값 얻기
Customer resultC = (Customer)request.getAttribute("resultCustomer");
//요청객체가 null이면 매개변수 없는 생성자를 이용해 객체생성하기, 
//요청객체속성(이름:"resultCustomer", 값 : resultC)으로 추가하기
if(resultC ==null){
	resultC = new Customer();
	request.setAttribute("reusultCustomer", resultC);
}
--%>
<jsp:useBean id="resultCustomer" class="com.day.dto.Customer" scope ="request"/>

<%--resultC.setEnabled(0);--%>
<jsp:setProperty name="resultCustomer" property="enabled" value="0"/>" <!-- setter메서드 요청과 같은 효과임 -->

<%-- out.print(resultC.getId()); --%>
<jsp:getProperty name="resultCustomer" property="enabled"/>

<%--
//요청속성(이름:"orderInfo")얻기
//null인 경우 객체생성후 요청속성으로 추가
OrderInfo oi = (OrderInfo)request.getAttribute("orderInfo");
if(oi == null){
	oi = new OrderInfo();
	request.setAttribute("orderInfo", oi);
}
--%>
<jsp:useBean id = "orderInfo" class="com.day.dto.OrderInfo" scope="request"/>
<%--//요청속성(이름:"orderInfo")의 프로퍼티중  order_c값을 설정한다
Customer c = new Customer();
oi.setOrder_c(c);
--%>
<%--
<jsp:setProperty name="orderInfo" property="order_c" value = "new Customer()"/>  
--%>

<%--//요청속성(이름:"orderInfo")의 프로퍼티중 order_c값을 얻어온다
	Customer c1 = oi.getOrder_c();
	out.print(c1.getId());
 --%>


Servlet -> Jsp -> EL  JSTL(EL기반)

requestScope은 attribute이름의 값을 가져오는 것.

param은 request.getParameter이름의 값을 가져오는 것.

 

parameter와 attribute 비교

api parameter attribute
request request
servlet  
application application
메서드 getParameter getAttribute
  setAttribute
  removeAttribute
값의 자료형 string Object

JSTL 설치

https://mvnrepository.com/artifact/javax.servlet/jstl/1.2

.jar파일 : eclipse 프로젝트 內 / WEB-INF / lib폴더 ->임포트

 

jackson ver 2.11.3(core, annotation도 둘다 다운로드 버전동일)


과제

1. MVC패턴 구조 작성  localhost:8888/myback/productlist

https://github.com/sblee1031/Kosta/blob/main/ORACLE_DB_THEOTY/Oracle_DB_project/myweb/myback/src/com/day/control/ProductListServlet.java

https://github.com/sblee1031/Kosta/blob/main/ORACLE_DB_THEOTY/Oracle_DB_project/myweb/myback/WebContent/productlist.jsp


2. front / Back 분리 작성  localhost:8888/myfront/productlist.html

https://github.com/sblee1031/Kosta/blob/main/ORACLE_DB_THEOTY/Oracle_DB_project/myweb/myfront/WebContent/productlist.html

https://github.com/sblee1031/Kosta/blob/main/ORACLE_DB_THEOTY/Oracle_DB_project/myweb/mybackjson/src/com/day/control/JacksonServlet.java

결과화면