我正在使用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>
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;
}
当我尝试检索表单提交的信息时,会出现问题。 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");
}
我知道我可以使用Vector(一维数组)并且它工作正常,但遗憾的是我需要遵循一些规范(并且规范强迫我使用具有10x10矩阵的类MyForm ......)。如何使用struts填充二维数组的正确方法?
感谢您的帮助!
答案 0 :(得分: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;
}
}
然后在你的表单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;
}
并在您的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">
</td>
</tr>
<tr>
<td align="center" valign="top" colspan="2">
<html:submit property="command" value="Test"></html:submit>
</td>
</tr>
</table>
当您提交该表单时,您将获得填充了值的所有MatrixRow对象列。
我希望这会对你有所帮助。我没有在Struts1中找到任何其他使用多维数组的方法。