2015-12-09 30 views
1

我正在嘗試處理動態彈簧表單。重點是,在運行之前我不知道有多少輸入形式。我不知道可以與@RequestParam一起使用的輸入名稱或任何類型的信息。處理動態表格

這裏是我的控制器:

@RequestMapping(value = "/surveys/{id}", method = RequestMethod.GET) 
public String survey(@PathVariable int id, ModelMap model) { 
    model.addAttribute("surveyForm", getQAForm(id)); 
    return "user/survey"; 
} 

@RequestMapping(value = "/submitSurvey", method = RequestMethod.POST) 
public String submitSurvey(@ModelAttribute("surveyForm") QAForm qaForm, ModelMap modelMap){ 
    Set<Answer> answers = qaForm.getAnswers(); 
    modelMap.addAttribute("answers", answers); 
    return "test"; 
} 

和JSP的:

 <f:form method="post" modelAttribute="surveyForm" action="/submitSurvey">> 
     <h2>${surveyForm.survey.title}</h2> 
     <h5>${surveyForm.survey.description}</h5> 
     <c:forEach items="${surveyForm.answers}" var="answer"> 
      <div class="panel panel-default"> 
      <div class="panel-heading"> 
       ${answer.question.text} 
      </div> 
      <div class="panel-body"> 
       <f:input path="${answer.answerText}" type="text" /> 
      </div> 
      </div> 
     </c:forEach> 
     <input class="btn btn-default" type="submit" value="Submit"/> 
     <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/> 
     </f:form> 

處理後/ submitSurvey要求其簡單的重定向,而不從形式的任何信息test.jsp的。如果有什麼辦法可以解決這個問題,我會感激我的一個正確方向。

+0

究竟什麼是你的問題? –

+0

@AlanHay如何從此表單中獲取結果。 – irisk

回答

1

目前還不清楚但是如果你問什麼它圍繞一個集合進行綁定,您將需要進行以下更改。

  1. 答案將需要存儲在列表中,而不是集合。即qaForm.getAnswers()必須返回一個List,因爲Spring只能綁定到索引可訪問的集合。

  • 更改JSP標記來按如下方式使用索引屬性。

    <c:forEach items="${surveyForm.answers}" var="answer" varStatus="status"> 
         <div class="panel panel-default"> 
          <div class="panel-heading"> 
           ${answer.question.text} 
          </div> 
          <div class="panel-body"> 
           <f:input path="answer[${status.index}].answerText" type="text" /> 
          </div> 
         </div> 
    </c:forEach> 
    

    以填充提交進行以下修改(按http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-modelattrib-method-args)現有的調查

    1. 添加在您的形式提交surveyId隱藏字段。

    2. 到控制器添加方法如下,將加載指定的調查和提交數據綁定到現有實例。

    @ModelAttribute("surveyForm") 
    public SurveyForm getSurveyForm(@RequestParam(required = false) Integer surveyId){ 
        if(id != null){ 
         //load the form required by id 
        } 
    } 
    
  • +0

    謝謝,現在我又遇到了另一個問題,當我返回我的surveyForm時,Answer對象不包含Question,User,只有answerText字段可用。這是正常的行爲嗎?以及如何從我的jsp獲取這個對象? – irisk

    +0

    您需要填寫現有調查。春天將創造一個新的。查看更新。 –

    +0

    現在有400個狀態錯誤, – irisk

    0

    你可以做財產以後這樣的:

    @RequestMapping(value = "/surveys/{id}", method = RequestMethod.GET) 
        public String survey(@PathVariable int id, ModelMap model,@RequestParam("description") String desc) { 
        // here you can handl the param description 
        model.addAttribute("surveyForm", getQAForm(id)); 
        return "user/survey"; 
    } 
    

    但是你應該有這個PARAM在你的形式在頁面中的JSP,如:

    <f:form method="post" modelAttribute="surveyForm" action="/submitSurvey">> 
    <input type="text" name="description" /> 
    ....