1 / 34

DT211/3 Internet Application Development

DT211/3 Internet Application Development. JSP: Actions elements and JSTL. So far, have looked at these. Introduction. Previously, identified that JSP provides a variety of techniques to enable dynamic processing:. Directive elements. Scripting elements. Action elements and JSTL.

brina
Download Presentation

DT211/3 Internet Application Development

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. DT211/3 Internet Application Development JSP: Actions elements and JSTL

  2. So far, have lookedat these Introduction • Previously, identified that JSP provides a variety of techniques to enable dynamic processing: • Directive elements • Scripting elements • Action elements and JSTL • Java Beans

  3. JSP Action elements • Action elements are an important syntax element in JSP • They are represented by tags (as is HTML) • They assist JSP developers to develop in tags rather thanscriplet programming • Instead of <%, they just use the < character (like HTML) <prefix:action_name>body </prefix:action_name >

  4. JSP Action elements • JSP tags have a “start tag”, a “tag body” and an “end tag”. • The start and end tag have the same name enclosed in < and > • The tag names have an embedded colon character “:” in them, the part before the colon (prefix) describes the type of the tag <prefix:action_name> body </prefix:action_name> • Part after the “:” is the Action Name • Note: Syntax of Action Elementags is based on XML

  5. JSP Action elements • Tags have associated attributes (like HTML e.g. <img src = “..”) • Full syntax of JSP Action Elements is: • <prefix:action_name attr1 = “value” attr2 = “value2” action_body</prefix:action_name>If the element doesn’t have a body, can lose the end tag and use shorthand syntax of:<prefix:action_name attr1 = “value” attr2 = “value2” /> For example: <jsp:include page="scripts/login.jsp" />

  6. JSP Action elements Two types 1. 2. JSP Pre-defined tags External tag library (e.g. JSTL) Tags prefix is <jsp: ….> Tags prefix is whatever developer chooses or library recommends Customand JSTL (Also called Standard Action Elements)

  7. JSP Action elements: JSP Predefined Tags • Also called JSP Standard Action Elements • List of these elements are:<jsp:forward> • <jsp:include> • <jsp:param> • <jsp:plugin> • <jsp:useBean> • <jsp:getProperty> • <jsp:setProperty> • See http://java.sun.com/products/jsp/syntax/1.1/syntaxref11.html for detailed attributes and values Used for Java Beans

  8. JSP Predefined Tag Example <jsp:include> • Standard Action Example: <JSP: include> tag • Example: • <HTML> <BODY> • Going to include hello.jsp...<BR> • <jsp:include page="hello.jsp"/> • </BODY> • </HTML> • Executes the included JSP page and adds its output into the this page

  9. JSP Predefined Tag Example <jsp:include> • What’s Difference from Using the ‘include’ directive?e.g. %@ include file = ‘hello.jsp’ %> • The include directive includes the contents of another file at compilation time. Good for including common static code e.g. header file, footer file. Good on performance  included only once. • But, what if including dynamic common code (e.g. a navigation bar where links are read from the dB?).. need to re-run the file each time a request is made - JSP: include • JSP: include incorporates the output of the included JSP file at run time

  10. JSP Predefined Tag Example <jsp:forward> • Standard Action Example: <JSP: forward> tag • Stops processing of one page and starts processing the page specified by the page attribute • Example: • <HTML> <BODY> • Error occurred…please wait<BR> • <jsp:forward page=“errorpage.jsp"/> • </BODY> • </HTML>

  11. JSP Predefined Tag Example <jsp:param> • Standard Action Example: <JSP: param> tag • Can be used to pass parameters when using <jsp:include> or <JSP:forward> • Example • <jsp:include page="login.jsp"> • <jsp:param name="username" value="jsmith" /> • </jsp:include> Executes a login page jsp:param passes in username to the login page

  12. JSP Action Elements JSP pre-defined tags (standard actions elements) Done External Tag library: ------- Custom ------- Java Standard tag library

  13. JSP Action elements: External tag libraries – custom • JSP 1.1. introduced ‘tag libraries’, allowing addition of tags beyond standard action <jsp: > tags • Developers can develop their own Custom action elementsusing custom tag libraries • Custom tag libraries are useful where developers are working in teams – reusabletags can be supplied to team via custom tag libraries. • The library is identifed in the JSP page using the <taglib>directive

  14. JSP Action Elements JSP pre-defined tags (standard actions elements) Done External Tag library: ------- Custom Done ------- Java Standard tag library

  15. JSP Action elements: External tag libraries – JSTL • JSP 1.2 introduced supported for a special tag librarycalled the JSP Standard Tag Library (JSTL) • Version 1.0 released in June 2002Version 1.1 released in June 2004 • The JSTL saves programmers from having to develop custom tag libraries for a range of common tasks, suchas database access, conditional loops etc • Enabled developers to produce more maintainable, simpler JSP code • Important development for JSP technology

  16. JSTL • The JSP Standard Tag Library groups actions into four libraries as follows**: ** more may be added in later releases…

  17. JSTL • To use any of these libraries in a JSP, need to declare using the taglib directive in the JSP page, specifying the URI and the Prefix Example of declaring use of core library: <%@ taglib prefix = “c” uri = “http://java.sun.com/jsp/jstl/core %>

  18. JSTL: Example Example: JSP page using JSTL that outputs 1 to 10 on a webpageusing the <c:forEach> and <c:out> tags of the core library <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> <head> <title>Count to 10 Example (using JSTL)</title> </head> <body> <c:forEach var="i" begin="1" end="10" step="1"> <c:out value="${i}" /> <br /> </c:forEach> </body> </html> A taglib directive declare use of core library JSTL tag examples

  19. JSTL: Example <c:foreach> Looking more closely at <c:forEach tag> ….. <c:forEach var="i" begin="1" end="10" step="1"> <c:out value="${i}" /> <br /> </c:forEach> </body> </html> The <forEach> tag enables loop logic. In this case, will lookthrough 10 times. Equivalent to java “for” loop Closing tag <c:forEach> placed after the end of body of loop

  20. JSTL: Example <c:foreach> All JSTL tags have a set of attributes (similar to HTML tags..) e.g. <c:foreach> tag has 6 attributes: var, items, varStatus, begin, end, step The full details for each attribute is in the JSTL specification document (on distrib). See p 65 for <c:foreach> tag definition Willl need to use this document to verify WHICH tag should be used and HOW is should be used

  21. JSTL: Example <c:out> <c:out> .. outputs a value to webpage. Usually uses just one attribute value Examples: <c:out value="${i}" /> <c:out value=“The result of 1 + 2 is ${1+2}” /> <c:out value=“param.userName" />

  22. JSTL: Example <c:if> <c:if> .. evaluates a condition. Uses an attribute test to hold the condition Example : <%-- Simple if conditions --%> <c:if test='${param.p == "someValue"}'> Generate this template text if p equals someValue </c:if> Example 2 <c:if test='${param.p}'> Generate this template text if p equals "true“ </c:if>

  23. JSTL: Multiple ‘if’ conditions An if/else action requires the use of the <c:choose> tag Syntax : <c:choose> body content (<when> and <otherwise> subtags) </c:choose> See page 52 of JSTL specification doc.

  24. JSTL: Multiple ‘if’ conditions Uses <c:choose>, <c:when> and <c:otherwise> Example: <c:choose> <c:when test='${param.p == "0"}'> <c:out value = “zero recorded”/> </c:when> <c:when test='${param.p == "1"}'> Generate this <c:out value = “single value”/> </c:when> <c:otherwise> <c:out value = “Set to ${param.p}”/> </c:otherwise> </c:choose>

  25. JSTL: Other core <c:..> actions Other examples: (NOT a complete list!) <c:set> ….sets the value of a variable <c:remove> ….removes a scoped variable <c:catch> ….catches an exception <c:url> ….. encodes a URL <c:import>… imports the content of a resource <c:redirect>.. redirects to another URL <c:param>.. adds a request parameter to other actions

  26. JSTL: Expression language • Up to now, could only use Java expressions to assign dynamic values  syntax errors common • JSTL now provides an expression language (EL) to support the tags  simpler syntax, less errors • The EL is now part of the JSP specification (as of versions JSP2.0) – can be used in JSTL tags or directly in JSP pages.

  27. JSTL: Expression language • All EL expressions are evaluated at runtime • The EL usually handles data type conversion and null values -- easy to use • An EL expression always starts with a ${and ends with a }

  28. JSTL: Expression language • The expression can include • - literals ( “1”, “100” etc) • - variables • - implicit variables (more later) • Examples: • <c:out value = “${1+2+3}” /> • <c:if test = “${param.Address == “D6”}” /> expression

  29. JSTL: Expression language - operators == != < + - * / or div > <= >= • Logical operators consist of && (or and), || (or or), and ! (or not). • The empty operator is a prefix operator that can used to determine if a value is null or empty. For example: • <c:if test=”${empty param.name}”> • Please specify your name. • </c:if>

  30. JSTL implicit variables • The JSTL EL allows these objects to be accessed as ‘Implicit Variables’ • Implicit variable are just pre-agreed fixed variable names that can be used in JSTL Expressions • -- Think of as“variables that are automatically available to your JSP page”..

  31. JSTL: Expression language – Implicit Objects • Full set of implicit variables on page 211 of JSTL documentation (includes: header, pageScope..) or on page 74 of O’Reilly’s Java Server Pages book • Very common implicit object is param • param refers to parameter passed in a request message (e.g. information entered into a form by a user). • e.g. <c:out value = “${param.userName}”/> • Further Examples of using param in next topic

  32. Comparing JSTL vs Scriptlets JSTL removes complexity by using tags instead of java code(abstraction) JSP pages using JSTL usually easier to maintain JSTL allows HTML ‘tag’ developers to ‘program’ JSTL often more difficult to debug Note: Using JSTL does not eliminate scriplets entirely.. may still need them sometimes for more complex logic http://www.informit.com/isapi/product_id~{27F6F199-F5FD-474C-AC7F-B3FC2C1F57B6}/st~{94C03A97-1188-4875-8A06-17743BA224B7}/session_id~{6A766315-391B-4F55-9AFB-B5425F6196C5}/content/articlex.asp

  33. Info on JSTL 1. Java Server Pages by Hans Bergsten – full reference on all JSTL tags in the Appendix. 2. Sun tutorial. Contains examples. Chapter 17 is on JSTL at: http://java.sun.com/webservices/docs/1.1/tutorial/doc/index.html 3. Sun’s JSTL 1.0 tag specification on distrib. Good for definition of each tag. Poor on examples.

  34. Setting up JSTL in a web application • JSTL is provided as part of Tomcat 5 • Ensure the two JSTL .jar files are copied into the WEB-INF\lib directory of your web application • Ensure the taglib directive(s) is in your JSP page

More Related