How can we write more than formbean in Action class?

Questions by mytramesh   answers by mytramesh

Showing Answers 1 - 4 of 4 Answers

santosh nimbolkar

  • Oct 16th, 2006
 

we can use the dynaactionForm instade of only formbeans . the dynaactionform is framework class provided by the apache struts. Using this perticular class we do not need to write formbeans this will generate the beans automatically only need to mapp the fields to struts-config.xml and the datatypes of the fiels this will generate the fields and their setter and getter methods too........

for more details find dynaActionForm search on google.....

thanks,

Santosh

  Was this answer useful?  Yes

satish

  • Jan 24th, 2007
 

Hi,We can make use of more than one form beans in a single Action class.By default every form bean object will be stored in the session.If we take the registration process, the user has to fill more than on form to be successfully registered.from the first form Forward to the another form(jsp) like ---every form bean will be stored in the session having the Attribute name as its form bean namenow you can get the form bean (presentBean1,presentBean2...................) in the RegAction class's execute(----) method from the session (reg.getSession()).And you can get the form bean objects using the attribute names of the form bean like ...session.getAttribute("presentBean1");session.getAttribute("presentBean2");and so on.......what ever the form bean (it may be ActionForm/DynaActionForm/..........)the form bean will be stored in the scope specified in the form bean tag.The scope attribute is default to session.

  Was this answer useful?  Yes

KidJava

  • Apr 13th, 2007
 

As per Struts convention only one formbean can be associated with one action class.

execute(ActionMapping am, ActionForm actionForm.... )

But if you want another form bean values you can do that with get attribute method.

# sample code to get value from other bean

public class DemoAction extends Action {

 

public ActionForward execute(ActionMapping mapping, ActionForm actionForm,

HttpServletRequest req, HttpServletResponse res) {

 

// Basic form bean associated

DemoFormBean demoFormBean = (DemoFormBean) actionForm;

 

// Getting other form bean value, if it is in session scope

OtherBeanOne otherBeanOne = (OtherBeanOne) req.getSession().getAttribute("OtherBeanOne");

 

// Getting other form bean value, if it is in request scope

OtherBeanTwo otherBeanTwo = (OtherBeanTwo) req.getAttribute("OtherBeanTwo");

 

// String in the get attribute is form name defined in the struts-config.xml

// <form name="OtherBeanOne" type="com.demo.OtherBeanOne"/>

 

return mapping.findForward("anyForward");

}

}

Hope this will help you

Thank
KidJava
rahul.shale@gmail.com

Jigar Sutaria

  • Sep 14th, 2007
 

Hi,

If I have understood your question properly. You want  to map two forms with one action class..

It can be done but its not really good idea to do by means of design if they are not from same inheritance.

Form Hierachy,
Like,  User -> Customer ->VIP  Customer  and User -> Dealer -> Premium Dealer

I would suggest its better if  you have sepeate action for processing customer and dealer but you can have one action to entertal Customer and VIP if they dont have much difference in business logic


You can have two action mapping entries

action="/path/CustomerAction" type="path.CustomerAction"
name="path.CustomerFrom"

action="/path/VIPCustomerAction" type="path.CustomerAction"
name="path.CustomerFrom"

First identify which kind of form is requested then  type cast it accordingly.

Hope I have justified your question..

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions