• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Action Class or Regular Java Class to use?

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please be sure to ask Struts questions in the Struts forum. I have moved this post there for you.
 
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic