The final step shows how to use the Supplier business object and the scripting from a client perspective. Two examples will be shown, one accessing the script engine in a JSP by using the supplied taglib, and the other in code, such as from a JUnit test. It should be noted that usually you will not need to directly access the script engine. Instead, access will be provided by an eQ!-supplied adapter, such as when using the Struts web framework; however, it is shown here for clarity and understanding purposes.
JSP - This example uses the eQ! taglib for executing the script
engine. It also shows usage of the JSTL
(Java Standard Tag Library) for outputting the properties of the Supplier
objects. The usage of the taglibs is highlighted:
<!-- Import the eQ! taglibrary -->
<%@ taglib uri="/WEB-INF/eq.tld"
prefix="eq"
%>
<!-- Import the JSTL taglibrary -->
<%@ taglib uri="/WEB-INF/c.tld"
prefix="jstl-core"
%>
<html>
<body>
<p align="center"><b>Suppliers</b></p>
<!--
Setup the script tag -->
<eq:script
scriptName="retrieve_business_object_set">
<!--
Setup the persisterId we want to use -->
<eq:script-attribute
name="persisterId"
value="default"
/>
<!--
Execute the script -->
<eq:script-execute
/>
<!--
Put the set into the pageContext. This is currently required
so
that the JSTL tags can 'see' our BusinessObjectSet.
-->
<%
pageContext.setAttribute( "set", scriptResponse.getAttribute( "supplierSet"
) ); %>
<!--
Create an HTML table showing the results -->
<table>
<tr>
<td>Name</td>
<td>Street</td>
<td>City</td>
<td>State</td>
<td>Zip</td>
</tr>
<!--
Loop through the set, outputting the properties of each Supplier -->
<jstl-core:forEach
var="supplier"
items="${set.iterator}"
>
<tr>
<td><jstl-core:out
value="${supplier.name}"
/></td>
<td><jstl-core:out
value="${supplier.street}"
/></td>
<td><jstl-core:out
value="${supplier.city}"
/></td>
<td><jstl-core:out
value="${supplier.state}"
/></td>
<td><jstl-core:out
value="${supplier.zip}"
/></td>
</tr>
</jstl-core:forEach>
</table>
</body>
</html>
Java Code - Here is some code with comments inline that is set up as a JUnit test method:
public void testSupplierSetRetrieve() throws Exception {
ScriptEngine engine = null;
// Get a reference to the
engine
engine = (ScriptEngine)SystemContainer.getManager().lookup(ScriptEngine.ROLE);
// Create a request
ScriptRequest request = new ScriptRequest();
// Set the name of the script
to execute
request.setScriptName("retrieve_business_object_set");
// Create a Hashtable for
the params used by the SQL statement
java.util.Hashtable params = new java.util.Hashtable();
params.put( "value", "Joe" );
// Put the params into the
request
request.setAttribute( "params", params );
// Put the persisterId into
the request
request.setAttribute( "persisterId", "bySupplierName"
);
// Execute the request and
get a response
ScriptResponse response = engine.execute( request
);
// Get the set out of the
response
BusinessObjectSet set = (BusinessObjectSet)response.getAttribute(
"bo_set" );
assertNotNull( "No set was returned", set
);
assertTrue( "The set is empty", set.getRowCount()
> 0 );
// Iterate through the set
and display each Supplier
Iterator it = set.getIterator();
while( it.hasNext() ) {
Supplier supplier = (Supplier)it.next();
System.out.println( supplier.getName()
);
//
Or using the symbolic means of property access:
System.out.println( supplier.getValue(
"name" ) );
}
}
Using this example code, and the SQL defined in the Set
Persister Config step, the resulting SQL call would look something like
this:
SELECT supplier_id, name,
street, city, state, zip
FROM Suppliers
WHERE name LIKE '%Joe%'
Next: Summary