You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Bussie, Andre D" <an...@lmco.com> on 2004/07/30 15:07:36 UTC

Newbie - DataBase

Is there a way in the Jakarta Struts framework to perform a database
query in an Action class w/o using a Action Form Class?

 

Scenario:

I want the user to click on a link once the link is selected provide an
action class to do the database logic and then display result of
database function on a jsp.

 

Is this possible with the Struts Framework?

 

 


Re: Newbie - DataBase

Posted by Paul Barry <pa...@nyu.edu>.
I think why you would want to do it is pretty obvious, you need to get data from the database to display on your page, 
such as a list of items.  A better question is why would you not want to.  That answer to that question is that it would 
be better to encapsulate your database query in a Data Access Object(DAO) and have your action call the DAO.  Here is a 
sample of the execute method for doing it with the DAO:

private ItemDAO dao = DAOFactory.getItemDAO();

public ActionForward execute(ActionMapping mapping, ActionForm form,
         HttpServletRequest request,
         HttpServletResponse response)
     	throws Exception {

     request.setAttribute("items",dao.getItems());
     return mapping.findForward("list");

}

The idea would be that ItemDAO is an interface and the getItems method returns a collection of items.  You would write 
an implementation of the ItemDAO where the getItems method does the database query.  DAOFactory.getItemDAO() is a static 
method that returns an instance of an object that implements the ItemDAO interface.

As your application gets more complicated, you may want to have the action call a service, which in turn calls the dao, 
but if your page is pretty simple, that step probably won't be needed.


Peng Tuck Kwok wrote:

> On Fri, 30 Jul 2004 09:07:36 -0400, Bussie, Andre D
> <an...@lmco.com> wrote:
> 
>>Is there a way in the Jakarta Struts framework to perform a database
>>query in an Action class w/o using a Action Form Class?
> 
> 
> It sure is. 
> The next question would be: why would you want to do that in the action ? :D
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Newbie - DataBase

Posted by Peng Tuck Kwok <pe...@gmail.com>.
On Fri, 30 Jul 2004 09:07:36 -0400, Bussie, Andre D
<an...@lmco.com> wrote:
> Is there a way in the Jakarta Struts framework to perform a database
> query in an Action class w/o using a Action Form Class?

It sure is. 
The next question would be: why would you want to do that in the action ? :D

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org