jijesh Aj
jijesh Aj

Reputation: 614

How to get checked checkbox value from html page to spring mvc controller

im using spring mvc framework with thymeleaf template engine the problem is , i have 1 page with multiple check box iterated sing thymeleaf th:each iterator.When i clicked multiple check boxes i want to pass check box values to the controller method..

html content

<table> 
<tr th:each="q : ${questions}">
 <h3 th:text="${q.questionPattern.questionPattern}"></h3> 
<div>
 <p >
 <input type="checkbox" class="ads_Checkbox" th:text="${q.questionName}" th:value="${q.id}" name="id"/>
 </p>
 </div>
 </tr>
 </table> 

*Controller*

 @RequestMapping(value = Array("/saveAssessment"), params = Array({ "save" }))
  def save(@RequestParam set: String, id:Long): String = {
  var userAccount: UserAccount = secService.getLoggedUserAccount
    println(userAccount)
    var questionSetQuestion:QuestionSetQuestion=new QuestionSetQuestion
        var questionSet: QuestionSet = new QuestionSet
    questionSet.setUser(userAccount)
    questionSet.setSetName(set)
    questionSet.setCreatedDate(new java.sql.Date(new java.util.Date().getTime))
   questionSetService.addQuestionSet(questionSet)
     var list2: List[Question] = questionService.findAllQuestion
    var limit=list2.size
     var qustn:Question=null
    var a = 1;
     for( a <- 1 to limit ){
         println(  a  );
      qustn=  questionService.findQuestionById(a)
     questionSetQuestion.setQuestion(qustn)
    questionSetQuestion.setQuestionSet(questionSet)
    questionSetQuestion.setCreatedDate(new java.sql.Date(new java.util.Date().getTime))

    questionSetQuestionService.addQuestionSetQuestion(questionSetQuestion) } "redirect:/teacher/Assessment.html" }

Upvotes: 3

Views: 16143

Answers (2)

Usha
Usha

Reputation: 1468

I have used JSTL with JSP and thymeleaf was something new. I read the THYMELEAF documentation.

There is a section which explains multi valued check boxes.

<input type="checkbox" 
     class="ads_Checkbox" 
     th:text="${q.questionName}" 
     th:value="${q.id}" name="id"/>

In the above code we are not binding the value to the field of the command object. Instead try doing this

<input type="checkbox" 
     class="ads_Checkbox" 
     th:text="${q.questionName}" 
     th:field="*{selectedQuestions}" 
     th:value="${q.id}" />

here the selectedQuestions is an array object present in the spring command object.

Upvotes: 3

hubbardr
hubbardr

Reputation: 3173

I think you pretty much have it. With a checkbox, you can only send one piece of information back with the form...that being the value. So if you are trying to determine which checkboxes are checked when the user clicks the submit button, then I would have the checkboxes all use one name...like "id" (exactly like you have). Value is the actual id of the question (again like you have). Once submitted, "id" will be a String array which includes all the values of the checkboxes that were checked.

So your controller method needs to take param called "ids" mapped to parameter "id" which is a string[]. Now for each id, you can call questionService.findQuestionById.

(I'm not a Groovy guru so no code example sry :)

Upvotes: 4

Related Questions