Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass an arraylist from Action to JSP

Status
Not open for further replies.

s4sudha

Programmer
Nov 10, 2003
3
IN
hi pple

am a struts-beginner and i need help on passing an Arraylist frm Action to JSP.

-- users access ContactPage.jsp
-- present text boxes and a combo box with values loaded from DB

-- SetUpMessageAction is called when ContactPage.jsp is called and i load an arraylist here
-- next, i need send the arraylist to ContactPage.jsp

i cud use Session or request.setAttribute ( ) to get this done. but i am sure there is a 'struts-way' of doing this.

in the execute ( ) of SetUpMessageAction, i have:

Code:
MessageService service = new MessageService ( ) ;
DynaActionForm messageForm = ( DynaActionForm ) form ;
Collection categories = service.getCategories ( ) ;
request.setAttribute ( mapping.getAttribute ( ), messageForm ) ;
return ( mapping.findForward ( "continue" ) ) ;


i have a CategoryBean with int id, String description. in service.getCategories ( ), i do

Code:
 list.add ( new CategoryBean ( 1, "Accounting" ) ) ;
repeatedly to populate the Arraylist. This is supposed to make a 'select' from the DB. For now, i populate it manually.


from Action, request is fwd-ed to ContactPage.jsp, where I need to get the 'categories' collection to populate my combo box.

this is how i try to populate the combo box:


Code:
<html:select name=&quot;messageForm&quot; property=&quot;category_id&quot;>     <html  ptions collection=&quot;categories&quot; property=&quot;id&quot;    labelProperty=&quot;description&quot;/></html:select>

in my struts-config.xml, this is wht i have:

Code:
<form-beans>        
<form-bean  name=&quot;messageForm&quot;                    
                   dynamic=&quot;true&quot; 
                   type=&quot;org.apache.struts.validator.DynaValidatorForm&quot;> 
      <form-property name=&quot;name&quot; type=&quot;java.lang.String&quot;/>            
      <form-property name=&quot;email&quot; type=&quot;java.lang.String&quot;/>            
      <form-property name=&quot;category_id&quot; type=&quot;java.lang.String&quot;/>            
      <form-property name=&quot;message&quot; type=&quot;java.lang.String&quot;/>        
 </form-bean>    
</form-beans>

the exception i get is:


Code:
exception javax.servlet.ServletException: Cannot find bean under name categories     org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:673)     org.apache.jsp.ContactForm_jsp._jspService(ContactForm_jsp.java:227)     org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)     javax.servlet.http.HttpServlet.service(HttpServlet.java:856)     org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)     org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)     org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)     javax.servlet.http.HttpServlet.service(HttpServlet.java:856)root cause javax.servlet.jsp.JspException: Cannot find bean under name categories     org.apache.struts.taglib.html.OptionsTag.getIterator(OptionsTag.java:407)     org.apache.struts.taglib.html.OptionsTag.doEndTag(OptionsTag.java:232)     org.apache.jsp.ContactForm_jsp._jspx_meth_html_options_0(ContactForm_jsp.java:460)     org.apache.jsp.ContactForm_jsp._jspx_meth_html_select_0(ContactForm_jsp.java:433)     org.apache.jsp.ContactForm_jsp._jspx_meth_html_form_0(ContactForm_jsp.java:358)     org.apache.jsp.ContactForm_jsp._jspService(ContactForm_jsp.java:217)     org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)     javax.servlet.http.HttpServlet.service(HttpServlet.java:856)     org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)     org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)     org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)     javax.servlet.http.HttpServlet.service(HttpServlet.java:856)

what does that mean? am i not setting 'categories' explicitly in Action? what am i missing here?

thanks a ton.

-- $uDhA
 
request.setAttribute is the correct way of passing object from action class to JSP.

In your execute() method of your SetUpMessageAction class,

ArrayList list = new ArrayList();
list.add(new CategoryBean ( 1, &quot;Accounting&quot; ) ) ;
list.add(new CategoryBean ( 2, &quot;Housekeeping&quot; ) ) ;
......

request.setAttribute(&quot;categories&quot;, list);
// 'categories' would be the bean name in JSP

return mapping.findForward ( &quot;continue&quot; );

assuming in your struts.config file, you have the SetUpMessageAction mapped as:

<action path=&quot;setupMessage&quot;
type=&quot;yourPackage.SetUpMessageAction&quot;
name=&quot;messageForm&quot;>
<forward name=&quot;continues&quot; path=&quot;/setup.jsp&quot;/>
</action>

and you have another class SetupMessageSubmitAction will handle the busincess logic for the submitted form. And in your struts.config you will have:

<action path=&quot;/setupMessage&quot;
type=&quot;yourPackage.SetUpMessageSubmitAction&quot;
name=&quot;messageForm&quot;>
<forward name=&quot;continues&quot; path=&quot;/setup.jsp&quot;/>
</action>

<action path=&quot;/setupMessageSubmit&quot;
type=&quot;...SetUpMessageSubmitAction&quot;
name=&quot;messageForm&quot;>
<forward name=&quot;continues&quot; path=&quot;/setup_confirm.jsp&quot;/>
</action>

Then in the setup.jsp page you would have something like:

<html:form action=&quot;/setupMessageSubmit&quot;>
<html:select property=&quot;category_id&quot;>
<html:eek:ptions collection=&quot;categories&quot; property=&quot;id&quot; labelProperty=&quot;description&quot;/>
</html:select>





 

hi byam:

thanks for the response.

but isnt that exactly what am doing??

in execute() of SetUpMessageAction class:

Code:
ArrayList categories = service.getCategories ( ) ;
request.setAttribute ( &quot;categories&quot;, categories ) ;
return ( mapping.findForward ( &quot;continue&quot; ) ) ;

in struts-config.xml:
Code:
<action path=&quot;/setUpMessageForm&quot;
            type=&quot;net.pilot.SetUpMessageAction&quot;
            name=&quot;messageForm&quot;
            scope=&quot;request&quot;
            validate=&quot;false&quot;>
            <forward
                    name=&quot;continue&quot;
                    path=&quot;/ContactForm.jsp&quot;/>
</action>

i don't need an intermediate 'SetupMessageSubmitAction' class.

in my JSP:

Code:
 <html:select property=&quot;category_id&quot;>
      <html:options
                 collection=&quot;categories&quot;
                 property=&quot;id&quot;
                 labelProperty=&quot;description&quot;/>

why am i still getting the error????

Code:
javax.servlet.ServletException: Cannot find bean under name categories

please help.

thanks again
-- $uDhA
 
I tested the code. It works. Are you sure that your service.getCategories ( ) is not returning null?
 
thanks for the response.

i copied all my files to another directory and worked frm this new folder. and it works now. its absolutely crazy and ended up losing 2 days of work and a lot of sleep!

thanks again
-- $uDhA
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top