【问题标题】:Handling dynamic form处理动态表单
【发布时间】:2015-12-09 07:36:40
【问题描述】:

我正在尝试处理动态弹簧形式。关键是,在运行之前我不确切知道表单有多少输入。我不知道可以与@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,没有任何来自表单的信息。如果有其他方法可以解决这个问题,我将不胜感激,指出我正确的方向。

【问题讨论】:

  • 你的问题到底是什么?
  • @AlanHay 如何从此表单中获取结果。

标签: jsp spring-mvc jstl


【解决方案1】:

目前尚不清楚您要问什么,但是如果是围绕绑定集合,您将需要进行以下更改。

  1. 答案需要存储在列表而不是集合中。即 qaForm.getAnswers() 必须返回一个 List,因为 Spring 只能绑定到可通过索引访问的集合。

  2. 更改 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. 在表单中添加一个隐藏字段,用于提交调查 ID。

  2. 向您的控制器添加如下方法,该方法将加载指定的调查并将提交的数据绑定到此现有实例。

.

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

【讨论】:

  • 谢谢,现在我遇到了另一个问题,当我返回我的调查表单时,答案对象不包含问题、用户,只有 answerText 字段可用。这是正常行为吗?以及如何从我的jsp中获取这个对象?
  • 您需要填充现有调查。春天将创造一个新的。查看更新。
  • 可能是因为表单提交url不包含路径变量。
  • 如果从 getSurveyForm() 中删除 @PathVariable int id,一切正常,但是我无法访问选择的调查。是因为缺少路径变量吗?如果是,我应该在哪里添加它?
  • 好的,将您的 GET 代码恢复到之前的状态,即通过路径变量查找。然后查看更新的答案。
【解决方案2】:

你可以这样做:

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

但是您应该在页面 jsp 的表单中包含此参数,例如:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-16
    • 2016-06-27
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 2013-10-08
    • 1970-01-01
    相关资源
    最近更新 更多