How can I manage a multidimensional array using the Struts form

I use Apache Struts 1.3 to render the grid, whitch is a html form decorated with .jsp. Sort of

<html:form action="/MyController.do?action=processForm"> <html:text property="taxation[0][0]" value="" styleClass="gridInputs"></html:text> <html:text property="taxation[0][1]" value="" styleClass="gridInputs"></html:text> ... <html:text property="taxation[10][10]" value="" styleClass="gridInputs"></html:text> 

MyController is associated with ActionForm:

 public class MyForm extends ActionForm{ protected String taxation[][]= new String [10][10]; public String[] getTaxation() { return taxation; } public void setTaxation(String[][] taxation) { this.taxation = taxation; } 

The problem occurs when I try to get the information provided by the form. Whithin MyController.class I have a simple dispatcher action

 public class MyController extends DispatchAction { public ActionForward processForm(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { MyForm myform = (MyForm) form; // Here i can use the getter method to retrieve an array, but // myform is already wrong populated from struts } return mapping.findForward("stage2"); } 

I know that I can use Vector (a one-dimensional array) and it works fine, but unfortunately I need to follow some specifications (and the specifications force me to use the MyForm class with a 10x10 matrix ...). What would be the correct way to populate a two-dimensional array with struts?

Thanks for the help!

+4
source share
1 answer

Struts does not support filling a multidimensional array in a Form bean. However, it processes a one-dimensional array of the object. Therefore, if you can create a class (say, MatrixRow) that itself contains an Uni-dimensional array, then you can create a Uni-dimensional array of this object in the form of a bean. Your new class will look like

  public class MatrixRow { private String matrixCol[] = new String[10]; /** * @return the matrixCol */ public String[] getMatrixCol() { return matrixCol; } /** * @param matrixCol the matrixCol to set */ public void setMatrixCol(String[] matrixCol) { this.matrixCol = matrixCol; } } 

Then in your bean form

 private MatrixRow[] arrMatrix = new MatrixRow[10]; /** * @return the arrMatrix */ public MatrixRow[] getArrMatrix() { return arrMatrix; } /** * @param arrMatrix the arrMatrix to set */ public void setArrMatrix(MatrixRow[] arrMatrix) { this.arrMatrix = arrMatrix; } 

and in your JSP you can use it somehow like

  <html:form action="biArrayTestAction.do"> <table cellpadding="0" cellspacing="0" width="100%"> <logic:iterate id="matrixRows" name="biArrayTestForm" property="arrMatrix" indexId="sno" type="logic.MatrixRow" > <tr> <td><bean:write name="sno"/></td> <logic:iterate id="matrixCol" name="matrixRows" property="matrixCol" indexId = "colNo"> <td> <input type="text" name="arrMatrix[<%=sno %>].matrixCol[<%=colNo %>]"> </td> </logic:iterate> </tr> </logic:iterate> <tr> <td align="center" valign="top" colspan="2"> &nbsp; </td> </tr> <tr> <td align="center" valign="top" colspan="2"> <html:submit property="command" value="Test"></html:submit> </td> </tr> </table> 

When you submit this form, you will receive all the columns of the MatrixRow object filled with values.

Hope this helps you. I have not found another way to use a multidimensional array in Struts1.

+5
source

All Articles