Dynamic form and data binding with Spring MVC

后端 未结 2 1572
予麋鹿
予麋鹿 2021-02-04 08:44

In my Spring MVC application I need to implement a dynamic questionnaire form: I have N questions and for each I have 3 options.

So in my page I\'ll have something like

2条回答
  •  借酒劲吻你
    2021-02-04 09:25

    This class is my model attribute:

    public class Questionnaire {
        private List questions = new ArrayList<>();
        private List answers = new ArrayList<>();
    
        // set + get
    }
    

    And:

    public class Question {
        private int id;
        private String text;
    
        // set+ get
    
    }
    
    public class Answer {
        private int questionId;
        private int value;
    
        // set + get
    }
    

    I populate questions list before I put it into model.

    In my page I use this code:

    
        
    ${question.text}

    Posting this form I get a fully populated questionnaire instance in my controller.

    NOTE I found this post very helpful to solve my problem.

提交回复
热议问题