When utilizing the iBATIS Framework as the persistence layer of your applications, you may get stumped as to why your LIKE statements are not being handled correctly and throws errors. When at first glance the errors have to do with other parameters in the query. The solution to this problem is to program the % character into the parameters passed into the sql statement, and to not have them hard coded into the sqlMap.

Example:

In this example we want to have a query that selects users with a last name like an inputted value. The query for this would be of the following format:

 SELECT * FROM USERS WHERE lastName LIKE "DEN%"

At first glance we may want to try to create our sqlMap statement as:

<select id="selectByName"parameterClass="String" >
    SELECT * FROM USERS WHERE lastName LIKE #value#%
</select>

This however will end up throwing errors. The correct way of creating this statement would be to leave out the % character and simply have the following statement:

<select id="selectByName"parameterClass="String" >
    SELECT * FROM USERS WHERE lastName LIKE #value#
</select>

Doing so will enable the query to function correctly where the % character gets passed in as part of the parameter. This also enables your query to be more dynamic by having the % character in any placement of the variable string.

Resources

, ,

Java provides multiple Text Formatting classes to enable easy conversion of integers, decimals, floats, and etc into Strings. The DecimalFormat class enables the formatting of Floats and Decimals and can be used to easily display percentages and currency amounts.

Example:

Converting a Float into Dollars formatted String

import java.text.DecimalFormat;
...
float amount = 100453.23f;
DecimalFormat dollarFormat = new DecimalFormat( "$#,##0.0#" );
System.out.println( dollarFormat.format( amount ) );

Resources:

, , ,

As one that seldom finds himself having to run Java Applications from a Unix Bash Script, I thought it would be helpful to share the appropriate commands i use to execute the code and to notify the user of success or failure. For reference the folder structure I use is:

app_name
    - lib
    - config

where the lib folder contains all necessary jar files to run the application, and the config folder contains any properties or configuration files passed as run time arguments

Writing the script

  1. Specify the script is a bash script
    #!/bin/bash
  2. Define SUCCESS and FAILURE variables
    FAILURE=1
    SUCCESS=0
  3. Define the appdirectory
    APP_HOME=/tmp/app
  4. Define the lib folder variable
    LIB=${APP_HOME}/lib/
  5. Define the classpath variable. This is a : seperated list of jars needed by the application
    CP=${LIB}abc.jar:${LIB}xyz.jar
  6. Define java home
    APP_JAVA_HOME=/usr/jdk/instances/jdk1.5.0
  7. Execute the Application with any arguments and java settings
    ${APP_JAVA_HOME}/bin/java -ms256m -cp "${CP}"
      com.mdibtz.TestApp "${APP_HOME}/config/config.properties"
  8. Test for Failure
    if [ $? -ne 0 ]
    then
        exit ${FAILURE}
    fi
  9. Success Logic if needed
    exit ${SUCCESS}

Complete Script

#!/bin/bash
 
FAILURE=1
SUCCESS=0
 
APP_HOME=`pwd`
LIB=${APP_HOME}/lib/
 
CP=${LIB}activation-1.1.jar:${LIB}mail-1.4.jar
 
APP_JAVA_HOME=/usr/jdk/instances/jdk1.5.0
 
${APP_JAVA_HOME}/bin/java -ms256m -mx768m -cp "${CP}" 
  com.mdbitz.TestApp "${APP_HOME}/config/config.properties"
 
if [ $? -ne 0 ]
then
    echo "AppFailed during run"
    exit ${FAILURE}
fi
 
echo "App successfully run"
exit ${SUCCESS}
, , ,

Apache Tiles is a Java Framework used for the templating of Web Applications. Tiles allows developers to define fragments of pages that are combined to create the whole page based on page definitions. The Tiles framework is often used in conjunction with the Apache Struts Model View Controller (MVC) framework. Below are two solutions on how to nest tile elements.

The Problem

During processing Apache Tiles creates a Tiles Context for each level of tile where the attributes defined are only accessible to that context and not any nested tiles. What this means is that you need to pass attributes defined at the top level down to  the nested or inner tiles.  To illustrate lets assume our web application has 4 main elements:

Header
Menu
Body
Footer

Now lets assume that in for some pages our Body is created by Nesting 2 tiles in it so that instead the page consists of:

Header
Menu
Body Part 1 Body Part 2
Footer

What this would mean is that we would have a layout file that is similiar to:

<tiles:insert attribute="header" />
<tiles:insert attribute="menu" />
<tiles:insert attribute="body"/>
<tiles:insert attribute="footer" />

And that our body page for the 2 elements could be represented as:

<table>
  <tr>
    <td><tiles:insert attribute="part1"/></td>
    <td><tiles:insert attribute="part2"/></td>
  </tr>
</table>

Now how do we get those attributes passed to that nested tile.

Solution #1 Defining Nested Tiles in the Tiles Definition

In this solution we define the inner body tile in the definition page and use that definition as the body for the page. So that we end up with a default layout, a page layout, and the body part definitions in our tiles-defs.xml file. A sample is shown below.

<definition name="gLayout" template="/WEB-INF/tiles/gLayout.jsp">
  <put name="header" value="/WEB-INF/tiles/header.jsp" />
  <put name="menu" value="/WEB-INF/tiles/menu.jsp" />
  <put name="footer" value="/WEB-INF/tiles/footer.jsp" />
</definition>
 
<!-- define the inner tile -->
<definition name="page.body" template="/WEB-INF/tiles/body.jsp">
  <put name="part1" value="/WEB-INF/page/part1.jsp" />
  <put name="part2" value="/WEB-INF/page/part2.jsp" />
</definition>
 
<!-- define the page by referencing the nested tile -->
<definition name="editpage" extends="gLayout">
  <put name="body" value="page.body"  type="definition"/>
</definition>

Using this method allows you to easily nest tiles and their attributes without having to worry about tiles context and passing attributes to inner nested tiles. The main drawback to this method is that your tiles definitions file can become extremely large depending on your web application.

Solution # 2 Have the Layout file pass attributes to internal tiles

In this approach we update the layout file to pass attributes from its tiles context into the nested tile.  So that the nested tile does not need to be defined in the tiles definition but instead the page definitions will specify the attributes that are utilized as shown below.

tiles-defs.xml

<definition name="gLayout" template="/WEB-INF/tiles/gLayout.jsp">
  <put name="header" value="/WEB-INF/tiles/header.jsp" />
  <put name="menu" value="/WEB-INF/tiles/menu.jsp" />
  <put name="footer" value="/WEB-INF/tiles/footer.jsp" />
</definition>
 
<!-- define the page by referencing the nested tile -->
<definition name="editpage" extends="gLayout">
  <put name="body" value="/WEB-INF/page/body.jsp" />
  <put name="part1" value="/WEB-INF/page/part1.jsp" />
  <put name="part2" value="/WEB-INF/page/part2.jsp" />
</definition>

gLayout.jsp

<tiles:insert attribute="header" />
<tiles:insert attribute="menu" />
<tiles:insert attribute="body">
  <tiles:put attribute="part1" beanName="part1" />
  <tiles:put attribute="part2" beanName="part2" />
</tiles:insert>
<tiles:insert attribute="footer" />

We now have successfully passed the attributes defined in the tiles definitions into our nested tile by use of the layout file. Although this approach will minimize the size of your tiles definitions file your layout file will need to be expanded to contain all possible attributes that could be utilized by nested tiles, increasing its size.

Resources

, , ,

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

, ,