【问题标题】:Submit checkbox values Spring MVC提交复选框值 Spring MVC
【发布时间】:2014-08-27 04:19:48
【问题描述】:

也许这个问题被问了很多,但方式不同,所以这是我的问题,我希望有人能提供帮助。

在从数据库中检索了一些研究行并将结果返回到 List 后,我已将其绑定到我的 Spring MVC Controller Model

if(!result.hasErrors())
{
  try{
    List<Question> questionlist = questionservice.findByCategoryAndLevel(questionform.getCategory(),questionform.getLevel());
    model.addAttribute("questionlist",questionlist);
    return "addExam";
  }catch(NullPointerException e)
  {
    return "redirect:/admin/addexam";
  }
}

这是我的看法:

<form:form action="addexam" method="POST" modelAttribute="questionlist">
  <table class="table table-striped table-bordered table-hover" id="sample_1">
    <thead>
      <tr>
        <th class="table-checkbox">
          <input type="checkbox" class="group-checkable" data-set="#sample_1 .checkboxes"/>
        </th>
        <th>
          Category
        </th>
        <th>
          level
        </th>
        <th>
          Type of question
        </th>
        <th>
          Status
        </th>
        <th>
          &nbsp;
        </th>
      </tr>
    </thead>
    <tbody>
      <c:forEach items="${questionlist}" var="question">
        <c:choose>
          <c:when test="${question.isVisible()}">
            <tr class="odd gradeX">
              <td>
                <input type="checkbox"  class="checkboxes" />
              </td>
              <td>
                ${question.category.getCategoryName() }
              </td>
              <td>
                ${question.level }
              </td>
              <c:choose>
                <c:when test="${question.isSingleChoiceQuestion() }">
                  <td>Question à choix unique</td>
                </c:when>
                <c:otherwise>
                  <td>Question à choix mutiple</td>
                </c:otherwise>
              </c:choose>
              <td class="center">
                <c:choose>
                  <c:when test="${question.getState() == 'Desactivated'}">
                    <span class="label label-sm label-default"> Desactivated </span>
                  </c:when>
                  <c:when test="${question.getState() == 'Activated'}">
                    <span class="label label-sm label-success"> Activated </span>
                  </c:when>
                  <c:when test="${question.getState() == 'Pending'}">
                    <span class="label label-sm label-warning"> Pending </span>
                  </c:when>
                </c:choose>
              </td>
              <td>
                <a href="${pageContext.request.contextPath }/admin/question?view=${question.idQuestion}" class="btn btn-xs btn-default" target="_blank">View</a>
              </td>
            </tr>
          </c:when>
        </c:choose>
      </c:forEach>
    </tbody>
  </table>
</form:form>

现在如何提交所选项目?

【问题讨论】:

  • 您能否详细说明您的问题,您期待什么?到目前为止,您为提交表单做了什么?您还应该提供控制器方法签名以检查绑定。

标签: forms jakarta-ee spring-mvc checkbox


【解决方案1】:

是的,我想显示问题列表,然后通过复选框选择我需要的问题

我创建了一个表单,它包含 Idquestions 列表

public class checkedquestion {

    private List<Long> listIdQuestion ;

    //getter & setter
   }

然后在复选框中添加一个路径属性,如下所示:

<form:form action="submit" method="POST" modelAttribute="checkedquestion">  

// .......

     <td>
        <form:checkbox  value="${question.idQuestion}" path="listIdQuestion"/>
     </td>

通过提交我需要的 id 列表:

public String add(@ModelAttribute ("checkedquestion") @Valid CheckedQuestion checkedquestion , BindingResult result )
    {
        if(!result.hasErrors())
         {
                List<Long> list = checkedquestion.getListIdQuestion();
                List<Question> questionlist = questionservice.getQuestion(list);
         }
     }

好像还不错

【讨论】:

    【解决方案2】:

    给一个 id/name 来输入下面的复选框类型

    <input type="checkbox"  class="checkboxes" id="someId" name="someId" value="uniqueValueToEachCheckBox"/>
    

    然后在提交表单后,您可以访问控制器中选中的复选框值,如下所示。

    request.getParameterValues("someId");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-14
      • 2014-06-26
      • 2012-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-06
      • 2015-01-04
      相关资源
      最近更新 更多