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

How to get data back on result page from struts

 
RP Singh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ,

How do I get data back in the result jsp page from a action servlet. If I am retrieving data from a database through a model bean how do I get the data onto the output jsp page.

Does it have to go through the formbean and then read the formbean to output the data.

Please add some details.

Thanks

RP
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by RP Singh:
How do I get data back in the result jsp page from a action servlet. If I am retrieving data from a database through a model bean how do I get the data onto the output jsp page.



First you asked.. how to getback data in jsp from ActionServlet? Then you asked how to get it in jsp from model.

What exactly is your question? If you are asking data retrieval from model in some jsp then here is the way of doing it.


Code below




Regards

Naseem
 
RP Singh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Naseem,

The MyTransferObject which will return the data from model bean, you refer it to as a transfer object. Do you mean this MyTransferObject(bean) needs to extend actionForm , I mean is it a formBean.

My question is does the data coming from Model has to come through a FormBean.

Thanks for the help

RP

quote:
--------------------------------------------------------------------------------

Originally posted by RP Singh:
How do I get data back in the result jsp page from a action servlet. If I am retrieving data from a database through a model bean how do I get the data onto the output jsp page.

--------------------------------------------------------------------------------



First you asked.. how to getback data in jsp from ActionServlet? Then you asked how to get it in jsp from model.

What exactly is your question? If you are asking data retrieval from model in some jsp then here is the way of doing it.


Code below


code:
--------------------------------------------------------------------------------

public class TestAction extends Action{ public ActiuonForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest, request, HttpServletResponse response) throws Exception{ /** * Instantiate dao component. Suppose its TestDAO.class which has * meth() method which returns an instance of some TransferObject say * MyTransferObject. */ TestDAO testdao=new TestDAO(); MyTransferObject obj=testdao.meth(); /** * This transfer object actually carries the data from dao * component. Once you got transfer object set it in some scope like * request scope. Forward request, response to Output jsp page. * Finally in Output jsp page retrieve it form request scope. */ request.setAttribute("SomeKey", obj); mapping.findForward("success"); }}

--------------------------------------------------------------------------------




Regards

Naseem
--------------------
 
HungryJavaGoat
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
it is not mandatory that it should come through a FormBean. FormBean is used when data is retrieved from the html form. ActionServlet will instantiate a FormBean and load the form bean with the html form data. This is based on the struts-config.xml.
However while you are trying to retrieve data from db and display it in jsp , we usually have normal java bean object (Transfer Object or Value object) which we will load and keep it in request or session scope based on the need. But you can also use FormBean for that ..But it is good design to use a Transfer Object (its normal java bean .. NOT ActionForm bean). Pls correct if something is wrong.

Regards,
Karthik
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic