October 22, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Extend Your Java Studio Creator Options with JSTL

  • May 4, 2005
  • By Dick Wall
  • Send Email »
  • More Articles »

This article covers the use of JSTL within Creator: How to set it up and make use of the JSTL, and how to deploy a JSTL-enabled Web app to Sun App Server PE 8 or another App Server.

What is the JSTL?

The JSP Standard Template Library (JSTL for short) is a tag library for creating dynamic JSP Web pages. It defines a number of useful tags that allow conditionals, iterations, and other common operations on data. If you are familiar with struts, you will notice a lot of similarities within the JSTL to the struts bean and logic tags in particular.

Why use the JSTL in Creator?

Clearly within Java Studio Creator, the emphasis is on component controls and data binding. This allows you to put data-bound pages together very quickly. It is very successful; however, data binding does limit you in some ways. For example, when binding a data grid, you need to know the number of columns ahead of time and provide header information for them. Now, suppose that you have data that you want to display in a grid for a time period that you do not know ahead of time, or maybe you simply have a very large number of columns to display and don't want to create large numbers of pre-defined column details for a grid.

JSTL allows you to build up an HTML table completely dynamically. By nesting a couple of iterations inside one another, you can make an HTML table to display data of variable dimensions in both X and Y axes. This will form the example that you examine in this article. There are numerous other reasons I can think of to use JSTL in Creator (conditionals on what to display, and so forth), but there are also a lot of reasons that I cannot think of but that you will probably hit at some time or other. Either way, knowing you have a backup to the strict data-binding model only increases your options when creating dynamic Web apps in Creator.

About this Article

Unlike previous articles, I have chosen to make this one a descriptive one rather than a follow-along article. Full sources are available in the accompanying zip file: JSTLDemo.zip.

Getting Started

Using the Source View

JSTL has no GUI support in Creator. This means it is necessary to switch to the source view of the JSP page (use the tab at the bottom of the JSP window). It is possible to mix and match JSF and JSTL, and for the JSF parts of the page, you can use the GUI view and drag and drop. I recommend using a group panel to hold the JSTL code you are going to use because it makes an easy handle to move and lay out the JSTL part on the page in the GUI view.

Quick JSTL Primer

The JSTL is a JSP tag library; that is, after importing, it defines a set of new tags that can be used in a JSP page. The tags are typically imported into the JSP using the namespace c: so the tags look like this: <c:if>, <c:forEach>, <c:choose>, <c:out>, and so on.

This article is not intended to be a tutorial on JSTL. For that, I recommend the following links:

You may also notice that there are more namespaces included with JSTL than the c: namespace. For example, there is an x: namespace for XML operations, and a sql: namespace for SQL queries and manipulations of DBs. Although this article does not cover these other JSTL namespaces, the techniques used here can be employed to harness those in Creator in the same way.

About the Sample Project

The sample project accompanying this article is a simplification of a solution to a real-world problem I faced in the my latest project at NewEnergy Associates.

The requirements for the display of hourly schedule data went beyond what could be accomplished easily (or even at all) in the Creator Grid component. These included (in no particular order) 96 quarter-hourly interval values (I could have created a bound grid with over 100 columns in it, but I really didn't have the patience). Also, the data was not organized into a readily bindable grid, but rather into a fairly sophisticated set of nested beans. Finally, user requirements stated that a mismatched row should be highlighted in some way (colored to draw the user's attention).

From my knowledge of Struts in previous projects, it became apparent that an HTML table constructed using iterators over the nested data beans would be far easier than trying to force the model into a grid with over 100 columns.

For the accompanying example project used in this article, I reduced the number of interval values to 24 hourly intervals instead of 96 quarter-hourly ones. Also, some of the more arbitrary logic is ommitted for brevity. However, it shows the potential value of a JSTL solution in this case.

A JSTL Example

Setup Steps for the Example Project

Download the zip file referenced at the top of the article, unzip it, and then open it using Creator. Feel free to look around the project. The layout is simple. There is a package called jstldemo.bean with three classes in it.

The Schedule class is a simple Schedule bean that is used to hold some basic details about the Schedule (name, in party, out party, and the like) and also in and out time series with 24 hourly values in each time series.

TimeSeries is the object that holds the hourly values. It also holds a name for the time series and a status to show whether the time series is matched to its partner.

DataSetup is just a very simple class with static methods to set up some random demo data in the two data beans above.

This is only a demo data set. The structure of the beans made more sense when it was part of the larger system I was working on. Don't get too bogged down in the way this data is structured; it is sufficient to demonstrate the use of JSTL, which is the point of this article.

The clever stuff is all in Page1.jsp. Page1.java simply gets the demo data and puts it into a TreeMap in the page so that the JSTL in the jsp page can access it.

Once you have examined the project, run it to see the resulting grid. You will dig into how this was achieved below.

Using JSTL in a Creator Page

There are several steps necessary to use JSTL inside a Creator Page. First, the JSTL tags must be included in the page. You must do this from the source code view of the page, and also the JSTL tags can only be added and manipulated in the page source view.

It is also recommended to put a group panel in the page to hold the JSTL code. This makes it easier to lay out the page and move the JSTL part of the page around (this is a useful trick in Creator for handling any component or code that Creator cannot render in WYSIWYG mode).

Add Group Panel to hold the JSTL Grid

Creating a group panel to hold the JSTL code (a table in this case) means that it is easy to lay out the page around the otherwise invisible JSTL-generated part. It also lets you move around the container position on the page.

When the group is created, name it something sensible. Then, look in the code and you should see something like the following:

<h:panelGroup binding="#{Page1.jstlTablePanel}" id="jstlTablePanel"
              style="height: 444px; left: 24px; top: 36px;
              position: absolute; width: 720px"/>

To place JSTL code inside the panel, change it to look as follows:

<h:panelGroup binding="#{Page1.jstlTablePanel}" id="jstlTablePanel"
              style="height: 444px; left: 24px; top: 36px;
              position: absolute; width: 720px">

....Insert JSTL Code Here....
</h:panelGroup>




Page 1 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel