1

I'm trying to display list of questions on JSP page and using the check-box to select and submit them back to controller.
I can display them without any problem but when they're Posted back 'QuestionsListWrapper' is Null. Can someone point to me where I'm going wrong.

Entity Question

    public class Questions implements Serializable {
    
    @Id
    @GeneratedValue
    private Integer quesId;
    
    @Transient
    private boolean isSelected;
    

Wrapper class

    public class QuestionsListWrapper {

        private List<Questions> quesWrapperList;

        public List<Questions> getQuesWrapperList() {
            return quesWrapperList;
        }

        public void setQuesWrapperList(List<Questions> quesWrapperList) {
            this.quesWrapperList = quesWrapperList;
        }

Controller Get

    @GetMapping("/group/{id}")
    public String showForm(@PathVariable("id") Integer id, Model model) {

        Assessments ass = as.getAssById(id);        
        List<Questions> qlist = qs.getQuesByAssessment(ass);
        
        QuestionsListWrapper qlw = new QuestionsListWrapper();
        qlw.setQuesWrapperList(qlist);
        
        model.addAttribute("questions", qlw.getQuesWrapperList());
        return "stuEssay";
        

Controller Post

        @PostMapping("/saveSelectedQuestions")

//Here questions is null

        public String saveSelectedQuestions(@ModelAttribute("questions") QuestionsListWrapper questions, Model model) {
        
        List<Questions> selected = questions.getQuesWrapperList();
        
        System.out.println(questions.toString());
        System.out.println(questions.getQuesWrapperList());
        
        return "redirect:/studentHome";
        

JSP

      <form:form action="/saveSelectedQuestions" method="post" modelAttribute="questions">

            <c:forEach items="${questions}" var="question" varStatus="count">
            
                <input type="hidden" name="quesId" value="${question.quesId}">
                
                    <div class="form-group">
                        
                        <textarea rows="3" >${question.quesText}</textarea>
                        <input type="checkbox" name="isSelected[${count.count}]"/>
                    </div>
            
            </c:forEach><!-- End of question list -->
            
            <div class="modal-footer">
                <button type="submit" class="btn btn-primary" >Submit</button>
            </div>
        </form:form>    
Tony
  • 529
  • 2
  • 6
  • 24
  • 1
    I think there is problem in your input names that's the reason values are not getting to controller .Check [this](https://stackoverflow.com/questions/15480397/how-to-send-list-of-objects-to-view-and-back-to-post-method-in-controller) might help. – Swati Jul 30 '20 at 06:19

0 Answers0