21
Slide 1 of 21 JSP Standard Tag Library

Session 7 : jstl - Giáo trình Bách Khoa Aptech

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Session 7 : jstl  - Giáo trình Bách Khoa Aptech

Slide 1 of 21

JSP Standard Tag Library

Page 2: Session 7 : jstl  - Giáo trình Bách Khoa Aptech

Slide 2 of 21

Overview JSTL Core Tag Library Methods using EL I18N & Formatting Tag Library SQL Tag Library XML Tag Library

Page 3: Session 7 : jstl  - Giáo trình Bách Khoa Aptech

Slide 3 of 21

JSTL Custom tags provide a way to reuse

valuable components JSTL has reusable standard set of tags. Provides the user with a script-free

environment User can write code using tags instead of

Java code and scriptlets JSTL tags are easier for non-programmers

and inexperienced programmers

Page 4: Session 7 : jstl  - Giáo trình Bách Khoa Aptech

Slide 4 of 21

JSTL - Types

JSP Standard Tag Library (JSTL)

Core Tag Library

I18N & Formatting Tag Library

SQL Tag Library

XML Tag Library

Page 5: Session 7 : jstl  - Giáo trình Bách Khoa Aptech

Slide 5 of 21

Core Tag Library

Core Tag Library

General Purpose Tags

Decision Making Tags

Iteration Tags

set remove out forEach forTokensif choose

Page 6: Session 7 : jstl  - Giáo trình Bách Khoa Aptech

Slide 6 of 21

General-purpose tags <%@ taglib uri =

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

– <c:set var=“varName” value=“value” scope=“page|request|session|application” />

– <c:remove var=“varName” scope=“page|request|session|application” />

– <c:out value=“value|expression” escapeXml=“true|false” default=“defaultValue” />

Page 7: Session 7 : jstl  - Giáo trình Bách Khoa Aptech

Slide 7 of 21

General-purpose tags-Example

Page 8: Session 7 : jstl  - Giáo trình Bách Khoa Aptech

Slide 8 of 21

Decision-making tags

– <c:if test =“testcondition” var=“varName” scope=“page | request | session | application”>

Body content

– </c:if>– <c:choose>

<c:when test=“testCondition”>

Body Content

</c:when>

<c:otherwise>

Body Content

Page 9: Session 7 : jstl  - Giáo trình Bách Khoa Aptech

Slide 9 of 21

if tags

Page 10: Session 7 : jstl  - Giáo trình Bách Khoa Aptech

Slide 10 of 21

choose tags

Page 11: Session 7 : jstl  - Giáo trình Bách Khoa Aptech

Slide 11 of 21

Iteration tags– <c:forEach var=“varName” item=“collection”

begin=“begin” end=“end” step=“step”>Body Content

– </c:forEach>

– <c:forTokens items=“stringofToken” delims=“delimiters” var=“varName” >

Body Content

– </c:forTokens>

Page 12: Session 7 : jstl  - Giáo trình Bách Khoa Aptech

Slide 12 of 21

Iteration tags - Example

Page 13: Session 7 : jstl  - Giáo trình Bách Khoa Aptech

Slide 13 of 21

SQL Tag Library

SQL Tag Library

setDataSource query update paramtransaction

Page 14: Session 7 : jstl  - Giáo trình Bách Khoa Aptech

Slide 14 of 21

SQL Tag Library – setDataSource tag <%@ taglib uri = "http://java.sun.com/jsp/jstl/sql" prefix

= “sql" %>– <sql:setDataSource dataSource=“datasource” |

url=“jdbcurl” driver=“jdbcclassdriver” user=“username” password=“password” var=“varName” scope=“page | request | session | application” />

Page 15: Session 7 : jstl  - Giáo trình Bách Khoa Aptech

Slide 15 of 21

SQL Tag Library – query tag– <sql:query var=“varName” dataSource=“datasource”

scope=“page | request | session | application”>

SQL Statement

Page 16: Session 7 : jstl  - Giáo trình Bách Khoa Aptech

Slide 16 of 21

SQL Tag Library – update tag– <sql:update var=“varName” dataSource=“datasource”

scope=“page | request | session | application”>

SQL Statement• <sql:param value=“value” />

– </sql:update>

Page 17: Session 7 : jstl  - Giáo trình Bách Khoa Aptech

Slide 17 of 21

SQL Tag Library – transaction tag– <sql:transaction dataSource=“datasource”

isolation=“isolationLevel”>• <sql:update> or <sql:query> Statements

– </sql:transaction>

Page 18: Session 7 : jstl  - Giáo trình Bách Khoa Aptech

Slide 18 of 21

SQL Tag Library – transaction tag

Page 19: Session 7 : jstl  - Giáo trình Bách Khoa Aptech

Slide 19 of 21

param Tag Param is used to set values for parameters

markers (“?”) in SQL statement It acts as a sub tag for <sql:query> and

<sql:update>

Page 20: Session 7 : jstl  - Giáo trình Bách Khoa Aptech

Slide 20 of 21

Summary

JSTL Core Tag Library

– General purpose Tags– Decision making Tags– Iteration Tags

Page 21: Session 7 : jstl  - Giáo trình Bách Khoa Aptech

Slide 21 of 21

Summary Methods using EL I18N & Formatting Tag Library

– Locale, Bundle, Message– formatDate, formatNumber

SQL Tag Library– setDataSource– query– update– transaction– param