2014-09-14 6 views
0

나는 사용자로부터 몇 가지 입력 배열을 얻었습니다. 예를 들어 years []라고 입력하고 컨트롤러에 값을 처리하십시오.플레이 프레임 워크에서 배열 값 바인딩하기

컨트롤러 :
컨트롤러 코드를 시도 바인드 에 폼 데이터

여기 내 코드입니다. 내 경우에는, HTML 양식에서 년 값의 배열을 얻을 :

Form<FinStatementInput> ipForm = Form.form(FinStatementInput.class).bindFromRequest();//Array out of bound exception is thrown at this line. 
    if (ipForm.hasErrors()) { 
     return badRequest("<h1>error</h1>").as("text/html"); 
    } else { 
     FinStatementInput ip = ipForm.get(); 
     System.out.println("first year input(to test)>>"+ ip.years[0]); 
     return ok(); 
    } 

FinStatementInput 

모델 :

public int[] years;//array declaration 
. 
. 
FinStatmentInput (int[] years) { 
this.years = years; // in the constructor 
} 

는 HTML :

<form id="Start Here" name="Start Here" style="display: none;" 
    action="@routes.Application.calculate()" method="post"> 
    <table class="table table-condensed"> 
    <tr> 
     <td id="tdInput" > 
      <div class="input-group"> 
      <span class="input-group-addon">Year 1</span> <input name="years[0]" id="GreenInput" 
      pattern="[0-9.]+" type="text" class="form-control" required="required"> 
      </div></td> 
     <td id="tdInput" > 
      <div class="input-group"> 
      <span class="input-group-addon">Year 2</span> <input name="years[1]" id="GreenInput" 
      pattern="[0-9.]+" type="text" class="form-control" required="required"> 
      </div></td> 
     <td id="tdInput" > 
      <div class="input-group"> 
      <span class="input-group-addon">Year 3</span> <input name="years[2]" id="GreenInput" 
      pattern="[0-9.]+" type="text" class="form-control" required="required"> 
      </div></td> 
     <td id="tdInput" > 
      <div class="input-group">` ... like this input fields for all years needed ,say 10 years. 

내가이 실행 시간을 얻을 예외 :

[InvalidPropertyException: Invalid property 'years[1]' of bean class [models.FinStatementInput]: Invalid array index in property path 'years[5]'; nested exception is java.lang.**ArrayIndexOutOfBoundsException**] 

코드에서 배열의 크기를 명시 적으로 지정하지 않았습니다. 그냥 빈 배열을 선언하고 사용자 양식에서 여러 해를 구속하려고합니다. 1에서 10까지 모든 배열 요소에 액세스하려고합니다.하지만 ArryOutOfBound Exception으로 연결되는 실수가 어디인지는 알 수 없습니다. 어떤 도움을 주셔서 감사합니다.

+0

수정 해 주셔서 감사합니다. Jan Doggen :) – user3366706

답변

0

실수는 모델 클래스의 배열 선언에 있습니다. 아래 라인 에서처럼 수정하면 문제가 없습니다. 배열은 선언되었지만 인스턴스화되지 않았습니다. #silly 실수.

public int[] years = new int[10];