|
網誌存檔
熱門網志
|
|
|
This tutorial shows you how you can configure Struts DataSource Manager on the Tomcat 5.5.9 server. We will use struts 1.2.7 in this tutorial. In this tutorial we will configure Struts DataSource Manager to use MySQL Database and use the connection provided by Struts DataSource in action class. Downloading and Installing Tomcat 5.5.9 Download Struts Download MySQL JDBC Driver Creating MySQL Database Create database "strutsdatabase" on the MySQL server and then run the following query to create test table.
Above query creates test table and then populates the table with data. Configuring Struts Application Now start the tomcat and try to access the strutsdatabase application by typing the url http://localhost:8080/strutsdatabase in browser. Your browser should display the welcome page. After testing shutdown the tomcat server. Configuring Struts DataSource Manager Uncomment and then <data-sources> entry in the struts-config.xml and then change the line "org.apache.commons.dbcp.BasicDataSource" to "org.apache.tomcat.dbcp.dbcp.BasicDataSource". In tomcat 5.5.9 dbcp classes are packaged in naming-factory-dbcp.jar archieve, so we are using "org.apache.tomcat.dbcp.dbcp.BasicDataSource" instead of "org.apache.commons.dbcp.BasicDataSource". After this change the database dirver, database url and passwor in the <data-sources> tag. You <data-source> element should look like:
Create action Class to Test the DataSource
Following code is used to get the data source and then connection from Struts DataSource: dataSource = getDataSource(request); Save this file(TestDataSource.java) into jakarta-tomcat-5.5.9\webapps\strutsdatabase\WEB-INF\src\java\test directory. Add the servlet API into class path. Then open dos prompt and navigate to jakarta-tomcat-5.5.9\webapps\strutsdatabase\WEB-INF\src\ directory and issue run ant. This will compile the action class (TestDataSource.java) and copy it to the classes directory of the webapplication. Creating Action Mapping struts-config.xml
Running and testing
Now check tomcat console, it should display records fetched from database. You can download my struts-config.xml from here. Note: The DataSource manager is being retained in Struts 1.x for backward compatibility but may not be retained in Struts 2.x or later. |
-------------------------------------------------
| 上一篇:STRUTS ACTION - AGGREGATING ACTIONS IN STRUTS | 下一篇:STRUTS INTERNATIONALIZATION |


