개발자 도전기
[Spring] JSP로 view 작성하기 본문
JSP
JSP(JavaServer Page)는 Java 웹 어플리케이션에서 사용자 인터페이스를 구현하기 위한 서버 측 view 기술 중 하나로, HTML 문서 내에 Java 코드를 삽입하여 동적으로 웹 페이지를 생성하는 데 사용된다. 예를 들어, 데이터베이스에서 정보를 가져와 테이블에 표시하거나, 사용자의 입력을 받아 처리하는 기능을 구현할 때 사용한다
JSP 사용방법
Model 객체의 attributeValue를 불러오기 위해서는 ${} 내부에 attributeName을 넣는다
<h1>${name1}</h1>
<%-- ${attribute명}을 작성하면
해당 attribute명에 매핑된 attribute value가 출력됨 --%>
배열의 경우
배열 사용법과 유사하게 attributeName[index] 기입
<h1>배열 사용법</h1>
<h1>${car[0]}</h1>
<h1>${car[1]}</h1>
<h1>${car[2]}</h1>
List의 경우
배열 사용법과 동일
<h1>${myList[0]}</h1>
<h1>${myList[1]}</h1>
<h1>${myList[2]}</h1>
Map의 경우
attributeName["key"]와 attributeName.key의 두 가지 방법이 있다
<h1>${myMap["name"]}</h1> <%-- attributeName["key"] --%>
<h1>${myMap["age"]}</h1>
<%-- attributeName.key --%>
<h1>${myMap.name}</h1>
<h1>${myMap.age}</h1>
JavaBean의 경우
JavaBean의 속성(property)는 getter, setter의 메소드의 get, set을 제거하고 앞글자를 소문자로 바꾼 것으로
작성법은 Map과 유사하다
<%-- ${attributeName.propertyName} --%>
<h1>${person.name}</h1>
<h1>${person.age}</h1>
<%-- ${attributeName["propertyName"]} --%>
<h1>${person["name"]}</h1>
<h1>${person["age"]}</h1>
'개발공부 > Spring' 카테고리의 다른 글
[Spring] Redirect (0) | 2024.04.18 |
---|---|
[Spring] PreparedStatement를 이용해서 데이터베이스 출력하기 (0) | 2024.04.17 |
[Spring] 핸들러(Handler) - @RequestParam, @ModelAttribute (0) | 2024.04.16 |
[JSP] EL 연산자와 JSTL (0) | 2024.04.09 |
[Spring] Spring MVC Framework (0) | 2024.04.09 |