2013-02-04 3 views
4

Apache Struts 1.3을 사용하여 격자를 렌더링합니다. html 양식은 .jsp에 embebed됩니다.Struts 양식을 사용하여 다차원 배열을 관리하는 방법

<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이 연관되어있다 : I 양식이 제출 한 정보를 검색 할 때 문제가

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; 
} 

을 발생한다. 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"); 

} 

은 내가 벡터 (일차원 배열)를 사용할 수 있습니다 알고있는 간단한 디스패처 작업을했습니다 그리고 그것은 잘 작동하지만, 유감스럽게도 나는 몇 가지 사양 (따를 필요와 사양에 저를 강제로 Myx 클래스를 10x10 매트릭스와 함께 사용하십시오 ...). 스트럿을 사용하여 2 차원 배열을 채우는 올바른 방법은 무엇입니까?

도움 주셔서 감사합니다.

+1

해당 배열, 개체 또는 문자열 또는 숫자 내부의 값은 무엇입니까? – Naved

+0

디버그 모드에서 3x3 어레이로 시도합니다. 9 입력을 내 컨트롤러에 3x1 배열로 전송하면 값은 모두 문자열이지만 연속적이지는 않습니다. 내 말은 : 입력 값을 채 웠습니다. 1 2 3 4 5 6 7 8 9 그리고 ActionForm 객체에서 값 1 (postion [0] [0]) 4 (postion [1] [ 0]) 및 7 (위치 [2] [0]) – pabl0x

답변

4

Struts는 Form bean에서 다차원 배열 채우기를 지원하지 않습니다. 그러나 그것은 객체의 일차원 배열을 처리합니다. 따라서 Uni-dimensional 배열을 포함하는 클래스 (MatrixRow)를 생성 할 수 있다면 Form Bean에 해당 객체의 Uni-dimensional 배열을 생성 할 수 있다면 해결할 수 있습니다. 새 클래스는

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에서 양식 빈에서 다음

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; 
    } 
} 

과 같이 될 것입니다 당신이 양식을 제출하면 당신은 그것을

<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> 

같은 것을 사용 할 수 있습니다 MatrixRow 객체의 모든 열을 값으로 채 웁니다.

이 정보가 도움이되기를 바랍니다. Struts1에서 다차원 배열을 사용하는 다른 방법을 찾지 못했습니다.

관련 문제