如何使用Struts Form管理多维数组

pab*_*l0x 5 java web-applications struts-1

我正在使用Apache Struts 1.3来渲染网格,这是一个嵌入.jsp的html表单.就像是

<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>
Run Code Online (Sandbox Code Playgroud)

MyController与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;
}
Run Code Online (Sandbox Code Playgroud)

当我尝试检索表单提交的信息时出现问题.Whithin MyController.class我有一个简单的调度程序操作

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");

}
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用Vector(一维数组)并且它工作正常,但遗憾的是我需要遵循一些规范(并且规范强迫我使用具有10x10矩阵的类MyForm ......).如何使用struts填充二维数组的正确方法?

感谢您的帮助!

Nav*_*ved 6

Struts不支持在Form bean中填充多维数组.但是,它确实处理对象的一维数组.因此,如果您可以创建一个类(比如MatrixRow)本身包含一个Uni维数组,然后您可以在表单bean中创建该对象的Uni维数组.你的新课将会是这样的

    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;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在你的表单bean中

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;
    }
Run Code Online (Sandbox Code Playgroud)

在你的JSP中,你可以使用类似的东西

    <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>
Run Code Online (Sandbox Code Playgroud)

当您提交该表单时,您将获得填充值的所有MatrixRow对象列.

我希望这能帮到您.我在Struts1中找不到任何其他使用多维数组的方法.