Monday, May 26, 2008

Struts - Database Access by Defining Datasource within the struts-config.xml

A sample program which connecting to the MySQL database to display the data. I am using the Struts framework to developed this program. The main idea is of this post is to use the struts-config.xml to define the datasource and using Action class to access and select the data.

Please bear in mind, the Action class which represent as controller layer, should not contain any business logic code and database access code. The best pratice is to apply the DAO mechanism by putting your business logic or database access code within the model layer.
The datasource should only define within the struts-config.xml when there is a limitation of connectivity to the database within the model layer. Alternative, you can use the JNDI to define the datasource and that would be the best recommendation in best pratice. However, that will be my next post on my blog.



Here are the summary of development been used.

  • Struts 1.2.9 framework

  • index.jsp, selectBook.jsp, SelectBookActionForm.java - view layer

  • SelectBookAction.java - controller layer

  • Book.java - data layer

  • Defining datasouce at struts-config.xml

  • MySQL database
Read more and download the code: Click here

No comments: