728x90
반응형
JSTL:
Jsp Standard Tag Library
directive :지시자
page, include, taglib
prefix : 접두사 tag에 접두사를 붙여서 태그의 종류를 결정한다.
c:set core
<body>
<h1>ex03-core.jsp</h1>
<hr>
<h3>c:set == setAttribute(var, value)</h3>
<c:set var="test1" value="hello" scope="page" />
<c:set var="test2" value="world" scope="request" />
<c:set var="test3" value="Java" scope="session" />
<c:set var="test4" value="JSP" scope="application" />
<ul>
<li>${pageScope.test1 }</li>
<li>${requestScope.test2 }</li>
<li>${sessionScope.test3 }</li>
<li>${applicationScope.test4 }</li>
</ul>
</body>
Member 객체 준비
package ex03;
public class Member {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
다시 core로 와서
객체를 useBean으로 불러와서
EL태그로 선언
<h3>특정 객체에 대해 속성(필드)를 지정할 수도 있다.</h3>
<!-- 기본생성자 -->
<jsp:useBean id="m1" class="ex03.Member" />
<ul>
<li>m1.name : ${m1.name }</li>
<li>m1.age : ${m1.age }</li>
</ul>
<c:set target="${m1 }" property="name" value="이지은" />
<c:set target="${m1 }" property="age" value="31" />
<ul>
<li>m1.name : ${m1.name }</li>
<li>m1.age : ${m1.age }</li>
</ul>
<c:remove var="m1" scope="page"/> <%--removeAttribute(m1) --%>
<ul>
<li>m1.name : ${m1.name }</li>
<li>m1.age : ${m1.age }</li>
<li>m1.name : ${m1.name }</li>
</ul>
c:set target으로 객체를 지정해서 해당 파라미터에대한 값을 지정할 수 있다.
c:if
JSTL의 조건문
<body>
<h1>ex04-if.jsp</h1>
<hr>
<% request.setCharacterEncoding("UTF-8"); %>
<c:if test="${pageContext.request.method == 'GET' }">
<form method="POST">
<p><input type="text" name="name" placeholder="이름"></p>
<p><input type="submit"></p>
</form>
</c:if>
<c:if test="${pageContext.request.method == 'POST' }">
<h3>안녕하세요 !! ${param.name }</h3>
<a href="ex04-if.jsp"><button>다시</button></a>
</c:if>
</body>
pageContext에서 request로 참조해서
그 request의 요청방식이 get이면 입력폼을 띄우고
요청방식이 POST라면 객체의 이름을 띄운다.
c:choose
JSTL의 스위치문
<body>
<h1>ex05-choose.jsp</h1>
<hr>
<c:choose>
<c:when test="${empty param.age }">
<form>
<p><input type="number" name="age" placeholder="나이입력"></p>
</form>
</c:when>
<c:when test="${param.age >= 20 and param.age <=100 }">
<h3>당신은 성인입니다.</h3>
</c:when>
<c:when test="${0 < param.age and param.age < 20}">
<h3>당신은 미성년자 입니다.</h3>
</c:when>
<c:otherwise>
<h3>범위를 초과했습니다.</h3>
</c:otherwise>
</c:choose>
<a href="ex05-choose.jsp"><button>다시</button></a>
</body>
c:when , otherwise 중 조건에 맞는 하나의 케이스만 출력한다.
c:forEach
JSTL의 반복문
Test 객체준비
package ex06;
import java.util.ArrayList;
public class Test {
private ArrayList<String> list = new ArrayList<String>();
public ArrayList<String> getList() {
return list;
}
public boolean add(String data) {
return list.add(data);
}
public boolean remove(String data) {
return list.remove(data);
}
}
ex06-forEach.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="ex06.Test"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>ex06-forEach.jsp</h1>
<hr>
<jsp:useBean id="test" class="ex06.Test" scope="application" />
<c:if test="${empty param.data }">
<form>
<p><input type="text" name="data" placeholder="추가할 데이터 입력"></p>
</form>
<ul>
<c:forEach var="data" items="${test.getList() }" varStatus="st">
<li>${st.index } : ${data }</li>
</c:forEach>
</ul>
</c:if>
<c:if test="${not empty param.data }">
<c:set var="list" value="${test.getList() }" />
<h3>추가 결과 : ${list.add(param.data) }</h3>
<a href="ex06.jsp"><button>다시 입력</button></a>
</c:if>
</body>
core를 사용하기 위한 taglib 선언
class파일 import
useBean 객체 아이디 test 지정 .
클래스 할당. scope = 어트리뷰트 범위 (application = 서버를 중단할때까지 데이터보관)
param.data가 비어있으면 입력창 출력
c:forEach로 list에 담긴 index값 반복문으로 출력
만약 비어있지않다면 not empty
list.add함수를 param.data를 매개변수로 받아서 list에 저장
forEach에서 반복문 사용하기
<body>
<h1>ex08-forEach</h1>
<hr>
<h3>forEach에서 숫자를이용하여 반복문 사용하기</h3>
<ul>
<c:forEach var="i" begin="1" end="9" step="1">
<li> 7x${i } = ${7 * i }</li>
</c:forEach>
</ul>
<h3>step은 0보다 작거나 같을 수 없다.</h3>
<ul>
<c:forEach var="i" begin="0" end="5" step="1">
<li>${5-i }</li>
</c:forEach>
</ul>
</body>
begin = 반복문 시작
end = 몇번돌릴지
step = 증가
forTokens
delims를이용해서 문자열 split기능을 쓸 수 있다.
<body>
<%@ page import="java.io.File, java.util.Scanner, java.util.ArrayList" %>
<%
String path;
String filePath = application.getRealPath("passwd.txt");
out.print(filePath);
File f = new File(filePath);
Scanner sc = new Scanner(f);
ArrayList<String> list = new ArrayList<>();
while(sc.hasNextLine()) {
String line = sc.nextLine();
list.add(line);
}
sc.close();
pageContext.setAttribute("list", list);
%>
<table>
<thead>
<tr>
<th>계정이름</th>
<th>비밀번호</th>
<th>UID</th>
<th>GID</th>
<th>comment</th>
<th>HomeDirectory</th>
<th>Shell</th>
</tr>
</thead>
<c:forEach var="line" items="${list }">
<tbody>
<tr>
<c:forEach var="ob" items="${line.split(':') }">
<td>${ob }</td>
</c:forEach>
</tr>
</tbody>
</c:forEach>
</table>
</body>
getRealPath로 passwd.txt가 있는 절대경로를 찾아서.
파일객체를 생성해서 담아준다.
스캐너에 파일 객체 f를 담아준다.
새로운 ArrayList 객체를 생성해서.
while문으로 다음줄이 있으면 변수 line을 list에 추가해준다.
forEach문으로 line을 선언해주고 반복문을 돌릴 items는 list를 쓸것이다.
이중 forEach문 안에
<td> ${ob} </td>
var를 ob로 새로 선언해서 출력한 line을 문자열 ":" 를기준으로 잘라준다.
728x90
반응형
'JSP' 카테고리의 다른 글
session (0) | 2023.08.18 |
---|---|
Connection Pool (커넥션 풀) , singleton(싱글톤) (0) | 2023.08.10 |
EL 태그 (1) | 2023.08.04 |
영화정보를 El태그, action태그를 사용해서 추가하기 (0) | 2023.08.04 |
Form - action 태그 (0) | 2023.08.04 |