6/1 el언어

리무 rimu ·2023년 6월 1일
0

DataBase

목록 보기
6/8

requestScope 소거가능
myString만 사용하면 속성을 쓸 수 있는 애들 순서대로 확인해서 있으면 가져옴
여기 4개 다 없으면 null이 됨
표현식은 null이라고 표현을 해주지만 el은 결과가 아무것도 안나오게 됨

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">

<title>elEx.jsp</title>
</head>
<body>
<%
	pageContext.setAttribute("myString","aaa");
	request.setAttribute("myString", null);
	
	Some s = new Some(100, "myValue");
	session.setAttribute("some", s);
%>

<%
	String str = (String)request.getAttribute("myString");
%>
<%= str %>
<%-- pageContext => request => session => application --%>
${ requestScope.myString }
<hr>
<%
	Some temp = (Some)session.getAttribute("some");
%>
<%= temp.getNum() %> <%= temp.getStr() %>

</body>
</html>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">

<title>elEx.jsp</title>
</head>
<body>
<%
	pageContext.setAttribute("myString","aaa");
	request.setAttribute("myString", null);
	
	Some s = new Some(100, "myValue");
	session.setAttribute("some", s);
%>

<%
	String str = (String)request.getAttribute("myString");
%>
<%= str %>
<%-- pageContext => request => session => application --%>
${ requestScope.myString }
<hr>
<%
	Some temp = (Some)session.getAttribute("some");
%>
<%= temp.getNum() %> <%= temp.getStr() %>
${ sessionScope.some.num }
$( some.str }
<hr>
<%= application.getInitParameter("pname") %>
${ initParam.pname }
${ param.myParam }
<%-- 쿠키 뽑아쓸때 --%>
<%-- ${ cookie.cookieName } --%>
</body>
</html>

회사마다 쓰는 방식이 다르다 el방식 or 표현식/스크립트릿
el언어에서는 pageContext밖에 사용하지 못함

수식표현도 가능함! true/false 구분가능~

복수데이터, 컬렉션, 배열

JSTL 다운받기

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

이거 다운받기!

원래 아파치 lib 에 넣어도 되는데 가능한 외부라이브러리는 프로젝트안에 lib에 넣는걸 습관들여야 함!

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import = "kr.ac.green.Some" %>
<%@ page import = "java.util.*" %>

<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">

<title>elEx.jsp</title>
</head>
<body>
<%
	pageContext.setAttribute("myString","aaa");
	request.setAttribute("myString", null);
	
	Some s = new Some(100, "myValue");
	session.setAttribute("some", s);
	
	Vector<String> vec = new Vector<String>();
	vec.add("a");
	vec.add("b");
	vec.add("c");
	vec.add("d");
	
	request.setAttribute("list", vec);
	
	Map<String, String> map = new Hashtable<String, String>();
	map.put("aKey", "aValue");
	map.put("aKey", "aValue");
	map.put("aKey", "aValue");
	map.put("aKey", "aValue");
	request.setAttribute("map", map);
	
	Vector<String> otherList = new Vector<String>();
	request.setAttribute("otherList", otherList);
	
	request.setAttribute("arr", new int[]{});
%>

<%-- 2번째를 가져올수 있음 --%>
<%-- null입니까? null검사 가능 / null 이니 true 값이 나옴 --%>
${ list[2] } ${ map["cKey"] } ${ empty otherList }
<hr>
<%--배열의 길이를 0으로 만드는 이유가 있을까? 비었는지 안비었는지의 검사를 안함 null인지 아닌지만 검사 --%>
<%-- 배열은 검사가 안됨!! --%>
${ list[2] } ${ map["cKey"] } ${ not empty otherList } $ { empty arr }
<hr>
<%-- map도 되고 Vector도 됨 / 비지 않았습니까? --%>
 ${ not empty otherList }

<%-- 조건문이랑 반복문을 만들어보자 / 코드로 만들 수 있는걸 태그로 만듬 --%>
<%--jspl은 외부 라이브러리 --%>

<hr>
<%
	String str = (String)request.getAttribute("myString");
%>
<%= str %>
<%-- pageContext => request => session => application --%>
${ requestScope.myString }
<hr>
<%
	Some temp = (Some)session.getAttribute("some");
%>
<%= temp.getNum() %> <%= temp.getStr() %>
${ sessionScope.some.num }
$( some.str }
<hr>
<%= application.getInitParameter("pname") %>
${ initParam.pname }
${ param.myParam }
<%-- 쿠키 뽑아쓸때 --%>
<%-- ${ cookie.cookieName } --%>
<hr>
<%-- pageContext는 모든 객체를 다 뽑을 수 있음 / 쓸 수 있는게 pageContext밖에 없음 getRequest한 결과--%>
${ pageContext.request.contextPath}
<hr>
${ 4 < 3 } ${ 3 == 3 }

</body>
</html>

이렇게는 사용 못함

조건문 사용하는 법


우리가 아는 for문과는 다르게 역순은 안됨(역방향으론 안됨)

<%
	ArrayList<String> list = new ArrayList<String>();
	list.add("a");
	list.add("b");
	list.add("c");
	list.add("d");
	request.setAttribute("list", list);
%>
<br>
<c:forEach items = "${ list }" var = "myString">
	${ myString }

업로드중..

여기서 forEach는

status는 index번호 반복횟수 뽑아낼 때 유용

list.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="java.util.*" %>
<%@ page import="kr.ac.green.Book" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>list.jsp</title>
</head>
<body>
	<h1>list</h1>
	<a href="add.book">책 등록하기</a>
	<hr>
	<form action="findByTitle.book" method="get">
		제목으로 검색할 수 있어요 : <input type="text" name="findTitle" />
		<input type="submit" />
	</form>
	<hr>
	<table>
		<tr>
			<th>번호</th>
			<th>제목</th>
			<th>저자</th>			
		</tr>
		<c:choose>
			<c:when test="${ empty list }">
			<tr>
				<td colspan="3">no data</td>
			</tr>
			</c:when>
			<c:otherwise>
				<c:forEach items="${ list }" var="book">
				<tr onclick="showBookInfo(${book.num})">
					<td>${book.num}</td>
					<td>${book.title}</td>
					<td>${book.writer}</td>
				</tr>		
				</c:forEach>
			</c:otherwise>
		</c:choose>
		<tr>
			<th colspan ="3">
				<c:forEach var="i" begin="1" end="${ pageCount }">
					<c:choose>
						<c:when test="${ i == pageNum }">
							[${ i }]
						</c:when>
						<c:otherwise>
							<a href="list.book?pageNum=${ i }">[${ i }]</a>						
						</c:otherwise>
					</c:choose>
				</c:forEach>
			</th>
		</tr>
	</table>
	<script>
		function showBookInfo() {
			location.href="view.book?num=" + arguments[0];
		}
	</script>
</body>
</html>



profile
JAVA / SQL / Spring 을 공부하고 있습니다 🐥

0개의 댓글