Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Action Class or Regular Java Class to use?

 
prakash chauhan
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

-thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be sure to ask Struts questions in the Struts forum. I have moved this post there for you.
 
Brent Sterling
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic