190 likes | 289 Views
Intermediate JSP. Matt Wheeler. Notes. This is a training NOT a presentation Please ask questions Prerequisites Introduction to Java Stack Basic Java and XML skills Introduction to JSP Installed LDSTech IDE (or other equivalent). Overview. Review Scriptlets Expressions
E N D
Intermediate JSP Matt Wheeler
Notes • This is a training NOT a presentation • Please ask questions • Prerequisites • Introduction to Java Stack • Basic Java and XML skills • Introduction to JSP • Installed LDSTech IDE (or other equivalent)
Overview • Review • Scriptlets • Expressions • Expression Language (EL) • Taglibs • Custom taglibs • Functions • Templating
Review • Scriptlets • Expressions • …
Scriptlets • Scriptlets are code in a JSP page (delimited with <% %> • Will be compiled into the doService method of the resulting servlet • Lets look at a simple example <% String user= request.getAttribute(“loggedInUser”); if (user != null) { %> Welcome <% } %>
Expressions • Like scriptletsevaluate a singular Java expression and return the result • Result must be a String or convertible to a String • The syntax is as follows: <%= expression %> • For example: <%= someBean.something%> <%-- Or we could enhance our previous example --%> <% String user= request.getAttribute(“loggedInUser”); if (user != null) { %> Welcome <%= user %> <% } %>
Disadvantages • Maintenance, maintenance, maintenance • Difficult to read • Difficult to understand • Not testable • Not reusable • Difficult to refactor • Tightly coupled UI and back end code • The long version can be found here: • http://www.javaranch.com/journal/200603/Journal200603.jsp#a5
Expression Language (EL) • The expression language is meant to provide easy access within a JSP page to application data in JavaBeans • EL is really the bridge between the model and the view and allows for separation of concerns • For detailed information on the JSP EL please see: http://download.oracle.com/javaee/6/tutorial/doc/gjddd.html
EL (continued) • In tandum with taglibs which we will discuss later, EL helps write scriptless JSP pages • EL allows access to properties and attributes of: • JavaBeans • Collections • Implicit objects //accessing a JavaBean ${someBean} ${someBean.someProperty} //accessing a value of a map with key of someKey ${someBean.map[‘someKey’]} //accessing an implicit object (request) ${request.param}
EL (JavaBeans) • EL looks for a specified bean in all scopes (request, session, application) to resolve the EL • ${someBean.whatever} • After a bean/resource named someBean is found EL attempts to access the whatever property of that bean
EL (Collections) • EL provides special syntax for accessing items in lists or maps • List properties can be accessed with array notation • Map items can be accessed with map or dot notation ${someBean.list[1]} //access the item at index 1 in the list ${someBean.map[‘key’]} //access the item in the map with key of ‘key’ ${someBean.map.key} //equivalently use dot notation for the same result
EL (Implicit Objects) • Objects exposed for reference in EL without any extra work or configuration from the developer • Some of these objects include: • pageContext (provides access to request, session, application), pageScope, requestScope, sessionScope, applicationScope, param, paramValues, header, headerValues, cookie, cookies, initParam, exception ${requestScope[‘nameOfSubmitted]} //extracts value for attribute of given name ${param[‘nameOfRequestParam’]} //gets value off the url for the given name ${header[‘Accept-Language’]} //find value for header with name Accept-Language ${initParam[‘paramName’]} //gets the value of the initParam with name paramName ${pageContext.request.servletPath} //gets the servlet path from the request
EL (operators) • But not all logic must be forced into JavaBeans • EL provides some basic operators • Logical: &&, ||, !, and, or not • Comparison: ==, !=, <, >, <=, >=, eq, ne, lt, gt, ge, le • Conditional (turnary): test ? result1 : result2 • Arithmetic: +, -, *, /, div, %, mod • Empty: empty, null • For operator precedence, please see: http://download.oracle.com/javaee/6/tutorial/doc/bnaik.html
EL (operators) • Some examples ${someBean.administrator && someBean.owner} ${someBean.count > 0} ${someBean.count + 1 % 2} ${someBean.count * .1 gt 50 && (someBean.payTaxes || someBean.goToJail)} ${4.0 eq (3 + 1)/1} ${someBean.map[‘someKey’]}
EL (Evaluation) • There are multiple implicit resolvers that attempt to handle EL expressions • In general, say we are resolving ${someBean.abc} • One of the EL resolvers will, grab the first portion someBean • Will look for an implicit object of that name • Will then look for a bean of that name • Once found, it will look for a property on that name or implicit object (abc) and get that value
Taglibs • Primary goal of taglibs it to provide reusable functionality • Through reusable tag elements • Through functions that extend el • Simplifies the page making it more readable / maintainable by separating logic from the page’s presentation
Credit where credit is due • http://springsource.org • Spring Recipies 2nd Edition (Gary Mak, Josh Long and Daniel Rubio)