This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Struts and the fly likes Array inside Struts ActionForm Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Array inside Struts ActionForm " Watch "Array inside Struts ActionForm " New topic
Author

Array inside Struts ActionForm

Meera Godse
Greenhorn

Joined: Jan 09, 2007
Posts: 28
Hi all,
I have gone thru all the struts thread on this site which described the same problem as this one but i am not clear with the explanations. so here is my precise problem:-
I want an ActionForm that accepts/displays a list of 10 'param,value' items in JSP.
eg:
class myForm extends ActionForm {
private Spec[] specs = new Spec[10] ;

...getSpecs()...
...setSpecs()...
}

The Specification class Spec looks like:-
class Spec {
private String param ;
private String val ;
...
}

So, ultimately i should have 10 items of the specs object in the above array. my jsp will therefore have an iterate tag as:-
<logic:iterate name="myForm" id="specs" property="specs">
parameter : <html:text name="specs" property="?" />
value : <html:text name="specs" property="?" />
</logic:iterate>

In this case, when i enter the property as 'param' & 'val'. Also tried using <html:text name="specs" property="specs" />. for both, i get an exception. like
-javax.servlet.ServletException:Cannot find bean: "specs" in any scope
-javax.servlet.jsp.JspException: Cannot find bean: "specs" in any scope

As per the suggestions on other threads I wrote 2 more setter, getter methods in my form bean as :-
public Spec getSpec(int index)
{
return (this.specs[index]);
}

public void setSpec(int index, Spec spec) {
this.specs[index].setParameter(spec.getParameter());
this.specs[index].setParamvalue(spec.getParamvalue());
}

can anyone help me in this.
[ January 09, 2007: Message edited by: Meera Godse ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

"MG MCS",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Try this:


The trick is that the id you specify in your <logic:iterate> tag must match the name of your indexed getter. So, if your indexed getter is getSpec, the name of your id must be spec

Note: If your actionForm is in request scope, you must initialize the array in your ActionForm either by overriding the reset method or in the constructor. By "initialize" I mean not only create the array, but also populate it with blank objects.
[ January 09, 2007: Message edited by: Merrill Higginson ]

Merrill
Consultant, Sima Solutions
Meera Godse
Greenhorn

Joined: Jan 09, 2007
Posts: 28
I tried that out. Now I am getting a NullpointerException in the Formbean. bean looks like below. I think the problem is in the constructor. (as per the stacktrace).

public class ProductForm extends ActionForm
{
private Spec[] specs = new Spec[2];

public ProductForm()
{
this.specs[0].setParam("para1");
this.specs[0].setVal("val1");
this.specs[1].setParam("para1");
this.specs[1].setVal("val1");
}

public Spec[] getSpecs() {
return specs;
}
public void setSpecs(Spec[] specs) {
this.specs = specs;
}

public Spec getSpec(int index)
{
return (this.specs[index]);
}

public void setSpec(int index, Spec spec) {
this.specs[index].setParam(spec.getParam());
this.specs[index].setVal(spec.getVal());
}
}

Is this right ?
Mattia Merenda
Ranch Hand

Joined: Dec 14, 2006
Posts: 47
Hi Meera,
If I've understood your problem, you have a bean named Spec that has 2 parameters and the getter and setter methods in it.Your form you work with the object Spec.I don't know how to work with your array but if you have only a simple Spec object in your ActionForm,you should try in your jsp this code.
<html:form action="specs">
parameter : <html:text property="specs.param" />
value : <html:text property="specs.value" />
<html:submit value="Continue" />
</html:form>

It works fine for one object.I have not tested the next code but you can try it:

<html:form action="specs">
<% for(int i=0;i<=10;i++) {%>
parameter : <html:text property="specs.param" />
value : <html:text property="specs.value" />
<% } %>
<html:submit value="Continue" />

I hope this helps.
Mattia
</html:form>
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The problem is in this code:

When you initialize an array (specs = new Spec[2]) the array elements are still null unless you put something in them. Therefore you're going to get a null pointer exception when you execute:

because there's no object in that space. Change your constructor to:

[ January 10, 2007: Message edited by: Merrill Higginson ]
Meera Godse
Greenhorn

Joined: Jan 09, 2007
Posts: 28
Thank you so much Merrill. Your suggestion worked.
It never occured to me that the problem was in the basic array declaration. Thanks again !
rgds,
meera.
prabakaran perumal
Greenhorn

Joined: Jan 24, 2007
Posts: 27
In the above mentioned code, is there any option to get the values in the action class after submitting form. or i need to use javascript to collect all the data and set into hidden field.
prabakaran perumal
Greenhorn

Joined: Jan 24, 2007
Posts: 27
Hi all

I checked, everthing is working fine.

This is a good example for array of text or etc.. in action form

code.
jsp page
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<html:html>
<head>

<title>GL Test</title>
</head>
<body>
<html:form action="/GlTest">
<% for(int i=0; i<2; i++){ %>
<html:text property='<%= "arrayForm[" + i + "].glCode"%>'/>
<html:text property='<%= "arrayForm[" + i + "].operatingUnit"%>'/>
<% }%>
<html:submit/>
</html:form>

</body>
</html:html>


action class

package com.scb.gltest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class GlTestAction extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
try {
System.out.println("This is my test for array of index");
GlTestForm glTestForm = (GlTestForm) form;

if (glTestForm != null) {
for (int i = 0; i < 2; i++) {
ArrayForm arrayform = glTestForm.getArrayForm(i);
System.out.println("gl code ="+ arrayform.getGlCode());
System.out.println("operating unit ="+arrayform.getOperatingUnit());

}
}

} catch (Exception e) {
e.printStackTrace();
}
return mapping.findForward("success");
}
}

action form
package com.scb.gltest;

import org.apache.struts.action.ActionForm;

public class GlTestForm extends ActionForm {

ArrayForm arrayForm[] = new ArrayForm[2];

public GlTestForm() {
// TODO Auto-generated constructor stub
for (int i = 0; i < 2; i++) {
arrayForm[i] = new ArrayForm();
}

arrayForm[0].setGlCode("mytestGL1");
arrayForm[0].setOperatingUnit("mytestoperting1");
arrayForm[1].setGlCode("mytestGL2");
arrayForm[1].setOperatingUnit("mytestoperting2");

}

public ArrayForm[] getArrayForm() {
return arrayForm;
}

public void setArrayForm(ArrayForm[] arrayForm) {
this.arrayForm = arrayForm;
}

public ArrayForm getArrayForm(int index) {
return this.arrayForm[index];
}

public void setArrayForm(int index,ArrayForm arrayForm) {
this.arrayForm[index].setGlCode(arrayForm.getGlCode());
this.arrayForm[index].setOperatingUnit(arrayForm.getOperatingUnit());
}


}
Eleazar Araujo
Greenhorn

Joined: Oct 20, 2012
Posts: 2
Hi, im having the same problem. But i can't solve it the way described above because the size of my array "specs" is dinamic. What can i do?

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array inside Struts ActionForm
 
Similar Threads
Simple Question
how to obtain list object from an actionform
using indexId
Validation rule for array of objects.
HashMap mapped property / action form