Providers Configuration
Providers are declared in an xml file:
<providers>
<provider name="countries"
className="ar.com.koalas.providers.FixedProvider">
<set-property property="keyName" value="key"/>
<set-property property="description" value="value"/>
<value key="01" value="Argentina" tipo="SouthAmerica"/>
<value key="02" value="Brasil" tipo="SouthAmerica"/>
<value key="03" value="Italia" tipo="Europe"/>
<value key="04" value="Espaņa" tipo="Europe"/>
</provider>
<provider name="southAmericanCountries"
className="ar.com.koalas.providers.RefProvider">
<set-property property="reference" value="fixedTest"/>
<set-property property="filter" value="//*[@tipo='SouthAmerica']"/>
</provider>
<provider name="cities"
className="ar.com.koalas.providers.JDBCProvider">
<set-property property="jndi" value="MySqlDS"/>
<set-property property="keyName" value="code"/>
<set-property property="description" value="description"/>
<param>
<param-name>query</param-name>
<param-value>select * from cities</param-value>
</param>
</provider>
</providers>
countries provider uses a fixed provider and has a declaration of countries. southAmericanCountries
uses a reference provider that filters countries using XPath, to get only south american countries. Finally,
cities provider uses JDBC provider to obtain a collection with an SQL statement.
Providers JSP usage
These providers are used in the JSP in the following way:
<%@ taglib uri="/WEB-INF/tld/providers.tld" prefix="prv" %>
<html>
<body>
<form name="test">
<select name="selTest">
<prv:options provider="fixedTest"/>
</select>
<select name="selTest">
<prv:options provider="refTest"/>
</select>
</form>
</body>
</html>
ComboSelect example (new features!)
In the previous Providers version, you needed to supply a collection in a matrix fashion
and set it in the request scope in order to CombSelectTag to work, and indicating the "metadata" of the collection.
Currently, this is supported for backwards compatibility but now this functionality can be achieved as follows:
We have 3 database tables with te following information:
COUNTRIES |
idCountry | country |
1 | Argentina |
2 | Brasil |
|
STATES |
idCountry | idState | state |
1 | 01 | Buenos Aires |
1 | 02 | Cordoba |
2 | 09 | Rio de Janeiro |
2 | 22 | Bahia |
|
CITIES |
idCountry | idState | idCity | city |
1 | 01 | 56 | Tandil |
1 | 01 | 77 | Ramos Mejia |
1 | 02 | 89 | Carlos Paz |
2 | 09 | A6 | Ipanema |
2 | 22 | A1 | Salvador De Bahia |
|
Afterwards, you define 3 JDBCProviders to get data from that tables:
<provider name="JDBCCountries"
className="ar.com.koalas.providers.JDBCProvider">
<set-property property="jndi" value="MSSQLServer"/>
<set-property property="initialContext"
value="java:comp/env"/>
<set-property property="keyName" value="idcountry"/>
<set-property property="description" value="country"/>
<param>
<param-name>query</param-name>
<param-value>select * from countries</param-value>
</param>
</provider>
<provider name="JDBCStates"
className="ar.com.koalas.providers.JDBCProvider">
<set-property property="jndi" value="MSSQLServer"/>
<set-property property="initialContext"
value="java:comp/env"/>
<set-property property="keyName" value="idstate"/>
<set-property property="description" value="state"/>
<param>
<param-name>query</param-name>
<param-value>select * from states</param-value>
</param>
</provider>
<provider name="JDBCCities"
className="ar.com.koalas.providers.JDBCProvider">
<set-property property="jndi" value="MSSQLServer"/>
<set-property property="initialContext"
value="java:comp/env"/>
<set-property property="keyName" value="idcity"/>
<set-property property="description" value="city"/>
<param>
<param-name>query</param-name>
<param-value>select * from cities</param-value>
</param>
</provider>
Then, assuming that the database connection is available in MSSQLServer jndi name, you just place the following
tags in the JSP page:
<%@ taglib uri="/WEB-INF/tld/providers.tld" prefix="prv" %>
<%@ taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html" %>
<html>
<body>
<h1>ComboSelect Example</h1>
<p>These selects are dependant.
<html:form action="demo">
<prv:comboselect>
<prv:select property="country2" doMatches="true">
<prv:options provider="JDBCCountries" />
</prv:select>
<prv:select property="state" doMatches="true">
<prv:options provider="JDBCStates" />
</prv:select>
<prv:select property="city" doMatches="true">
<prv:options provider="JDBCCities" />
</prv:select>
</prv:comboselect>
</html:form>
</body>
</html>
Also, SelectTag has an attribute doMatches, that generates a JavaScript to allow you to write on the select and
this select will do matches to find the corresponding option.
HTML generated will be seen like this:
DefineTag example
If you need to define a bean containing the collection returned by a provider, you can use this tag:
<prv:define id="collectionBean" toScope="request" provider="fixedTest"/>
After this, you could use "collectionBean" to iterate it: this bean has the collection returned by fixedTest provider.
|