aspose file tools*
The moose likes Struts and the fly likes array of arrays and a form Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "array of arrays and a form" Watch "array of arrays and a form" New topic
Author

array of arrays and a form

elemak valek
Greenhorn

Joined: Mar 22, 2006
Posts: 6
Hi, my problem is not to show up the information,the logic:iterate inside logic:iterate works fine, the problem is to get the information again by tyhe form.
------
<logic:iterate name="calificacionForm" id="calificacion" property="matriculas" indexId="index">

<bean:write name="calificacion" property="codigo"/><html:hidden name="calificacion" property="codigo" indexed="true"/>

<bean:write name="calificacion" property="nombre"/><html:hidden name="calificacion" property="nombre" indexed="true"/>


<logic:iterate name="calificacion" id="nota" property="notas" indexId="index2">

<html:text name="nota" property="nota" indexed="true" size="3"/>

</logic:iterate>
</logic:iterate>
-----
I cant get the "notas" information for each "calificacion" to the form in the internal logic:iterate.
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
Maybe Merrill will come along with a better answer, but I am suspecting that your nested html:text tag will have to look something like this:

<html:text name="calificacionForm" property='<%= "matriculas[" + index + "].notas[" + index2 + "].nota" %>' size="3"/>

or maybe this:
<html:text name='<%= "calificacionForm.matriculas[" + index + "].notas" %>' property="nota" indexed="true" size="3"/>

- Brent
[ March 22, 2006: Message edited by: Brent Sterling ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I believe Brent's first solution should work. You should also remove indexed="true" from the <html:text> tag.

Using the indexed attribute works OK for simple forms with only one array list, but for multiple arrays, I've found that it's better just to take control of creating the property name so that you're sure you get a property name that will correctly reflect the structure of the data.
[ March 22, 2006: Message edited by: Merrill Higginson ]

Merrill
Consultant, Sima Solutions
elemak valek
Greenhorn

Joined: Mar 22, 2006
Posts: 6
Hey thanks for the help,I tried that way, but there is another problem now:
it cant populate the form because of this...
---
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
java.util.ArrayList.RangeCheck(ArrayList.java:547)
java.util.ArrayList.get(ArrayList.java:322)
org.apache.commons.beanutils.PropertyUtils.getIndexedProperty(PropertyUtils.java:521)org.apache.commons.beanutils.PropertyUtils.getIndexedProperty(PropertyUtils.java:428)org.apache.commons.beanutils.PropertyUtils.getNestedProperty(PropertyUtils.java:770)org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:801)org.apache.commons.beanutils.BeanUtils.setProperty(BeanUtils.java:881)org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:808)org.apache.struts.util.RequestUtils.populate(RequestUtils.java:1252)org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.java:821)org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:254)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
-----

I have to mention Im working with ArrayList, and just like before, it shows the information, but after the submit, this exception. I cant find the reason for an IndexOutofBounds...
Frank Bueckert
Greenhorn

Joined: Mar 23, 2006
Posts: 8
Can you post your form-bean class and maybe your .jsp, please? It might be you're attempting to pass too much, or not enough. Or maybe nothing.
elemak valek
Greenhorn

Joined: Mar 22, 2006
Posts: 6
Hi, this is the CalificacionForm
-----
//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_3.8.3/xslt/JavaClass.xsl
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import co.edu.icesi.notas.*;
public class CalificacionForm extends ActionForm {
// --------------------------------------------------------- Instance Variables

/** matriculas property */
private ArrayList matriculas;
private String nombre;

// --------------------------------------------------------- Methods
public void reset(){
matriculas=new ArrayList();
}
public CalificacionForm(){
reset();
}
/**
* Returns the matriculas.
* @return ArrayList
*/
public ArrayList getMatriculas() {
return matriculas;
}
public Calificacion getCalificacion(int index){
while(index>=matriculas.size()){
matriculas.add(new Calificacion());
}
return (Calificacion) matriculas.get(index);
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
}
-----

the list matriculas has an ArrayList filled with Calification objects
A Calificacion object has an ArrayList filled with Nota objects
Nota has an attribute named nota

and the jsp part:

<logic:iterate name="calificacionForm" id="calificacion" property="matriculas" indexId="index">

<bean:write name="calificacion" property="codigo"/><html:hidden name="calificacion" property="codigo" indexed="true"/>
<bean:write name="calificacion" property="nombre"/><html:hidden name="calificacion" property="nombre" indexed="true"/>

<logic:iterate name="calificacion" id="nota" property="notas" indexId="index2">

<html:hidden name="calificacionForm" property='<%= "matriculas[" + index + "].notas[" + index2 + "].nota" %>' />
<html:text name="calificacionForm" property='<%= "matriculas[" + index + "].notas[" + index2 + "].nota" %>' size="3"/>

</logic:iterate>
Frank Bueckert
Greenhorn

Joined: Mar 23, 2006
Posts: 8
Ok. So according to your code and your explanation, CalificacionForm is structured as follows:


Am I right with that? Seems kinda off from what you've been describing. Can you post the java file that is handling the submit, please? I suspect it's attempting to access an empty ArrayList.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Any time you use indexed properties you run into a problem when the ActionForm is in request scope. You build this structure of arrays within arrays, and everything works fine to display the page, but what happens when you're ready to submit the form? Since the ActionForm is in request scope, that bean as well as any objects you put in it is completely gone! Struts instantiates a new bean for you, but the newly instantiated bean doesn't have the array of arrays that the old one had. That's why you're getting the array index error: You're expecting a structure to exist within your ActionForm that no longer exists.

What to do?

The easiest way to solve this problem is simply to change the scope of your ActionForm from request to session. If the bean is in session scope, the structure you created before the form was displayed will still be there when the form is submitted.

If for some reason you don't want to put the ActionForm in session scope, you will have to rebuild the structure prior to the setters being called by struts. One way to do this is to override the reset() method in ActionForm. Since this method is called by Struts before it calls the setters, it's a good place to put code to rebuild the structure of the ActionForm.

Another method is "lazy initialization", which means you put code in the getters to instantiate objects if they don't exist. Here is a link explaining that method
[ March 23, 2006: Message edited by: Merrill Higginson ]
elemak valek
Greenhorn

Joined: Mar 22, 2006
Posts: 6
Hey , thanks a lot !!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: array of arrays and a form