| < Day Day Up > |
|
The first component that we configure is a DataSource. As we discussed in Chapter 11, "Integrating the Jakarta Commons Database Connection Pool (DBCP)," there are two methods for configuring a DataSource. With the first method, which is not applicable to this discussion, you directly manipulating a DataSource instance using one of your own components. The second method, which we demonstrate in this section, involves modifying the struts-config.xml file. This method leaves management of the DataSource in the capable hands of the Controller, allowing you, the developer, to focus on project-specific efforts.
When describing DataSource instances, you must use the <data-sources> element. This element contains n-number of <data-source> sub-elements, which describe each DataSource instance. The syntax of the <data-sources> element, containing a single <data-source> sub-element, is shown in the following code snippet:
<data-sources> <data-source type="org.apache.commons.dbcp.BasicDataSource"> <set-property property="driverClassName" value="com.mysql.jdbc.Driver" /> <set-property property="url" value="jdbc:mysql://localhost/stocks" /> <set-property property="username" value="YOUR USERNAME" /> <set-property property="password" value="YOUR PASSWORD" /> </data-source> </data-sources>
As you can see, the <data-source> sub-element is described completely using <set-property> sub-elements. Each one of the properties listed in the <set-property> elements maps to a single <data-source> attribute. Table 15.3 describes the attributes of a <data-source> entry.
Attribute | Description |
---|---|
type | The fully qualified class name of the DataSource object being used in this <data-source> definition. This class must extend javax.sql.DataSource, and it must be completely configurable using <set-property> sub-elements (all configurable data members must satisfy the JavaBean specification). (optional) |
className | The fully qualified classname of the configuration object for the instance of this DataSource. The implementation of this object must extend org.apache.struts.config.DataSourceConfig. (optional) |
key | A unique key identifying a DataSource instance stored in the ServletContext. If this property is not used, then the key is defaulted to Action.DATA_SOURCE_KEY. If you intend to use more than one DataSource in your application, you must include a key for each one. (optional) |
As we stated earlier, the actual attributes used by a specific DataSource are dependent on that DataSource's implementation. Therefore, the contents of your particular <set-property> sub-elements also depend on your specific implementation.
This code snippet shows an example that uses the <data-source> sub-element:
<data-sources> <data-source type="org.apache.commons.dbcp.BasicDataSource"> <set-property property="driverClassName" value="com.mysql.jdbc.Driver" /> <set-property property="url" value="jdbc:mysql://localhost/stocks" /> <set-property property="username" value="YOUR USERNAME" /> <set-property property="password" value="YOUR PASSWORD" /> </data-source> </data-sources>
| < Day Day Up > |
|