jstl 1 - 碁峰資訊epaper.gotop.com.tw/pdfsample/acl016200.pdf · jstl jstl el java script java...

53
JSTL 1.1 JSTL JavaServer Pages Standard Tag Library 1.1 JSTL JCP Java Community Process Java Web Web JSTL EL Web Java Scripting JSTL URL U18N XML SQL JSTL JSTL 7-1 JSTL 1.1 7-2 Core tag library 7-3 i18n i18n-capable formatting tags library 7-4 SQL SQL tag library 7-5 XML XML tag library 7-6 Functions tag library

Upload: phamngoc

Post on 02-Jul-2018

253 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

JSTL 1.1

JSTL JavaServer Pages Standard Tag Library

1 . 1 J S T L J C P J a v a

Community Process

Java Web

Web JSTL EL Web

Java Scripting

JSTL

U R L

U18N XML SQL JSTL

JSTL

7-1 JSTL 1.1

7-2 Core tag library

7-3 i18n i18n-capable formatting tags

library

7-4 SQL SQL tag library

7-5 XML XML tag library

7-6 Functions tag library

Page 2: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

JavaServer Pages Standard Tag Library 1.1 JSP

JSTL

XML

JSTL

1. Core tag library

2. I18N I18N-capable formatting tag library

3. SQL SQL tag library

4. XML XML tag library

5. Functions tag library

JSTL EL Expression Language

JSP

7-2

7-1 JSTL 1.1

JSTL

c <c:out>

URI

http://java.sun.com/jsp/jstl/core

SQL sql <sql:query>http://java.sun.com/jsp/jstl/sql

XML xml <x:forBach>http://java.sun.com/jsp/jstl/fmt

fn <fn:split>http://java.sun.com/jsp/jstl/

functions

I18N fmt <fmt:format

Date>

http://java.sun.com/jsp/jstl/xml

<%= userList.getUser().getPhoneNumber() %>

Page 3: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

JSTL

JSTL EL

Java Script

Java

7-1-1 JSTL 1.1

JSTL 1.1 Servlet 2.4 JSP 2.0 Container

JSTL Apache Jakarta Project

http://jakarta.apache.org/builds/jakarta-taglibs/releases/standard/

JSTL 1.1 JSTL 1.1 jakarta-taglibs-

standard-current.zip

7-3

Chapter 7 JSTL 1.1

<c_rt:out value="<%= userList.getUser( ).getPhoneNumber( ) %>" />

<c:out value="${userList.user.phoneNumber}" />

7-1 jakarta-taglibs-standard-1.1.0-B1

Page 4: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

lib jstl.jar standard.jar Tomcat WEB-INF\lib

JSP JSTL .jar tld

WEB-INF

- HelloJSTL.jsp

HelloJSTL.jsp

7-4

lib jstl.jar standard.jar old-dependencies

JSTL 1.0 JSTL 1.1

tld TLD JSTL 1.0 TLD

c-1_0.tld c-1_0-rt.tld

<%@ page contentType="text/html;charset=big5" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>

<head>

<title> JSTL </title>

</head>

<body>

<c:out value=" JSTL "/>

</br>

:

</br>

<c:out value="${header['User-Agent']}"/>

</body>

</html>

Page 5: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

HelloJSTL.jsp Core

EL header JSP JSTL

JSTL

(p re f i x ) c u r i

http://java.sun.com/jsp/jstl/core

out value ${header['User-

Agent']} User-Agent

HelloJSTL.jsp 7-2

7-5

Chapter 7 JSTL 1.1

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

JSTL 1.0 uri http://java.sun.com/jstl/core

JSTL 1.1 jsp/ JSTL 1.1 JSTL 1.0 1.1

uri http://java.sun.com/jstl/core

JSTL 1.0

<c:out value=" JSTL " />

<c:out value="${header['User-Agent']}" />

7-2 HelloJSTL.jsp

Page 6: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

taglib uri web.xml

uri http://www.javaworld.com.tw/jstl/core web.xml

<taglib-uri> URI <taglib-location>

TLD <%@ taglib %>

7-1-2 JSTL 1.1 VS JSTL 1.0

JSTL 1.0 JSTL 1.1

1. EL JSTL 1.0 EL JSP 2.0

JSTL 1.1 EL JSTL EL

2. JSTL 1.0 EL RT JSTL 1.1

EL RT

7-6

<web-app>

<jsp-config>

<taglib>

<taglib-uri>http://www.javaworld.com.tw/jstl/core</taglib-uri>

<taglib-location>/WEB-INF/tld/c.tld</taglib-location>

</taglib>

</jsp-config>

</web-app>

<%@ taglib prefix="c" uri="http://www.javaworld.com.tw/jsp/jstl/core" %>

Page 7: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

EL

Expression Language

RT

Scriptlet

Java

EL

3. JSTL 1.1 (functions)

fn:contains fn:containsIgnoreCase fn:endsWith fn:indexOf fn:join

fn:length fn:replace fn:split fn:startsWith fn:substring

JSTL 1.1 B Changes

7-1-3 standard-examples

jakarta-taglibs-standard-current.zip 7-1

standard-examples.war Tomcat webapps

Tomcat webapps standard-examples

IE URL http://localhost:8080/standard-

examples

7-7

Chapter 7 JSTL 1.1

Page 8: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

JSTL

General Purpose Tags

Conditional Tags

Iterator Tags

Import Tags

I18N & Formatting Tags

XML Tags

SQL Tags

Functions

Tag Library Validators

Miscellaneous

7-8

7-3 standard-examples

Page 9: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

JSTL

Core

URL

7-9

Chapter 7 JSTL 1.1

7-2 Core tag library

Core

out

set

remove

catch

if

choose

when

otherwise

forEach

forTokens

URL

import

param

url

param

redirect

param

Page 10: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

JSP JSTL <%@ taglib %>

prefix uri

JSTL

7-2-1

<c:out> <c:set> <c:remove>

<c:catch>

<c:out>

<c:out> <%= scripting-language %>

1 body

<c:out value="value" [escapeXml="{true|false}"] [default="defaultValue"] />

7-10

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

JSTL

JSTL

Hello ! <c:out value="${username}" />

Page 11: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

2

<c:out value="value" [escapeXml="{true|false}"]>

default value

</c:out>

Null

value null default default

<c:out> < > ' " & &lt; &gt;

&#039; &#034; &amp; <c:out>

escapeXml fasle

7-11

Chapter 7 JSTL 1.1

value Object

EL

Y

default ObjectYvalue null

default

escapeXml booleanY true<

&lt;

EL EL Y

attribute = "${ }" N

Page 12: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

1. Hello JSP 2.0 !!

2. 8

3. data data data

null No Data

4. <p> </p>

5.

<c:set>

<c:set> JSP JavaBean

1 value scope varName

7-12

Entity

< &lt;

> &gt;

' &#039;

" &#034;

& &amp;

<c:out value="Hello JSP 2.0 !! " />

<c:out value="${ 3 + 5 }" />

<c:out value="${ param.data }" default="No Data" />

<c:out value="<p> </p>" />

<c:out value="<p> </p>" escapeXml="false" />

Page 13: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

<c:set value="value" var="varName" [scope="{ page|request|session|

application }"]/>

2 scope varName

<c:set var="varName" [scope="{ page|request|session|application }"]>

...

</c:set>

3 value target

<c:set value="value" target="target" property="propertyName" />

4 target

<c:set target="target" property="propertyName">

...

</c:set>

Null

3 4

target null

target java.util.Map JavaBean7-13

Chapter 7 JSTL 1.1

value Object

EL

Y

var StringN

scope StringN pagevar JSP

property StringYtarget

target ObjectYJavaBean java.util.Map

Page 14: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

value null

1 var scope

scope PageContext.removeAttribute(varName, scope)

scope PageContext.removeAttribute(varName)

3

target Map Map.remove(property)

target JavaBean property null

<c:set> var scope

scope="session" session

<c:set> scope Page

1+1 2 number <c:set> value

value <c:set> </c:set>

7-14

var scope

scope="${ourScope}" var="${username}"

<c:set var="number" scope="session" value="${1 + 1}"/>

Page 15: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

<c:out value="${1+1}" /> 2 <%=1+1%>

<c:set> body value

<c:set> body

number 1 + 1 1 + 1

1. 2 Request number

2. 8 Session number

3. ${param.number} null Request number

${param.number} null ${param.number} Request

number

7-15

Chapter 7 JSTL 1.1

<c:set var="number" scope="session">

<c:out value="${1+1}" />

</c:set>

<c:set var="number" scope="session">

1 + 1

</c:set>

<c:set var="number" scope="request" value="${1 + 1}" />

<c:set var="number" scope="session" />

${3 + 5}

</c:set>

<c:set var="number" scope="request" value="${ param.number }" />

<c:set target="User" property="name" value="${ param.Username}" />

Page 16: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

4. ${param.Username} null User JavaBean name

null ${param.Username} null ${param.Username}

User JavaBean name setter

<c:remove>

<c:remove>

<c:remove var="varName" [scope="{ page|request|session|application }"] />

<c:remove> var scope

7-16

3 ${param.number} null Request

number

var String

EL

N

scope StringN pagevar JSP

<c:remove var="number" scope="session" />

Page 17: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

number Session scope <c:remove>

Page Request Session Application

number

<c:set> <c:remove>

Core_set_remove.jsp

Core_set_remove.jsp

7-17

Chapter 7 JSTL 1.1

<%@ page contentType="text/html;charset=Big5" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>

<head>

<title>CH7 - Core_set_remove.jsp</title>

</head>

<body>

<h2><c:out value="<c:set> <c:remove> " /></h2>

<c:set scope="page" var="number">

<c:out value="${1+1}"/>

</c:set>

<c:set scope="request" var="number">

<%= 3 %>

</c:set>

<c:set scope="session" var="number">

Page 18: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

7-18

4

</c:set>

<table border="1" width="30%">

<tr>

<th>pageScope.number</th>

<td><c:out value="${pageScope.number}" default="No Data" /></td>

</tr>

<tr>

<th>requestScope.number</th>

<td><c:out value="${requestScope.number}" default="No Data" /></td>

</tr>

<tr>

<th>sessionScope.number</th>

<td><c:out value="${sessionScope.number}" default="No Data" /></td>

</tr>

</table></br>

<c:out value='<c:remove var="number" scope="page" /> '/>

<c:remove var="number" scope="page" />

<table border="1" width="30%">

<tr>

<th>pageScope.number</th>

<td><c:out value="${pageScope.number}" default="No Data" /></td>

</tr>

<tr>

<th>requestScope.number</th>

<td><c:out value="${requestScope.number}" default="No Data" /></td>

</tr>

<tr>

<th>sessionScope.number</th>

Page 19: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

Page Request Session

number <c:remove var="number" scope="page" /> Page

number <c:remove var="number" />

number Core_set_remove.jsp

7-19

Chapter 7 JSTL 1.1

<td><c:out value="${sessionScope.number}" default="No Data" /></td>

</tr>

</table></br>

<c:out value='<c:remove var="number" /> '/>

<c:remove var="number" />

<table border="1" width="30%">

<tr>

<th>pageScope.number</th>

<td><c:out value="${pageScope.number}" default="No Data" /></td>

</tr>

<tr>

<th>requestScope.number</th>

<td><c:out value="${requestScope.number}" default="No Data" /></td>

</tr>

<tr>

<th>sessionScope.number</th>

<td><c:out value="${sessionScope.number}" default="No Data" /></td>

</tr>

</table>

</body>

</html>

Page 20: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

<c:catch>

<c:catch>

<c:catch [var="varName"] >

...

</c:catch>

7-20

7-4 Core_set_remove.jsp

var String

EL

N

Page 21: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

<c:catch> <c:catch> </c:catch>

varName

<c:catch> </c:catch> <c:catch>

</c:catch>

Core_catch.jsp <c:catch>

Core_catch.jsp

7-21

Chapter 7 JSTL 1.1

<c:catch var="message">

//

</c:catch>

<%@ page contentType="text/html;charset=Big5" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>

<head>

<title>CH7 - Core_catch.jsp</title>

</head>

<body>

<h2><c:out value="<c:catch> " /></h2>

<c:catch var="error_Message">

<%

Page 22: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

<c:catch>

error_Message <c:out>

7-5

<c:catch>

<c:catch> </c:catch> 7-6

7-22

String eFormat = "not number";

int i = Integer.parseInt(eFormat);

%>

</c:catch>

${error_Message}

</body>

</html>

7-5 Core_catch.jsp

Page 23: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

7-2-2

<c: i f> <c:choose> <c:when>

<c:otherwise>

<c:if>

<c:if> if

1 body

<c:if test="testCondition" var="varName" [scope="{page|request|session|

application}"]/>

7-23

Chapter 7 JSTL 1.1

7-6 Core_catch.jsp <c:catch> </c:catch>

Page 24: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

2

<c:if test="testCondition" [var="varName"] [scope="{page|request|session|

application}"]>

</c:if>

<c:if> test test true

false ${param.username = = 'admin'}

param.username admin true admin

false

admin "ADMIN !! // body "

<c:if> body "ADMIN !! body "

<c:if> JSP Scriptlet JSP

HTML

7-24

test boolean

EL

Ytrue

false

var StringNt e s t

true false

scope StringN pagevar JSP

<c:if test="${param.username = = 'admin' }">

ADMIN !! //body

</c:if>

Page 25: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

test <c:if> var scope <c:if>

var scope var

var scope

Core_if.jsp

Core_if.jsp

7-25

Chapter 7 JSTL 1.1

<%@ page contentType="text/html;charset=Big5" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>

<head>

<title>CH7 - Core_if.jsp</title>

</head>

<body>

<h2><c:out value="<c:if> " /></h2>

<c:if test="${param.username == 'Admin'}" var="condition" scope="page">

Admin

</c:if></br>

:${condition}

</body>

</html>

Page 26: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

username Admin

condition true pageScope false condition

JSTL condition

${condition} ${pageScope.condition} Core_if.jsp

7-7

<c:choose>

<c:choose> <c:when> <c:otherwise>

<c:choose>

( <when> <otherwise> )

</c:choose>

7-26

core_if.jsp ?username=Admin

7-7 Core_if.jsp

Page 27: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

<c:choose>

1 <c:when>

0 <c:otherwise>

<c:when> <c:otherwise>

<c:choose>

7-27

Chapter 7 JSTL 1.1

<c:choose>

<c:when>

</c:when>

<c:otherwise>

</c:otherwise>

</c:choose>

Page 28: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

<c:when>

<c:when> when

<c:when test="testCondition" >

</c:when>

<c:when> <c:choose> </c:choose>

<c:choose> <c:when> <c:otherwise>

<c:when> test test true

false

<c:otherwise>

<c:choose> <c:when>

<c:otherwise>

7-28

test boolean

EL

Ytrue

false

Page 29: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

<c:otherwise>

</c:otherwise>

<c:otherwise> <c:choose> </c:choose>

<c:choose> <c:otherwise>

<c:choose> <c:when> test true

<c:otherwise>

<c:choose> <c:when> <c:otherwise>

7-29

Chapter 7 JSTL 1.1

<c:choose>

<c:when test="${condition1}">

condition1 true

</c:when>

<c:when test="${ condition2}">

condition2 true

</c:when>

Page 30: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

condition1 true "condition1 true" condition1

false condition2 true "condition2 true"

false "condition1 condition2 false"

7-2-3

Iterate <c:forEach> <c:forTokens>

<c:forEach>

<c:forEach> (Collection)

<c:forEach>

7-30

<c:otherwise>

condition1 condition2 false

</c:otherwise>

</c:choose>

condition1 condition2 true "condition1

true" <c:choose> <c:when>

<c:when>

Page 31: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

1

<c:forEach [var="varName"] items="collection" [varStatus="varStatusName"]

[begin="begin"] [end="end"] [step="step"]>

</c:forEach>

2

<c:forEach [var="varName"] [varStatus="varStatusName"] begin="begin"

end="end" [step="step"]>

</c:forEach>

7-31

Chapter 7 JSTL 1.1

var String

EL

N

begin intY 0

step intY 1

end intY

varStatus StringN

items Arrays

Collection

Iterator

Enumera-tion

Map

String

Y

Page 32: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

begin begin 0

end begin

step step 0

Null

items null

begin items

items

Core_forEach.jsp

Core_forEach.jsp

7-32

<%@ page contentType="text/html;charset=Big5" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>

<head>

<title>CH7 - Core_forEach.jsp</title>

</head>

<body>

<h2><c:out value="<c:forEach> " /></h2>

Page 33: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

atts Request

<c:forEach> items

var <c:out> item

7-8

7-33

Chapter 7 JSTL 1.1

<%

String atts[] = new String [5];

atts[0]="hello";

atts[1]="this";

atts[2]="is";

atts[3]="a";

atts[4]="pen";

request.setAttribute("atts", atts);

%>

<c:forEach items="${atts}" var="item" >

${item}</br>

</c:forEach>

</body>

</html>

7-8 Core_forEach.jsp

Page 34: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

<c : fo rEach> J2SE

ArrayList List LinkedList Vector Stack Set

java.util.Map HashMap Hashtable Properties Provider

Attributes

<c:forEach> begin end step begin

( 0) end step

Core_forEach1.jsp

7-34

varName <c:forEach>

varName ${item} </c:forEach>

<c:forEach items="${atts}" var="item" >

</c:forEach>

${item}</br>

${item} item

<%@ page contentType="text/html;charset=Big5" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>

<head>

<title>CH7 - Core_forEach1.jsp</title>

</head>

<body>

Page 35: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

<c:forEach> atts 2 5

atts[1] atts[3]

7-9

7-35

Chapter 7 JSTL 1.1

<h2><c:out value="<c:forEach> begin end step " /></h2>

<%

String atts[] = new String [5];

atts[0]="hello";

atts[1]="this";

atts[2]="is";

atts[3]="a";

atts[4]="pen";

request.setAttribute("atts", atts);

%>

<c:forEach items="${atts}" var="item" begin="1" end="4" step="2" >

${item}</br>

</c:forEach>

</body>

</html>

7-9 Core_forEach1.jsp

Page 36: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

begin end step

begin end step

1. begin end

7-36

<%

int atts[] = {1,2,3,4,5,6,7,8,9,10};

request.setAttribute("atts", atts);

%>

<c:forEach items="${atts}" var="item" begin="0" end="9" step="1" >

${item}</br>

</c:forEach>

begin

-

end

-

step

- 1 2 3 4 5 6 7 8 9 10

5 - - 6 7 8 9 10

- 5 - 1 2 3 4 5 6

- - 5 1 6

5 5 - 6

5 5 5 6

0 8 2 1 3 5 7 9

0 8 3 1 4 7

0 8 4 1 5 9

15 20 -

20 8 -

0 20 - 1 2 3 4 5 6 7 8 9 10

Page 37: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

2. begin end

3. end end

4. <c:forEach>

items items

begin end

Core_forEach2.jsp

begin end

1 10

7-37

Chapter 7 JSTL 1.1

<%@ page contentType="text/html;charset=Big5" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>

<head>

<title>CH7 - Core_forEach2.jsp</title>

</head>

<body>

<h2><c:out value="<c:forEach> " /></h2>

<c:forEach begin="1" end="10" var="item" >

${item}</br>

</c:forEach>

</body>

</html>

Page 38: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

10 item Core_forEach2.jsp

7-10

step step 2 7-11

<c:forEach> varStatus

varStatus="s" s

varStatus index count first last

7-38

7-10 Core_forEach2.jsp

7-11 step 2

Page 39: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

varStatus

Core_forEach3.jsp

7-39

Chapter 7 JSTL 1.1

index number

count number

first boolean

last boolean

<%@ page contentType="text/html;charset=Big5" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>

<head>

<title>CH7 - Core_forEach3.jsp</title>

</head>

<body>

<h2><c:out value="<c:forEach> varStatus " /></h2>

<%

String atts[] = new String [5];

atts[0]="hello";

atts[1]="this";

atts[2]="is";

atts[3]="a";

atts[4]="pen";

request.setAttribute("atts", atts);

%>

Page 40: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

7-12

7-40

<c:forEach items="${atts}" var="item"varStatus="s">

<h2><c:out value="${item}"/> </h2>

index ${s.index}</br>

count ${s.count}</br>

first ${s.first}</br>

last ${s.last}</br>

</c:forEach>

</body>

</html>

7-12 Core_forEach3.jsp

Page 41: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

<c:forTokens>

<c:forTokens>

delimiters

<c:forTokens items="stringOfTokens" delims="delimiters" [var="varName"]

[varStatus="varStatusName"] [begin="begin"] [end="end"] [step="step"]>

</c:forTokens>

begin begin 0

end begin

step step 0

7-41

Chapter 7 JSTL 1.1

var String

EL

N

items StringY

delims StringN

varStatus StringN

begin intY 0

step intY 1

end intY

Page 42: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

Null

items null

begin items

<c:forTokens> begin end step var varStatus <c:forEach>

items delims items

delims items

<c:forTokens>

ABCDE (,)

5 5 A,B,C,D,E (,)

items EL

7-42

<c:forTokens items="A,B,C,D,E" delims="," var="item" >

${item}

</c:forTokens>

<%

String phoneNumber = "123-456-7899";

request.setAttribute("userPhone", phoneNumber);

%>

<c:forTokens items="${userPhone}" delims="-" var="item" >

${item}

</c:forTokens>

Page 43: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

1234567899 123-456-7899 (-)

3 item

delims

ABCDE delims

<c:forEach>

ABCDE <c:forEach> delims

<c:forEach> <c:forEach>

(,) <c:forTokens> delims (,)

<c:forTokens> <c:forEach>

7-2-4 URL

JSTL URL <c : impo r t>

<c:redirect> <c:url>

url

7-43

Chapter 7 JSTL 1.1

<c:forTokens items="A,B;C-D,E" delims=",;-" var="item" >

${item}

</c:forTokens>

<c:forEach items="A,B,C,D,E" var="item" >

${item}

</c:forEach>

Page 44: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

<c:import>

<c:import> JSP JSP Action

<jsp:include> <jsp:include> web

application <c:import> web application

web application

1

<c:import url="url" [context="context"] [var="varName"] [scope="{page|

request|session|application}"] [charEncoding="charEncoding"]>

</c:import>

2

<c:import url="url" [context="context"] varReader="varReaderName"

[charEncoding="charEncoding"]>

</c:import>

7-44

url String

EL

Y

scope StringN Pagevar JSP

context StringYContainer web

/

var StringN

String

Page 45: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

Null

url null JspException

<c:import> url

<c:import> http://java.sun.com

<c: impor t> FTP FTP

ftp.javaworld.com.tw data.txt

Hello.jsp <c:import>

webapps <c:import>

/ web Tomcat

webapps hello.txt webapps/examples/images

context examples hello.txt

JSP 7-45

Chapter 7 JSTL 1.1

charEncoding String

EL

Y

varReader StringN

Reader

<c:import url="http://java.sun.com" />

<c:import url="ftp://ftp.cse.yzu.edu.tw/data.txt" />

<c:import url="Hello.jsp" />

Page 46: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

web

context web others

others jsp index.html

<c:import> var scope var

String

varName scope varName

7-46

<c:import url="images/hello.txt" />

<c:import url="/jsp/index.html" context="/others" />

web server.xml <Context>

crossContext true others

web

server.xml

<Context path="/others" docBase="others" debug="0"

reloadable="true" crossContext="true"/>

<c:import url="/images/hello.txt" var="s" scope="session" />

Page 47: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

<c:import> <c:param>

name value

EL

< c : i m p o r t > < c : p a r a m >

Core_import.jsp Core_imported.jsp

Core_import.jsp

7-47

Chapter 7 JSTL 1.1

name String

EL

Y

value StringY

<c:import url="http://java.sun.com" >

<c:param name="test" value="1234" />

</c:import>

http://java.sun.com?test=1234

<%@ page contentType="text/html;charset=Big5" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>

<head>

<title>CH7 - Core_import.jsp</title>

</head>

Page 48: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

<c:set> <c:param>

Core_imported.jsp

7-48

<body>

<h2><c:out value="<c:import> " /></h2>

<c:set var="input1" value=" Core_imported.jsp "

scope="request"/>

core_imported.jsp <hr/>

<c:import url="Core_imported.jsp" >

<c:param name="input2" value=" <c:param> Core_imported.jsp "/>

</c:import><hr/>

${output/}

</body>

</html>

<%@ page contentType="text/html;charset=Big5" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<html>

<head>

<title>CH7 - Core_imported.jsp</title>

</head>

<body>

Page 49: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

core_imported.jsp core_import.jsp

input1

${input1} input2 ${param.input2}

<c:set> Core_import.jsp <c:param>

<c:param>

Core_import.jsp

7-49

Chapter 7 JSTL 1.1

<fmt:requestEncoding value="Big5" />

<c:set var="output1" value=" Core_import.jsp " scope="request"/>

${input}

<c:out value="${param.input2}"escapeXml="true"/>

</body>

</html>

7-13 Core_import.jsp

Page 50: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

<c:url>

<c:url> URL

1

<c:url value="value" [context="context"] [var="varName"] [scope="{page|

request|session|application}"] />

2 (Query String)

<c:url value="value" [context="context"] [var="varName"]

[scope="{page|request|session|application}"] >

<c:param>

</c:url>

7-50

value String

EL

YURL

context StringYContainer web

/

var StringN

String

scope StringN Pagevar JSP

<c:url value="http:// www.javaworld.com.tw " >

<c:param name="param" value="value"/>

</c:url>

Page 51: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

<c:url> <c:param>

http://www.javaworld.com.tw?param=value HTML

<a>

<c:url> context var scope context

<c:import> web <c:url>

var varName

<c:url>

web

<c:url>

<c:url> web

image code.gif

<c:redirect>

<c:redirect> JSP

1

<c:redirect url="url" [context="context"] />

7-51

Chapter 7 JSTL 1.1

<a href="

<c:url value="http:// www.javaworld.com.tw " >

<c:param name="param" value="value"/>

</c:url>"> Java </a>

<img src="<c:url value="/images/code.gif" />" />

Page 52: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

2 (Query String)

<c:redirect url="url" [context="context"] >

<c:param>

</c:redirect >

url

http://www.javaworld.com.tw

context web

/others /jsp/index.html

<c:redirect>

<c:param>

7-52

url String

EL

Y

context StringYContainer web

/

<c:redirect url="http://www.javaworld.com.tw" />

<c:redirect url="/jsp/index.html" context="/others" />

Page 53: JSTL 1 - 碁峰資訊epaper.gotop.com.tw/PDFSample/acl016200.pdf · JSTL JSTL EL Java Script Java 7-1-1 JSTL 1.1 JSTL 1.1 Servlet 2.4 JSP 2.0 Container JSTL Apache Jakarta Project

Core_redirect.jsp

http://

java.sun.com/?param=

value

7-53

Chapter 7 JSTL 1.1

<%@ page contentType="text/html;charset=Big5" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>

<head>

<title>CH7 - Core_redirect.jsp</title>

</head>

<body>

<h2><c:out value="<c:redirect> " /></h2>

<c:redirect url="http://java.sun.com">

<c:param name="param" value="value"/>

</c:redirect>

<c:out value=" !!!" />

</body>

</html>

7-14 Core_redirect.jsp