Struts newbie question about logic:iterating
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Stephen_Montgomery
Posted On:   Thursday, February 10, 2005 03:36 PM

Hi - I have been going through quite a few web examples and haven't quite pieced together the right picture yet. 1) If I have an ActionForm for a JSP, how does the JSP know about it. I don't see where anything is defined when I call html:form. I'm guessing that the form submit action looks up in the struts config file what the corresponding ActionForm name is and loads that into scope. Cool. 2) However, now how do you access other beans within this form. I would like to create some dynamic radio buttons so I created ANOTHER action form, defined it in in the struts-config file and low and behold it wasn't found (Cannot find bean in scope). Do I need to explicitly import this bean to g   More>>

Hi -




I have been going through quite a few web examples and haven't quite pieced together the right picture yet.




1) If I have an ActionForm for a JSP, how does the JSP know about it. I don't see where anything is defined when I call html:form. I'm guessing that the form submit action looks up in the struts config file what the corresponding ActionForm name is and loads that into scope. Cool.




2) However, now how do you access other beans within this form. I would like to create some dynamic radio buttons so I created ANOTHER action form, defined it in in the struts-config file and low and behold it wasn't found (Cannot find bean in scope). Do I need to explicitly import this bean to get it to work or is there a better way of doing it??? Like adding another ActionMapping or something.




3) If I import my bean explicitly in my JSP (the one listed for convenience below):



			
public class SourceDatabaseForm extends ActionForm {

private ArrayList sourceDatabases;

public SourceDatabaseForm() {
sourceDatabases = new ArrayList();
sourceDatabases.add(new LabelValueBean("Ensembl", "ENSEMBL"));
sourceDatabases.add(new LabelValueBean("NCBI", "NCBI"));
}

public Collection getSourceDatabases() {
return sourceDatabases;
}
}



and then make a statement like



			







I cannot get an iterator for my collection. Any idea what I am doing wrong???




Thanks so much for the help. I'm trying hard to get up to speed on this stuff - I think it is really cool.




All the best,

Steve-o

   <<Less

Re: Struts newbie question about logic:iterating

Posted By:   Mark_Hartnett  
Posted On:   Friday, February 11, 2005 06:18 AM

You can only have one formbean associated with an action. This association is defined in the struts-config.xml file by the 'name' attribute. It seems as though you see how that works.




validate="false"

name="listBoxForm"

type="com.pvr.support.ListBoxEditAction">










If you would like to include another javabean (weather it is a actionform extended bean or a POJO) you need to create and populate the bean and put it into either request or session scope.

For example, if you had an action of initListBoxEdit with the associated form of listBoxForm, the listBoxForm object would be populated automatically through the struts framework.



If you would like to include your SourceDatabaseForm bean as part of the request, then your action object must create it:



SourceDatabaseForm sbf = new SourceDatabaseForm();



Populate it:
sbf.setXXX('bbb'); (or however you want to populate it)



Place it in some scope:



request scope would be:

request.setAttribute("SourceDatabaseForm", sbf);



session scope would be:

request.getSession().setAttribute("SourceDatabaseForm", sbf);



Your iterate tag then would now have access to the bean even though it is not associated with the action. Make sure that your iterate tag specifies the scope of the bean that you are trying to use:







A second option is to create an attribute on the form associated with the action ( in the above example on the listBoxForm) that would hold the SourceDatabaseForm.




In addition to all the other attributes, listBoxForm would now have an attribute and getter and setter defined as:



SourceDatabaseForm sbf= null:
getSourceDatabaseForm() return sbf;
setSourceDatabaseForm(SourceDatabaseForm inSbf)
sbf = inSbf;



Instead of putting the SourceDatabaseForm into either request or session scope as in the first example, just use the setter method of the actionform (listBoxForm) associated with the action to put the new bean into the form.



Once the bean is associated with the actionform associated with the action then it is available in your jsp.




Hope this helps
About | Sitemap | Contact