提交复选框值Spring MVC

时间:2014-07-06 16:13:20

标签: forms java-ee spring-mvc checkbox

也许这个问题被问了很多,但问题不一样,所以这是我的问题,我希望有人可以提供帮助。

从数据库中检索了一些研究行并将结果返回到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>

现在我该如何提交所选项目?

2 个答案:

答案 0 :(得分: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);
         }
     }

似乎工作正常

答案 1 :(得分:0)

输入ID /名称以输入复选框类型,如下所示

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

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

request.getParameterValues("someId");