2015-01-30 7 views
0

저는 며칠 동안 플레이 형식으로 작업 해 왔으며 극도로 좌절했습니다. 나는 문서를 통해 가서 "재생 자바"책을 사용하고, userWeights 모델 클래스를 생성 :재생 프레임 워크의 입력 텍스트 값은 항상 0을 전달합니다.

public class UserWeights { 
     public HashMap<ServicesWeights, Float> userWeights = new HashMap<>(); 
     @Constraints.Required 
     public String user; 
     @Constraints.Required 
     public String password; 
     public int sampleSize; 
     public int misSampleSize; 
} 

내 컨트롤러 :

public class Application extends Controller { 

    private static final Form<UserWeights> userWeightsForm = Form.form(UserWeights.class); 

    public static Result index() { 
     return ok(index.render("Your new application is ready.")); 
    } 

    public static Result finder() { 

     return ok(finder.render(userWeightsForm)); 
    } 

    public static Result runWithUserInput() { 
     Form<UserWeights> boundForm = userWeightsForm.bindFromRequest(); 

     if (boundForm.hasErrors()) { 
      return badRequest(index.render("FAIL")); 
     } 
     UserWeights weights = boundForm.get(); 

     if (weights.checkboxChoices.get(0) != null && weights.checkboxChoices.get(0).equals("1")) { 
      runMethodA(); 
     } else if (weights.checkboxChoices.get(1) != null && weights.checkboxChoices.get(1).equals("2")) { 
      runMethodB(); 
     } 

     return TODO; 
    } 

그리고보기 :

@(UserWeightsForm: Form[UserWeights]) 
@import helper._ 
@import helper.twitterBootstrap._ 

@main("finder") { 
<h1>Please fill the required fields</h1> 
@helper.form(action = routes.Application.runWithUserInput(), 'enctype -> "multipart/form-data") { 
<fieldset> 
    <legend>Finder</legend> 
    @helper.inputText(UserWeightsForm("user"),'_label -> "User") 
    @helper.inputPassword(UserWeightsForm("password"), '_label -> "Password") 
    <br/> 
     <label for="checkboxInput">Input Type:</label><br/> 

     <span class="buttonSet" id="checkboxInput"> 
     <input type = "checkbox" id = "genericCheckbox" name = 'checkboxChoices[0]' value = "1"> 
     <label for = "genericCheckbox">Generic Sample</label> 
     <input type = "number" name = 'UserWeightsForm("sampleSize")' id = "genericInput" value = "@UserWeightsForm("sampleSize").value"><br/> 
     <input type = "checkbox" id = "misCheckbox" name = 'checkboxChoices[1]' value = "2"> 
     <label for = "misCheckbox">MisSample</label> 
     <input type = "number" name = 'UserWeightsForm("misSampleSize")' id = "misInput" value = "@UserWeightsForm("misSampleSize").value"><br/> 
     </span> 

내가 원하는 것 - 첫 번째 체크 박스가 선택되면 사용자는 sampleSize 텍스트 입력 필드에 값을 입력하고 해당 값은 폼에 바인딩되며 misSampleSize 필드는 bla가됩니다. nk/zero/whatever (이 경우에는 어쨌든 사용하지 않습니다). 그 반대.

문제 - 확인란을 선택하고 sample 텍스트 입력을 채울 때 값 0을 양식에 바인딩합니다.

input 태그의 value을 완전히 null로 설정하고 템플릿 도우미를 사용하여 설정하려고 시도했습니다. 템플릿 도우미를 사용합니다 (유연하지 않기 때문에 선호하지 않습니다). 텍스트 필드에 입력 한 숫자가 무시되는 이유를 이해할 수없고 프로그램에서 0으로 간주합니다.

그래서 질문 1 : 0 값이 채워지는 것을 어떻게 중지합니까?

2 어떻게 텍스트 필드 (sampleSize 또는 misSampleSize) 중 하나의 값을 보내고, 양식 오류를받지 않고, 다른 하나의 빈 남길 수 있습니다 질문?

감사합니다.

답변

1

Java가 아닌 Scala에서 작업합니다. 하지만 당신은 입력 필드와 이름 필드가 폼 필드 이름과 일치해야한다고 생각합니다. 그것은 그들을 묶는 데 사용합니다. 당신은 당신의 UserWeightsForm 표시되지 않습니다,하지만 당신은 같은 것을 시도해 볼 수도 있습니다 : 나는 또한 경우 값에 "getOrElse"를 사용

<input type="number" name='sampleSize' id="sampleSize" value="@UserWeightsForm("sampleSize").value.getOrElse("0")"><br/> 
    <input type="number" name='misSampleSize' id="misSampleSize" value="@UserWeightsForm("misSampleSize").value.getOrElse("0")"><br/> 

을 (오류가 아마도) 값이 없습니다.

+0

Java에서 입력 이름은 양식 필드 이름과 일치해야합니다. 그래서 그것은'name = "sampleSize"' – Sivakumar

+0

이어야합니다. @Sivakumar – wwkudu

+0

답해 주셔서 감사합니다.하지만 작동하지 않습니다. getOrElse()로 컴파일되지 않으며, 제안한 것처럼 필드 이름 만 넣으면 런타임 예외가 발생합니다 - [IllegalStateException : No value]. – Ozilophile