Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

개발자 도전기

[Spring] JSP로 view 작성하기 본문

개발공부/Spring

[Spring] JSP로 view 작성하기

jnnjnn 2024. 4. 9. 19:55

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>