Archive for July, 2006

Iterate over HashMap with JSTL

Written by coregps on Friday, July 21st, 2006 in Java.

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

I’ve ever written a java bean base on Commons DBUtils, which returns an ArrayList of HashMap results from SQL query. The following is one of my Java Bean:

package database;
import java.util.ArrayList;
import java.util.List;
public class CategoryDBAO {
    ...

    public List getAllCategories() {
        ArrayList categoryList = new ArrayList();
        try {
            String sql = “select categories_id, categories_name from categories”;
            attrList = DBConn.searchToMapList(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return categoryList;
    }

    …
}

Many times I need to iterate over the set of the values stored in the map. This can be done like this:

<jsp:useBean id="categoryDB" class="database.CategoryDBAO" scope="page" />
<select name="categories_id" id="categories_id">
   <c:forEach var="category" begin="0" items="${categoryDB.allCategories}“>
        <c:set var=”categoryid” value=”${category[’categories_id’]}” />
        <c:set var=”categoryname” value=”${category[’categories_name’]}” />
        <option value=”${categoryid}”>${categoryname}</option>
   </c:forEach>
</select>

What should be noted here is the code fragment in red.

I meet some trouble when adding a new script function to a dynamically created input element. The code is something like this:

var inputElement = document.createElement("input");
inputElement.setAttribute("type", "text");
inputElement.setAttribute("id", "price");
inputElement.setAttribute("name", "price");
inputElement.setAttribute("value", "0");
inputElement.setAttribute(”onkeypress”, “return numeralsOnly(this.value, event, 2);”);
inputElement.style.width = “90%”;
inputElement.className = “text”;

Everything works except the onkeypress function. After changing the code to the following, it works fine.

var inputElement = document.createElement("input");
inputElement.setAttribute("type", "text");
inputElement.setAttribute("id", "price");
inputElement.setAttribute("name", "price");
inputElement.setAttribute("value", "0");
inputElement.style.width = "90%";
inputElement.className = "text";
var func = new Function(”return numeralsOnly(this.value, event, 2);”);
inputElement.onkeypress = func;

A free 10-week “AJAX Programming” online course

Written by coregps on Sunday, July 16th, 2006 in AJAX.

A free 10-week “AJAX Programming” online course is about to start from August 4th, 2006. This course is for anyone who wants to learn AJAX for the first time or increase their knowledge on AJAX. In this 10-week course, students learn basic concept of AJAX as well as how to use various AJAX frameworks and toolkits such as Dojo toolkit, jMaki, Google Web Toolkit, and AJAX-fied JavaServer Faces (JSF) components.

http://www.javapassion.com/ajaxcodecamp/

This course runs very much like a regular college course in which the students are expected to do weekly homeworks after studying the presentation material but it is free and can be taken online. There is also class group alias where students can ask/answer questions. The complete set of course contents (StarOffice slides with detailed speaker notes and flash demo files, hands-on labs, homework assignments, etc.) are available on the website of the course. The only thing you have to do in order to register for the course is sending an blank email to

ajaxworkshop-subscribe@yahoogroups.com.



Site Navigation