|
In this issue
Welcome to the Enterprise Java Technologies Tech Tips for January 27, 2007. Here you'll get tips on using enterprise Java technologies and APIs, such as those in Java Platform, Enterprise Edition (Java EE). This issue covers: These tips were developed using the Java EE 5 SDK. You can download the SDK from the Java EE Downloads page. You can download the sample archive for Fun With Expression Language in JSP 2.1 tip. Any use of this code and/or information below is subject to the license terms.
Expression language (EL) was first introduced in the JavaServer Pages Standard Tag Library (JSTL) 1.0. The goal was to give developers easy access in a JavaServer Pages (JSP) technology page to application data in beans, arrays, request parameters, and cookies. EL has proven to be very useful in simplifying JSP authoring. For example, a simple line like the following could be used in a JSP page to access data in a bean: You have ${cart.itemCount} items in your cart.
The EL expression, Because of its usefulness, EL was incorporated into JSP 2.0. JavaServer Faces technology 1.0 then adopted a version of EL. Since then, EL has been further extended to allow the setting of bean properties and to enable specifying methods for actions. A unified EL is new in JSP 2.1. It unifies the EL in JSP 2.0 and in JavaServer Faces technology 1.0. This tip covers a number of new features of the unified EL introduced in JSP 2.1. A sample web application package accompanies the tip. The code examples in the tip are taken from the source code of the sample (which is included in the package). ELContext
One of the new things regarding EL in JSP 2.1 is the
An
One of the interesting aspects of Using a Custom
The August 26, 2006 Tech Tip Extending the Java EE Unified
Expression Language with a Custom
Here is what the JSP page displays when you run the example:
Before examining the custom public class ELResolverListener implements ServletContextListener { public void contextInitialized(ServletContextEvent evt) { ServletContext context = evt.getServletContext(); JspApplicationContext jspContext = JspFactory.getDefaultFactory().getJspApplicationContext(context); jspContext.addELResolver(new ColorELResolver()); } }
Notice that to plug the custom
The container's default
During property resolution, the resolvers are queried based on
their order in the list. The
Here is part of the code in a custom public class ColorELResolver extends ELResolver { public Object getValue(ELContext context, Object base, Object property) throws ELException { Object result = null; if(base == null) { // Resolving first variable (e.g. ${Color}). String propertyName = (String)property; if(propertyName.equals("Color")) { result = new ColorImplicitObject(); context.setPropertyResolved(true); } } else if(base instanceof ColorImplicitObject) { // Resolving a property on ${Color} ColorImplicitObject color = (ColorImplicitObject)base; result = ColorImplicitObject.fromName(colorName); context.setPropertyResolved(true); } return result; } }
A custom
The method
In the second trip, the
The statement
The actual code for A Second Example
Now let's look at a second example of using a custom The objective is to allow EL expressions such as the following: ${myBean.meth["arg1"]["arg2"]}
Here, the EL expression invokes the method
Normally, in processing this expression the
Here is part of the public class MethodInvocationELResolver extends ELResolver { public Object getValue(ELContext context, Object base, Object property) throws ELException { ... ResolvedMethod meth; if (base instanceof ResolvedMethod) { meth = (ResolvedMethod) base; meth.add(property); context.setPropertyResolved(true); return meth.isReady()? meth.invoke(): meth; } meth = getMethod(base, properties); if (meth != null) { context.setPropertyResolved(true); result = meth.isReady()? meth.invoke(): meth; return result; } return null; } private ResolvedMethod getMethod( Object base, Object property) { String methodName = property.toString(); Class beanClass = base.getClass(); for (Method method: beanClass.getMethods()) { if (method.getName().equals(methodName)) { return new ResolvedMethod(method); } } return null; } private static class ResolvedMethod { Method method; int argumentCount; ArrayList<Object> args = new ArrayList<Object>(); public ResolvedMethod(Method method) { this.method = method; argumentCount = method.getParameterTypes().length; } ...
As usual, the
Evaluating an EL expression with two arguments, such as
To test the <jsp:useBean id="map" class="java.util.HashMap"/> Adding ("key1", "item1"): ${map.put["key1"]["item1"]} </br> Adding ("key2", "item2"): ${map.put["key2"]["item2"]} </br> map is ${map}<br/>
The page invokes the method Here is what the JSP page displays when you run the example:
For more information about the Unified Expression Language as well as other new features in JSP 2.1, see Web Tier to Go With Java EE 5 : Summary of New Features in JSP 2.1 Technology. Running the Example You can run the sample application in any application server that conforms to Java EE 5, such as the application server in the Java EE 5 SDK. You can download the Java EE 5 SDK from the Java EE Downloads page.
About the Author Kin-man Chung is a senior staff engineer at Sun Microsystems. He has worked on code generation and optimizations for the C++ SPARC compiler. More recently, Kin-man has worked on web containers, including Tomcat and GlassFish. He is currently a co-specification lead for JavaServer Pages 2.1 technology.
jMaki is a lightweight framework for creating Web 2.0 applications using standards-based technologies such as CSS, HTML, and JavaScript. The jMaki framework provides:
The jMaki framework abstracts much of the JavaScript and CSS by providing defaults for widgets. However it makes the JavaScript and CSS for the widgets easily accessible so a designer or page developer can easily customize the code. jMaki focuses on the aspects of delivering JavaScript to the client allowing the JavaScript to communicate with server-side technologies in a way that's neutral to the technology. The server-side technologies include PHP, JavaServer Pages (JSP) technology, JavaServer Faces technology, and Phobos. The term jMaki signifies the concept of a JavaScript wrapper -- the "j" in jMaki is the first letter in JavaScript, and "maki" is Japanese for wrapper. The framework makes available widgets from popular toolkits such as Dojo, Yahoo, Scriptaculous, and Google in way that's familiar to Java technology, PHP, or Phobos developers. It does this by writing wrappers around the widgets in those toolkits. If you're familiar with using JSP technology, jMaki allows you to include a widget from any of the supported toolkits as a JSP taglib. For example, to start using jMaki, you add the following tag declaration to the top of a JSP page: <%@ taglib prefix="a" uri="http://java.sun.com/jmaki" %>If you use JavaServer Faces technology, you can include a widget as a JavaServer Faces component. Here's the tag declaration that you add to the top of a JSP page to use widgets as JavaServer Faces components: <%@ taglib prefix="a" uri="http://java.sun.com/jmaki-jsf" %>If you're a PHP developer, you can include a widget as a PHP tag. The PHP declaration looks like this: <?php require_once 'Jmaki.php'; ?>Or if you're a Phobos developer you can include widgets as enterprise components. jMaki automatically generates the appropriate JavaScript code to render the widgets on the page and wire components together. In this tip you'll learn some of the basics of the jMaki framework and also see some examples of jMaki in use. What's In the jMaki Framework? The jMaki framework includes client components and server components. Let's look at some of these components. Client Components One of the most useful client components for developing web applications is layouts. These are templates based on HTML and CSS that you can use to create specific format styles for your application. The jMaki layout gallery shows the jMaki layout templates that are available. All of the templates are plain CSS so you can easily customize them. The layout gallery itself was developed using a customized version of one of the templates. Another client component is the runtime, which is responsible for bootstrapping widgets and passing arguments from the server runtime. The client runtime component renders the widgets on a page based on the information in tags. You call the client runtime for a widget by adding the ajax custom tag to a page. For example, one of the widgets in jMaki wraps the table widget that's provided in the Dojo toolkit. You can include the Dojo table widget in a JSP page by adding the following tag (the example is shown on multiple lines to fit the width of this page): <a:ajax name="dojo.table" value="{columns: { 'title' : 'Title', 'author':'Author', 'isbn': 'ISBN #', 'description':'Description'}, rows:[['JavaScript 101', 'Lu Sckrepter','4412', 'Some long description'], ['Ajax with Java', 'Jean Bean','4413', 'Some long description']]}" />
The If you use PHP, you can include the Dojo table in your page by adding the following PHP call (the example is shown on multiple lines to fit the width of this page): <?php addWidget('dojo.table', null, null, "{ columns: { title : 'Title', author:'Author', isbn: 'ISBN #', description:'Description'}, rows:[ ['JavaScript 101', 'Lu Sckrepter','4412', 'Some long description'], ['Ajax with Java', 'Jean Bean','4413', 'Some long description'] ]}"); ?> One more important client component is the widget model, which provides the component model for jMaki widgets. The model specifies three files that comprise a jMaki widget:
The
If you want to wrap a widget that isn't included with jMaki,
you need to create a
Here's an example of what goes in a jmaki.namespace("jmaki.widgets.foo.bar"); jmaki.widgets.foo.bar.Widget = function(wargs) { // widget code here }
The code example shows a constructor for a widget named foo.bar
followed by the widget code. All jMaki widgets have
a constructor under the jMaki Glue
jMaki provides convenient APIs for performing jMaki Glue is built on top of the publish/subscribe event mechanism. The glue is a powerful feature that allows you to define application behavior and tie widgets together using JavaScript actions when a specific event is published to a topic. An application developer provides the code that is used by the glue feature. The current release of the jMaki framework includes the glue code needed to tie the Yahoo Geocoder to either the Yahoo or Google map widgets. Server Components
There are two server components in the jMaki framework: the
server runtime and a proxy named
The jMaki Examples Now that you've learned about some of the components in jMaki, let's look at some examples that use a jMaki widget in a web application. Using the Editor Widget In the first example, you'll include as a JSP tag handler the Dojo editor widget provided by jMaki. This widget presents a text editor that can be used to create and manipulate documents. Recall that to use a jMaki widget as a JSP tag handler, you first add an appropriate tag declaration to the top of a JSP page. <%@ taglib prefix="a" uri="http://java.sun.com/jmaki" %>
Then you add the <a:ajax id="myeditor" name="dojo.editor" value="Edit me."/>Your JSP page should also include JavaScript code to show what a user enters into the editor. <script type="text/javascript"> function showEditorContents() { var data = jmaki.attributes.get('myeditor').getEditorContent(); alert("contents=" + data); } </script> <a href="javascript:showEditorContents()">Show Editor</a>This JavaScript code brings up a window with the contents in the editor that the user added. All that's left to provide is some surrounding HTML code. Here's the complete code for the application: <%@ taglib prefix="a" uri="http://java.sun.com/jmaki" %> <h2>Dojo Editor Test</h2> <hr> <div style="height:300px"> <a:ajax id="myeditor" name="dojo.editor" value="Edit me." /> </div> <script type="text/javascript"> function showEditorContents() { var data = jmaki.attributes.get('myeditor').getValue(); alert("contents=" + data); } </script> <a href="javascript:showEditorContents()"> Show Editor Contents</a>You can also view this code by clicking the "Source" link for the Editor widget in the jMaki Widget Gallery. If you click on the "See Live" link you can run the application. Here's what you should initially see when you click on "See Live":
Enter some text in the editor widget and see what happens.
Using jMaki Glue The second example uses the jMaki glue code to tie together the Yahoo Geocoder and Google map widgets on a JSP page. <%@ taglib prefix="a" uri="http://java.sun.com/jmaki" %> <h2>Maps Page</h2> <hr> <div style="height:300px"> <a:ajax name="yahoo.geocoder" service="/xhp" /> <a:ajax name="google.map" args="{ centerLat:37.39316, centerLong:-121.947333700, }"/> </div>If you enter a location in the Geocoder widget, it returns longitude and latitude values that are picked up by the Google map widget and plotted.
Summary You can use jMaki to add Web 2.0 functionality to your existing applications or to create new applications. Take advantage of the features you need. Currently a Beta version of jMaki is available for download. It contains all the features discussed in this tip. Also, the jMaki page has documentation, blogs, and a screencast to help you get started. For more information on jMaki see the article New Technologies for Ajax and Web Application Development: Project jMaki, and the blog entry Building web 2.0 apps with jMaki. About the Author Carla Mott is a contributor on Project jMaki. She is also an editor on The Aquarium webblog, a Java Enterprise Community leader on java.net and a committer on Dojo, an open-source JavaScript toolkit. Previously, she was a project owner for Project GlassFish where she helped to open source Sun's application server and to build the GlassFish community. Correction to Last Month's Tip
In the December 16, 2006 Tech Tip Pitfalls in Constructing Java Persistence Query Language Queries the section "Using a LIKE Expression With an Escape Character" included the following sentence:
In this case, the expression is true if the value of
The sentence should be:
In this case, the expression is true if the value of
2007 JavaOne Conference Registration is Live
Register today for the JavaOne conference and save $200. The program is new and expanded. Check the 2007 JavaOne Conference page for details. Java Application Platform SDK Update 2 Now Available
Java Application Platform SDK Update 2 supports the newly released Java SE 6 and introduces a new runtime component, Portlet Container 1.0 Beta, to develop and test Java portlets. Developer Assistance
Need programming advice on Java EE? Try Developer Expert Assistance. | ||||||||||||||||
|
| ||||||||||||