1 / 40

JSTL

JSTL. Lec - 43. JSTL (ni). Acronym of J avaServer Pages S tandard T ag L ibrary JSTL (like JSP) is a specification, not an implementation Official reference implementation http://jakarta.apache.org/taglibs/doc/standard-doc/intro.html Development theme No Scriptlets.

hammer
Download Presentation

JSTL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JSTL Lec - 43

  2. JSTL (ni) • Acronym of • JavaServer Pages Standard Tag Library • JSTL (like JSP) is a specification, not an implementation • Official reference implementation • http://jakarta.apache.org/taglibs/doc/standard-doc/intro.html • Development theme • No Scriptlets

  3. JSTL Overview (ni) • JSTL tags are valid XML • JSTL contains action (tags) for common tasks • Iteration • Session Tracking • Redirect • XML • SQL etc. Remember – the development theme is “no scriptlets”

  4. Organization of the platform Your web pages Your application EL JSTL JavaServer Pages (JSP) JavaServletAPI Java language

  5. Why JSTL? (ni) • JSTL tags provide a standard implementation for typical application functionality • Reusability • Avoid reinventing the wheel • Another mechanism for avoiding the use of JSP scripting elements • EL considerably simple than Java

  6. JSTL & EL (ni) • JSTL includes support for EL • To replace the JSP expressions • EL is also standard part of JSP 2.0 • EL can only be used in attributes of JSTL tags prior to JSP 2.0 • With JSP 2.0 and onwards, it can be used anywhere in the document

  7. JSTL Tag libraries • Composed of 4 standard tag libraries • Core • Conditions, Control flow & variables access etc. • Internationalization / format • Locale messages • Text, numbers & date formation • XML • XML parsing / processing • SQL • Tags for accessing an SQL database

  8. Twin Tag Libraries • JSTL comes in two flavors • Request Time (RT) version • Dynamic attribute values are specified using JSP expression (i.e. <%= expression %> ) • Expression Language (EL) version • Dynamic attribute values are specified using JSTL expression language (i.e. ${ expression } ) • Why ?

  9. JSTL Tag libraries (cont.) • EL based

  10. JSTL Tag libraries (cont.) • RT based

  11. Using JSTL • Use taglib directive, as we used for custom tag library • For example • To use EL based core tag library <%@taglibprefix = “c”uri = http://java.sun.com/jsp/jstl/core%> • To use RT based core tag library <%@taglibprefix = “c_rt”uri = http://java.sun.com/jsp/jstl/core_rt%>

  12. Working with Core Actions (tags)

  13. Core Tag Library • Support actions • Manipulation of scoped variables • Output • Conditional logic • loops • URL manipulation • and Handling errors.

  14. Core Tag Library • c:set [1] • Provides a tag based mechanism for creating and setting scope based variables • Syntax: <c:setvar=“name”scope = “scope”value = “expression”/> • var Specifies the name of the scoped variables • scopepage | request | session | application  Optional and defaults to page • value Specifies the value to be bound to the variable  If evaluates to NULL, attribute/var will be removed if exist.

  15. Core Tag Library • c:set [2] • Examples <c:setvar= “timezone” value = “Asia / Karachi” /> <c:setvar= “email” scope = “request” value = “me@gmail.com” /> <c:setvar= “email” scope = “page” value = “${param.email }”/> <input type = “text” name = “email” />

  16. Core Tag Library • Using c:set forJavaBeans & Map [1] • Syntax: <c:settarget=“beanOrMap”property =“propertyOrKey” value = “value”/> Target must not be NULL If target is a bean, sets the value of the property, equivalent to <jsp:setProperty()> If target is a Map, sets the value of the key

  17. Core Tag Library • Using c:set forJavaBeans [2] • Example <jsp:useBean id=“person” class=“vu.PersonInfo” scope=“request” /> <c:settarget=“person”property =“name”value = “ali”/>

  18. Core Tag Library • c:out [1] • Equivalent to JSP expression i.e. <%= expression %> • Syntax: <c:outvalue=“expression”default = “expression”/> • value Evaluates the value attribute and outputs the result as string • defaultPrints it if the value attribute evaluates to null or empty string Optional

  19. Core Tag Library • c:out [2] • Examples: <c:outvalue = “Hello” /> <c:outvalue = “${param.num}” default = “0” /> Is equivalent to: <% String no = request.getParameter(“num”); if (no == null ) System.out.println(no); %> <c:outvalue= “${person.name}” default = “Not Set” />

  20. Core Tag Library • c:remove • Used to delete a scoped variable • Syntax: <c:removevar= “name”scope = “scope”/> • Examples: <c:removevar= “square” /> <c:removevar= “email”scope = “request” />

  21. Core Tag Library • c:forEach [1] • Used for iteration purpose • Supports two different styles of iteration • Iteration over an integer range • Iteration over a collection

  22. Core Tag Library • c:forEach [2] (ni) • Iteration over an Integer Range [1] • Like java language’s for statement • Syntax: <c:forEachvar=“name” begin=“expression” end=“expression” step=“expression”/> Body Content </c:forEach> If omitted, by default 1

  23. Core Tag Library • c:forEach [3] (ni) • Iteration over an Integer Range [2] • Example: To generate squares corresponding to range of integer values <c:forEachvar=“x” begin=“0”end=“10”step=“2” /> <c:out value=“${x * x}” /> </c:forEach>

  24. Core Tag Library • c:forEach [4] • Iteration over a Collection [1] • Can loop down on arrays, strings, list & map etc. • Syntax: <c:forEachvar=“name” items=“expression” /> Body Content </c:forEach> ArrayList HashMap Arrays etc.

  25. Core Tag Library • c:forEach [5] • Iteration over a Collection [2] • Example: To iterate over a String array • <% • for(int i=0; i<messages.length; i++) { • String message = messages[i]; • %> • <%= message %> • <% • } // end for • %> • <c:forEachvar=“message” items=“${messages}”> • <c:out value=“${message}” /> • </c:forEach> JSP without JSTL JSP after JSTL

  26. Core Tag Library • c:forEach [6] • Iteration over a Collection [3] • Example: To iterate over a persons ArrayList, contains PersonInfo objects • <% • ArrayList persons = (ArrayList)request.getAttribute(“pList”) • for(int i=0; i<persons.size(); i++) { • PersonInfo p == (PersonInfo)persons.get(i); • String name = p.getName(); • %> • <%= name %> • <% • } // end for • %> Automatic Type Conversion to appropriate type • <c:forEachvar=“p” items=“${persons}”> • <c:out value=“${p.name}” /> • </c:forEach> JSP without JSTL Type cast needed JSP after JSTL

  27. Exampole code Addressbook(MVC) using core tags netBeans project – jstl_ex2

  28. Core Tag Library (ni) • c:if • Used to conditionally process the body content • Syntax: <c:iftest= “expression”/> Body content </c:if> • Example: <c:iftest= “${a==b}” /> a equals b </c:if>

  29. Core Tag Library (ni) • c:choose [1] • Enables mutually exclusive conditionals • Syntax: <c:choose> <c:when test= “expression” > Body content </c:when> ……………. <c:otherwise > Body content </c:otherwise> </c:choose> -- Must appear at-least once -- Only one<c:when> is processed whose test evaluates to true -- Can appear at-most once -- Only execute if all <c:when> tests evaluate to false

  30. Core Tag Library (ni) • c:choose [2] • Example: <c:choose> <c:when test= “${a == b}” /> a equal b </c:when> <c:when test= “${a == c}” /> a equal c </c:when> <c:otherwise /> Don’t know what ‘a’ equal </c:otherwise> </c:choose>

  31. Working with SQL Actions (tags)

  32. SQL Tag Library • Support actions • To interact with relational databases • Issuing queries & updates • transactions etc.

  33. SQL Tag Library • sql:setDataSource [1] • Used for specifying data source • Syntax: <sql:setDataSource driver=“driver_name” url = “url” user = “user” password =“pwd” />

  34. SQL Tag Library • sql:setDataSource [2] • Example: To connect with Microsoft Access database <sql:setDataSource driver = “sun.jdbc.odbc.JdbcOdbcDriver” url = “jdbc:odbc:PersonDSN” user = “” password = “” />

  35. SQL Tag Library • sql:query • Used to execute queries • Syntax: <sql:query sql = “expression” var = “name” scope = “scope” /> -- Results of query stored -- Optional attribute to specify where to store var

  36. SQL Tag Library • sql:query [2] • Example: executing a simple select query & processing results <sql:querysql = “SELECT * FROM PersonInfo” var = “res” /> <c:forEach var = “row” items=“${res.rows}” > <c:out value = “${row.name}” /> <c:out value = “${row.address}” /> </forEach> Contains results of query

  37. Exampole code Addressbook(MVC) using core & sql tags netBeans project – jstl_ex

  38. SQL Tag Library (ni) • Problems • Heinous violation of MVC design pattern • DB code (i.e. raw SQL) doesn’t belong in the presentation layer

  39. Insert slide that contains only jsp pages – addressbook using only jsp pages

  40. Bringing it all together (lec 45 slide) • Java provides these mechanisms for internet programming • Applets • Servlets • Java Server pages • Scripting elements • JSP Standard Tag library • JSTL Expression language • Java Beans • (Enterprise Java Beans) • Ultimately leads to easier, faster, and more powerful web application development?

More Related