• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array inside Struts ActionForm

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Meera Godse
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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());
}


}
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic