File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Action Class or Regular Java Class to use? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Action Class or Regular Java Class to use?" Watch "Action Class or Regular Java Class to use?" New topic

Action Class or Regular Java Class to use?

prakash chauhan
Ranch Hand

Joined: Jun 22, 2007
Posts: 81
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?

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63838

Please be sure to ask Struts questions in the Struts forum. I have moved this post there for you.

[Asking smart questions] [About Bear] [Books by Bear]
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
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.

- Brent
I agree. Here's the link:
subject: Action Class or Regular Java Class to use?
It's not a secret anymore!