[Spring] EL과 JSTL 작성하기

임유빈·2022년 9월 16일

EL : 화면 출력방법

$ { } : 모델에 담겨있는 데이터를 화면에 출력할 수 있다.
$ {object.name} : 객체.필드 형태로 객체데이터를 출력한다.
$ {10+20} : 연산도 가능하다.

JSTL : 데이터 처리 화면 구성방법

JSTL는 상단에 태그 라이브러리가 있어야 동작하며, HTML 안에 JAVA 코드를 없애기 위해 사용한다.

값대입, 값출력

<c:set /> <c:out />
<!--변수 income에 value의 계산결과로 넣어준다.-->
<c:set var="income" value="${400*3}" />
<c:out value="${income}" />

if문

<c:if test="${income>8000 }">
	<p>8000 이상</p>
</c:if>

swith문

<c:choose>
	<c:when test="${income<=1000 }">
		1000보다 작다.
	</c:when>
	<c:when test="${income<=2000 }">
		2000보다 작다.
	</c:when>
	<c:when test="${income<=3000 }">
		3000보다 작다.
	</c:when>
	<c:otherwise>
		모두 조건에 안맞는다.
	</c:otherwise>

forEach문

<!-- j가 3이 될 때까지 반복한다-->
<c:forEach var="j" begin="1" end="3">
			<li>${j } <c:out value="${j }" /></li>
</c:forEach>

forEach문 연습


ArrayList<Human> list = new ArrayList<Human>();
list.add(new Human("홍길동1", "11"));
list.add(new Human("홍길동2", "22"));
list.add(new Human("홍길동3", "33"));
list.add(new Human("홍길동4", "44"));

model.addAttribute("list", list);        
        
<!--var : each문 돌린 list 값을 item에 대입 ( 출력시 item.name, item.age 형식으로)-->
<!--items : 위의 list를 받아온다.-->
<!--varStatus : 현재 상태를 보여준다(반복문이 몇번째 객체를 가져왔는지)-->
<c:forEach var="item" items="${list }" varStatus="status">
	index:${status.index }
</c:forEach>


<!--begin : 1부터 시작-->
<!--end : 10까지 each문 실행-->
<!--step : step="N" N의 값만큼 건너띄기-->
<c:forEach var="cnt" begin="1" end="10" step="2" varStatus="status">
${status.index }:${status.count }:${status.first }:${status.last }:${cnt }
</c:forEach>

not : 값이 true면 false로 출력되고 false면 true로 출력

<c:forEach var="item" items=${list } varStatus="status">
	${item }<c:if test="${not status.last }">,</c:if>
</c:forEach>
	

forTokens : delime 기준으로 문자열 구분하기

<c:forTokens var="city" items="서울.인천,대구.부산" delims=",">
${city } <br>
</c:forTokens>


<c:forTokens var="city" items="서울.인천,대구.부산" delims=",.">
${city } <br>
</c:forTokens>

3자리 수마다 컴마로 표시하기

<fmt:formatNumber value="1234567.89" />
<!--결과-->
1,234,567.89 

3자리 수마다 컴마로 표시 하지 않기

<fmt:formatNumber value="1234567.89" groupingUsed="false" />
<!--결과 groupingUsed="false"-->
1234567.89

숫자를 퍼센트로 출력

<fmt:formatNumber value="0.5" type="percent" />
<!--결과 1은 100%-->
 50%

돈단위를 넣어서 출력

<fmt:formatNumber value="10000" type="currency" />
<!--결과 브라우저 세팅 언어에 따라 돈단위가 뜬다-->
₩10,000

원하는 돈표시 선택

<fmt:formatNumber value="10000" type="currency" currencySymbol="$" />
<!--결과-->
$10,000

패턴대로 출력하는 방식

<!--#자리에 0이 오면 출력되지 않는다. 0을 출력할 땐 0으로 표시한다.-->

<fmt:formatNumber value="1234567.8912345" pattern="#,#00.0#" />
<!--결과-->
1,234,567.89

<fmt:formatNumber value="1234567.8912345" pattern=".000" />
<!--결과-->
1234567.891

<fmt:formatNumber value="1234567.10" pattern=".00" />
<!--결과-->
1234567.10

<fmt:formatNumber value="1234567.10" pattern=".0#" />
<!--결과-->
1234567.1

<fmt:formatNumber value="1234567.0" pattern=".0" />
<!--결과-->
1234567.0

<fmt:formatNumber value="1234567.0" pattern=".#" />
<!--결과 #자리가 0임에도 출력된 이유 : 소수점 자리인 것을 인식하기 위해 #=0이여도 출력된다.-->
1234567.0

<c:set var="now" value="<%=new java.util.Date()%>" />
<fmt:formatDate value="${now }" pattern="yyyy" />
<!--결과-->
2022
<fmt:formatDate value="${now }" pattern="yyyy년 MM월 dd일  HH시 mm ss" />
<!--결과-->
2022년 09월 16일 12시 26 50

0개의 댓글