fa244b3973a43d8b504029e054b42fb7.ppt
- Количество слайдов: 32
Custom Tags
Organization of the platform Your web pages Your application JSTL Java. Server Pages (JSP) Java Servlet API Java language
What’s irritating about Custom Tags? The tag-extension protocol is too complicated do. Start. Tag() or d f en? har , ev o To ling s Go do. Init. Body() do. Catch() do. End. Tag() release() Tag handler do. After. Body() do. Finally()
What’s irritating about JSP? Also, tags don’t support certain kinds of code reuse. ”> <% for (…) { %> <%= customer %>: <%= hat. Size %> <% } %> out. println(…); for(…) { out. println(…); … }
What’s bad about JSP? ( ) The general consensus says… Scriplets They complicate abstraction and code reuse. They make it harder for nonprogrammers to maintain pages
JSP 2. 0 Sun introduced the solution to these problems in JSP 2. 0. It addresses theses issues by introducing Expression language Tag files Simplified Tag API (Simple. Tag versus Tag) Improved XML syntax JSTL, although it is not part of JSP specificiation.
JSP Expression Language JSP became beautiful after this The motivation is to lose scriplets whatsoever. Also non java programmers should be able to use it easily, because it is just an expression language. It was inspired by xpath and java script except it is much easier than any of them
EL syntax Expressions appear between ${ and }. Note that ${ and } may contain whole expressions, not just variable names E. g. , ${my. Expression + 2} Access Java. Bean properties using. (dot) ${book. title} translates to book. get. Title() Access to Array and Collection elements using collection[] Access to a Map value by key using map[“key”]
EL syntax EL searches each scope area if a scope isn’t specified Specific scope areas can be used: ${page. Scope. title} ${request. Scope. title} ${session. Scope. title} ${application. Scope. title} ${duck. beak. Color} can resolve to ((Duck) page. Context. get. Attribute(”duck”)). get. Beak. Color() Automatic Casting, wow
EL Operators Arithmetic +, -, *, / (or div), % (or mod) – note on XPath Relational == (or eq), != (or ne), < (or lt) > (or gt), <= (or le), >= (or ge) Logical && (or and), || (or or), ! (or not) Validation empty null values Collections or Arrays that are empty Strings that evaluate to “”
EL Operators (Examples) Arithmetic ${age + 3} Comparisons ${age > 21} Equality checks ${age = 55} Logical operations ${young or beautiful} Emptiness detection ${empty a} ‘a’ is empty String (“”), empty List, null, etc. Useful for ${empty param. x}
Implicit objects in EL Param and paramvalues Access HTTP request parameters Example: ${param. password} Header and headervalues Request header information Example: ${header[“User-Agent”]} Initparam (for the context init parameters of the webapp) Example: ${page. Context. request. remote. User} Cookies Example: ${cookie. crumb}
EL: Uses JSTL 1. 0 introduced the EL, but it could be used only within tags. In JSP 2. 0, it can be used almost anywhere Hi, ${user}. You are
JSTL JSP Standard Tag Library Set of tags that should be available in all compliant JSP containers Promotes rapid application development at the web page layer
Why JSTL? A big reason is that writing your own custom actions, or tags, can be a pain There was lots of reinventing the wheel going on, for tasks like displaying a date in a special format Many open source libraries sprung up, but there was still no standard This fueled the fire behind JSTL
JSTL: Features Control flow Iteration, conditions URL management Retrieve data, add session IDs Text formatting and internationalization Dates and numbers Localized messages XML manipulation XPath, XSLT Database access Queries, updates (you do not need to write java any more )
JSTL: Libraries Library features Recommended prefix Core (control flow, URLs, variable access) Text formatting c XML manipulation x Database access sql fmt
Managing variables Outputting values with EL
Iteration A loop with a start, end, and step (typical for statement)
| Value | Square | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ${x} | ${x *" />
Example
|












