Forum OpenACS Development: OT: JSP equivalent of OpenACS template tags

For any Java programmers: Is anyone aware of a easy-to-use JSP Tag Library that provides some of the functionality of the OpenACS template tags like <multiple>, <if> and @column-name@

One of the standard ways to get data from the database to a JSP page is to use the Model/View/Controller which means for a  db query you have to define a class for all the columns of the query result, in the Servlet populate a list of instances of these classes and then in the JSP iterate through this list of objects.

It is very tedious work and I can't see why a HashTable (or something) could not be used in a similar way as OACS populates various tcl lists with the results from the db query and then uses something like:

<multiple name="query">
<p>@query.name@: @query.email@
</multiple>

If anyone knows of a Tag Library that can do this please let me know.

Collapse
Posted by Talli Somekh on
Is that what JSTL is for?

talli

Collapse
Posted by David Cotter on
Thanks, yes that looks good.
Collapse
Posted by Peter Marklund on
David,
you may also want to check out Struts (http://jakarta.apache.org/struts/index.html) which is quite commonly used.
Collapse
Posted by Dave McBride on
I am not sure if I have used what you described, but I wrote a manufacturing app using JSPs hooked to postgresql running with the Tomcat web server.

My JSP's looked very similar to OpenACS .tcl files, like the old 3.x type that did not have .adp pages associated with them (see old version of ecommerce package).

It connected to the db, did a query, which got back a result set, then I did a while loop. See example:

  Statement stmt2 = con.createStatement();
  String queryStr2 = "SELECT part_id, part_name, sku, barcode, count, minimum FROM manu_parts where active='t' AND wip='f' AND count < minimum order by sku";
  ResultSet rs_parts = stmt2.executeQuery(queryStr2);

<%  while(rs_parts.next()) {
      count++; %>
      <tr>
        <td><%= count %>.</td>
        <td>Part</font></td>
        <td><%= rs_parts.getString("part_name") %></td>
      </tr>

<%    } // end while() %>

Here is an 'if' statement for JSP's as well.

<%    if (rs_products.getInt("count") >= rs_products.getInt("minimum")) { %>
        <td align=center><font size=-1 ><%= rs_products.getString("count")%></font></td>
<%    } else { %>
        <td align=center bgcolor="red"><font size=-1 color=white><%= rs_products.getString("count")%></font></td>
<%    } %>

I will send you versions of some of my files to show you how I did it if you would like to see them. I found this worked like the multiple in OpenACS. I can create JSP pages much like the OpenACS pages I write.

Dave McBride

Collapse
Posted by David Cotter on
Thanks. I didn't find quite what I was looking for. I wrote a very simple solution based a little bit on OpenACS.

A request comes in like:

/student/showDetails.do

Based on this request name, the controller servlet automatically loads a class called

com.company.product.student.ShowDetails

and calls a doRequest method.

It then redirects to a JSP called (by default but can something else if necessary)

/student/showDetails.jsp

A simple DB API which automatically sets the results in the request and then allows the results to ne iterated through in the JSP in a slightly similar way to the OpenACS templating tags.

To get a minimal page up and running I do 2 things:

1. Write the business logic class with the right name:


package com.blah.student;
public class  ShowDetails extends AutoRequestHandler {
    public void doRequest() {
        DB.dbMultirow("test", "select * from users where id < 1000", request);
    }
}

2. Write the JSP called /student/ShowDetails.jsp


<%@ taglib uri="/WEB-INF/tlds/db.tld" prefix="db" %>
<db:iterator name="test">
    <br><db:data name="email" />
</db:iterator>
where "email" is a column name returned in the query.

It is also possible to have conditionals on the rownum or on the data like <db:if test="rownum odd" /> or <db:if test "student_number eq 234" />


<%@ taglib uri="/WEB-INF/tlds/db.tld" prefix="db" %>
<table>
<db:iterator name="test">
<tr>
 <db:if test="rownum odd" />
  <td bgcolor="#003333">
 </db:if>
 <db:else>
  <td bgcolor="#006666">
 </db:else>
    <br><db:data name="email" />
  </td>
</tr>
</db:iterator>
</table>

Sorry to post here - it's only marginally OpenACS related. If anyone is interested in this or wants to tell me I'm waisting my time because there already exists such a solution then please email me.