3

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" }
3
  • 1
    <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> Commented Jun 3, 2013 at 7:05
  • 1
    Well, that's nice. But if you don't add the code that shows us how you've tried it, we can not help you. You must ask a specific question to get a usable answer. Commented Jun 3, 2013 at 7:06
  • 1
    i just updated my question.is that ok??? Commented Jun 3, 2013 at 9:58

2 Answers 2

4

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 :)

Sign up to request clarification or add additional context in comments.

2 Comments

thnx bt it is not groovy ..it is scala
Ah. Oops. I need to get out more. :)
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.