advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Tutorials: Add Logic to Your JSP Pages with the JSP Expression Language:

Expression Objects

An expression is written according to the rules of grammar defined in the Expression Language specification. So when an Expression Language evaluates and runs expressions, it interprets the commands within them and converts them into the language spoken by the underlying environment. There are two types of expressions, value expressions and method expressions. Value expressions retrieve the value of a variable within a model object or set the value into a variable in a model object. Method expressions invoke a method within a model object. All expression objects extend from the core class Expression-hence, all expressions are serializable. Furthermore the Expression base class gets the reference to the ELContext object, which is used as the context for evaluating the expression.

Creating Expressions

Expressions are created through the ExpressionFactory class. This class provides two methods, one to create ValueExpressions and the other to create MethodExpressions. To create an expression through the ExpressionFactory, you need the following:
  • ELContext
  • String representing the expression
  • The type of expression

Resolving Model Objects

One of the most helpful things about the Expression Language how it can help you resolve complex data types like arrays, vectors, and JavaBeans. To facilitate this, the EL API provides a variety of resolver classes, all of which derive from the ELResolver base class:
  • ArrayELResolver: This class manipulates arrays It's used to handle objects that are of type Java arrays. You can construct ArrayELResolver in read-only mode when you're not required to update the array with the calling JSP.
  • ListELResolver: This class manipulates Lists.
  • BeanELResolver: This class provides access to JavaBeans. It's used to manipulate any JavaBean that conforms to the JavaBean Specification. You can instantiate BeanELResolver in read-only mode to avoid accidentally writing to the JavaBean.
  • CompositeELResolver: This class resolves complex objects using a list of child ELResolvers. It also uses ELContext to help keep track of the resolved properties, combining the results once all the individual elements in the complex expression have been resolved.

EL Functions

Often, JSP pages need to perform repetitive actions on data elements before they are rendered to the page (converting to upper case, parsing a string, etc). To avoid repeating these operations every time, the EL API provides a convenient way to define these repetitive actions as a function. All custom functions are mapped with a FunctionMapper and are available to the ELContext. Whenever you need a particular function, you simply invoke the function using the function name.

${fn:formatDate(Hotel.checkinDate)}
The ELContext translates the function to the actual function registered with the FunctionMapper, executes the function, and returns the result to the caller.

EL Variables

Like custom functions, JSP pages sometimes need access to variables that may not be defined in any of the model elements present in the scope of the JSP page. For instance, a counter within a page that counts the number of occurrences of an object or a temporary variable that holds the values extracted from a model element. All EL Variables are mapped in a VariableMapper object, which is accessible to the ELContext. EL Variables do not need be defined as regular Java data types—they can be EL expressions. When an EL variable (that is also an expression) is invoked in the JSP page, ELResolver resolves the expression and returns the value of that expression as the value of the variable.

The Future

As of this release, the EL API is defined as part of the JSP2.1 Specification. Even though references are made to the EL API in the JSP2.1 specification, details of the API are defined as a separate document. Having a separate document gives Sun Microsystems the flexibility to move the EL API into a separate JSR if required in the future. The JSP specification and Java Server Faces specifications will refer to the EL Specification without including them directly into their respective specifications. As the Expression Language specification gains traction in the coming months, more frameworks and technologies may begin to implement them. For now, bundling the EL specification as part of the JSP 2.1 specification gives the Expression Language good exposure and allows it to evolve as an independent specification in the future.

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.

 Internet.com eBook Library
 IBM Software Construction Toolbox
 Microsoft RIA Development Center
 Destination .NET
XML error: not well-formed (invalid token) at line 38
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Mozilla's Ubquity Mashup: For The Masses?
iPhone Users Just Want to Have Fun
Oops! I Fixed the Linux Kernel
Jim Zemlin: The New Center of Linux Gravity
Microsoft's Novell Investment Tops $340M
Fedora 10 Takes Shape
IBM Gives a Mobile Voice to Developers
Inadequate Tools Send Software Down the Drain
USB 3.0 One Step Closer to Reality
Would-Be Linux Contributors May Get a Leg Up

Develop a Mobile RSS Feed the Easy Way
State of the Semantic Web: Know Where to Look
A 3D Exploration of the HTML Canvas Element
Setting Up and Running Subversion and Tortoise SVN with Visual Studio and .NET
Java/JRuby Developers, Say Open 'Sesame' to the Semantic Web
Interpreting Images with MRDS Services
DevXtra Editors' Blog: Executives Avoiding Cloud Computing in Droves
Q&A with James Reinders on the Intel Parallel Studio Beta Program
The Pros and Cons of Outsourcing Enterprise Emails
Hosting Options: Shared or Dedicated Server

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers