There are often odd cases when in a Flash Application you find that by design you want to call a function but only have the name of the function as a String. This could be caused by various reasons one being that the name is stored in a database as part of some settings. To accomplish this conversion we make use of the Function object.
How to Convert a String to a Function
var functionName:String = "toString";
// obtain the function handler
var namedFunction:Function = this[fName];
// execute the function and pass any desired arguments
nameFunction( arg1, arg2, ... );
ActionScript 3, AS3, Function
JavaServer Pages Standard Tag Library or JSTL for short is a library that contains a set of core functionality including iterations and conditionals. The tag I find myself most often using is the forEach tag to output a collection of data in a consistent format. By use of the optional status object and the if tag you can even put logic in that executes only if the item being iterated is first or last in the collection.
Getting Started
To utilize JSTL you first need to download the appropriate jar file and import it into you project in the web library. Once in your project you need to import the tag definition into your page using the following code.
<%@ taglib prefix="c" uri="http://java/sun.com/jsp/jstl/core %>
Basic ForEach Tag Usage
Once the tag library is imported we can use it by specifying 2 attributes var and items. VAR is used to denote the reference that the current item being iterated in the collection can be accessed by, while ITEMS specifies the collection to be iterated upon. Lets assume me have a collection of User objects that each have a firstName, lastName, and email property. We could iterate over them with the following code.
<c:forEach var="user" items="${users}">
</c:forEach>
The above code is functional but doesn’t output any data lets expand upon it by having it export rows of a table with the three properties each in their own cell.
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
<c:forEach var="user" items="${users}">
<tr>
<td>${user.firstName}</td>
<td>${user.lastName}</td>
<td>${user.email}</td></tr>
</c:forEach>
</table>
Advanced Usage: The varStatus Attribute
In addition to iteration over the collection the ForEach tag provides a status object that can be used to obtain information about where you are in the collection.
<c:forEach var "user" items="${users}" varStatus="status">
</c:forEach>
- first and last property
The status object contain a first and last property that returns a boolean set to true or false if the current item being iterated over is the first or last object in the collection. You can utilize the if tag to have code execute if it is one of these conditions.
<c:if test="${status.last}">
// code
</c:if>
- index property
The index property gives you the current index of the object within the collection this value can be used to output different classes on odd and even rows for example, in addition to other uses.
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
<c:forEach var="user" items="${users}" varStatus="status">
<tr class="${status.index % 2 == 0 ? 'even' : 'odd'}">
<td>${user.firstName}</td>
<td>${user.lastName}</td>
<td>${user.email}</td>
</tr>
</c:forEach>
</table>
Resources
forEach, if, JSTL
SQL Queries can often be written in multiple ways to obtain the same end result set. Tables for instance can be joined in the FROM clause or the WHERE clause of a SQL Statement. In the FROM Clause we can utilize LEFT, RIGHT, INNER keywords to denote if both sides of the statement needs to be present to return the results or if only 1 side needs to be. These same joins can be accomplished in the WHERE clause by use of the *= and =*. Where *= is equivalent to LEFT JOIN and =* is the same as RIGHT JOIN.
Example FROM clause LEFT JOIN
FROM USER u
LEFT JOIN ROLE r ON u.ROLE_ID = r.ID
Example WHERE clause LEFT JOIN
FROM USER u,
ROLE r
WHERE u.ROLE_ID *= r.ID
*=, =*, equals-star, star-equals
Much to every developers pain we often find that JavaScript that is functional in one browser is not functional in other browsers. Recently the update to Safari 3 requrired that the window open command to not contain the url. As it would fail to open a window if it did, the simple fix was to create a new window with no url and set the url after creating the window. However as other brothers do not require this we can set up a two step process in that we try to open the window normally and if it fails then we open one with no url defined.
function create_popup( url, win_title, win_options )
{
var popWin = window.open( url, win_title, win_options );
if( !popWin ) {
popWin = window.open('', win_title, win_options);
popWin.location.href = url;
}
}
new popup, safari, window.open
The Java Calendar class provides many helpful methods for modifying Date objects. Here you will find a short code snippet for obtaining the last day of the month. To utilize the code please don’t forget to import the java.util.Calendar class or which ever Calendar you utilize. How the snippet works is it utilizes the getActualMaximum function which returns the max value for a field based on the current settings of the Calendar.
Calendar cal = Calendar.getInstance();
cal.set( Calendar.MONTH, 11 );
cal.set( Calendar.DATE, cal.getActualMaximum(Calendar.DAY_OF_MONTH) );
Calendar, DAY_OF_MONTH, getActualMaximum