Hi , In my program which i am trying to implement with MVC (Struts) i have created an Action class that fetches the data from database and then puts it on request using setAttribute on Request object and forwards it to my View which is a jsp file. The reason why i am using Action class is so as to get the DataSource object thats been configured in my struts-config.xml file easily. Is it okay to do it like this?
So the flow of this module is like this:
<html:link> calls Action class which setAttributes the data onto request and then forwards it to the next jsp using mapping.findForward("res"); method.
----- Then in the same program i have another Action class which actually updates the same database but after updating the database is supposed to again get data from the database and display the existing data plus the new data in that same jsp . how can i call the my first Action class which gets data from database from my second Action class which updates the data ?
And so i wanted to know if i should use a Java class for getting /setting data from/to the database? and call these from the Actions ? but then how will i get my DataSource object which is defined in struts-config.xml file, in this helper java class?
A couple things...I have read that it is not a good idea to use Struts to configure your data sources. I would agree but I don't know the details. I have always been working with EJB based projects where the container managed the database connections.
What you are doing in your first action sounds good. I try to avoid using the request directly so I would populate fields on a form with the data from the database.
What I do is to redirect from my "save" action over to my "display" action. Another advantage is that this updates the URL that the browser shows so that when the user clicks the refresh button it will call the "DisplayRecord" action and not call the "SaveRecord" action a second time. You just need to specify forward="true" on the mapping and add code in your save action to add an id to the forward url.
That's a very big dog. I think I want to go home now and hug this tiny ad: