이 둘은 jsp에서 문법을 좀더 간단하고 효율적으로 사용하기 위해 사용되는 문법들이다.
EL
- Expression Lanuge
- 값들을 간단하게 표시하기 위해 사용
- <%=값%> 대신 ${값}으로 사용 하는 것 -보다 간단하게 표현가능
- 예시 코드
<%@ page contentType="text/html;charset=utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page import="com.fastcampus.ch2.*" %>
<%
Person person = new Person(); // person은 지역변수로 저장됨
request.setAttribute("person", person); //EL로 쓸려면 이렇게 저장을 해줘야함, 지역변수를 바로 ${lv}로 사용 불가
request.setAttribute("name", "남궁성");
request.setAttribute("list", new java.util.ArrayList());
%>
<html>
<head>
<title>EL</title>
</head>
<body>
person.getCar().getColor()=<%=person.getCar().getColor()%> <br>
person.getCar().getColor()=${person.getCar().getColor()} <br>
person.getCar().getColor()=${person.car.color} <br>
name=<%=request.getAttribute("name")%> <br>
name=${requestScope.name} <br> //request의 맵 이름이 requestScope임
name=${name} <br> //뺴고 적어도 된다. 그러면 우선순위순으로 page,request,session,application순으로 검색해서 가져옴
id=<%=request.getParameter("id")%> <br>
id=${pageContext.request.getParameter("id")} <br> //저장소중 pageContext의 request에 접근
id=${param.id} <br>
"1"+1 = ${"1"+1} <br>
"1"+="1" = ${"1"+="1"} <br>
"2">1 = ${"2">1} <br>
null = ${null}<br> //EL은 null을 출력하지 않음
null+1 = ${null+1} <br>
null+null = ${null+null} <br>
"" + null = ${""+null} <br>
""-1 = ${""-1} <br>
empty null=${empty null} <br>
empty list=${empty list} <br>
null==0 = ${null==0} <br>
null eq 0 = ${null eq 0} <br>
name == "남궁성"=${name=="남궁성"} <br>
name != "남궁성"=${name!="남궁성"} <br>
name eq "남궁성"=${name eq "남궁성"} <br>
name ne "남궁성"=${name ne "남궁성"} <br>
name.equals("남궁성")=${name.equals("남궁성")} <br>
</body>
</html>
[실행결과]
person.getCar().getColor()=red
person.getCar().getColor()=red
person.car.color=red
name=남궁성
name=남궁성
name=남궁성
id=asdf
id=asdf
id=asdf
"1"+1 = 2
"1"+="1" = 11
"2">1 = true
null =
null+1 = 1
null+null = 0
"" + null = 0
""-1 = -1
empty null=true
empty list=true
null==0 = false
null eq 0 = false
name == "남궁성"=true
name != "남궁성"=false
name eq "남궁성"=true
name ne "남궁성"=false
name.equals("남궁성")=true
JSTL
- JSP Standard Tag Library
- JSTL을 안쓰면 아래 사진 처럼 코드를 짜야함 - html과 java가 블럭단위로 쪼개짐 -> 실수유발, 보기 불편함
- 동적 문법을 좀 더 보기 쉽고 간편하게 작성하기 위해 사용
- <c: 로 시작함
<%@ page contentType="text/html;charset=utf-8"%>
core라이브러리쓸려면 넣어야함
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
fmt 형식화출력 라이브러리 쓸려면 넣어야함
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<title>JSTL</title>
</head>
<body>
c 접두사를 이용
<c:set var="to" value="10"/>
<c:set var="arr" value="10,20,30,40,50,60,70"/>
for 문
<c:forEach var="i" begin="1" end="${to}">
${i}
</c:forEach>
<br>
if-else if
<c:if test="${not empty arr}">
<c:forEach var="elem" items="${arr}" varStatus="status"> //status는 count와 index를 가지고 있음, count는1부터 시작, index는 0부터 시작
${status.count}. arr[${status.index}]=${elem}<BR>
</c:forEach>
</c:if>
<c:if test="${param.msg != null}">
msg=${param.msg}
msg=<c:out value="${param.msg}"/> //<p>등의 태그등을 인식못하게함, 스크립트를 이용한 공격방어
</c:if>
<br>
<c:if test="${param.msg == null}">메시지가 없습니다.<br></c:if>
<c:set var="age" value="${param.age}"/>
<c:choose>
<c:when test="${age >= 19}">성인입니다.</c:when>
<c:when test="${0 <= age && age < 19}">성인이 아닙니다.</c:when>
<c:otherwise>값이 유효하지 않습니다.</c:otherwise>
</c:choose>
<br>
<c:set var="now" value="<%=new java.util.Date() %>"/>
Server time is <fmt:formatDate value="${now}" type="both" pattern="yyyy/MM/dd HH:mm:ss"/>
</body>
</html>
'I leaned > 스프링,스프링부트' 카테고리의 다른 글
URL Encoding (0) | 2023.05.22 |
---|---|
@RequestParam과 @ModelAttribute (0) | 2023.05.19 |
서블릿과 JSP (0) | 2023.05.18 |
OOP 5대 설계원칙 - SOLID (0) | 2023.05.17 |
Eclipse(STS)와 IntelliJ (0) | 2023.05.10 |