java - Dynamic form and data binding with Spring MVC -
In my Spring MVC application I need to implement the form of a dynamic questionnaire: I have questions and I have 3 options for each I
Then in my page I will have something like this:
| Question 1 | 1 | 2 | 3 | | Question 2 | 1 | 2 | 3 | | Question 3 | 1 | 2 | 3 | | ... | 1 | 2 | 3 | | Question N | 1 | 2 | 3 | The questions are stored in the database and for the option I will use the radio button. I use a forEach tag to prepare dynamic rows , But I do not know how to post data in this scenario and how to force ModelAttribute ... which may be a good structure for my model specialty class ? Is it possible to use binding for a dynamic form with Spring MVC?
How to post data < P> You can do this,
I am considering classes like question : public class questions {Private string question; Private map & lt; Integer, option & gt; OptionMap; Private integer selected optic; // getters and setters} like and options class: public class options {personal integer option; Private string option text; // getters and setters} and for binding in the form of a question model class: public class question model {personal Maps & lt; Integer, question & gt; QuestionMap; // getters and setters} and in the Controller class GET handler method, for example, populate queries: @RequestMapping (Method = RequestMethod.GET) Public string index (model model) {option option A = new option (1, "A"); Option Option B = New Option (2, "B"); Option option c = new option (3, "c"); Maps & lt; Integer, option & gt; OptionMap = new hashmap & lt; Integer, option & gt; (); OptionMap.put (optionA.getOptionKey (), optiona); OptionMap.put (optionB.getOptionKey (), optionB); OptionMap.put (optionC.getOptionKey (), optionC); Question Question1 = New Question ("AQ", OptionMap, 1); Question Question2 = New Question ("BQ", OptionMap, 1); Question Question 3 = New Question ("CQ", OptionMap, 1); Maps & lt; Integer, question & gt; Questionmap = new Hashmap & lt; Integer, question & gt; (); QuestionMap.put (1, question 1); QuestionMap.put (2, question 2); QuestionMap.put (3, question 3); Model.addAttribute ("QuoteModel", New QuestionModel (Quiz)); Return "index"; Finally jsp page usage & lt; Form: hidden .. and to present the form elements to keep the old values such as: & lt; C: url value = "/ post posted" var = "postUrl" /> & Lt; Form: Form Action = "$ {postUrl}" modelAttubil = "questionmodel" method = "post" & gt; & Lt; Table & gt; & Lt; TR & gt; & Lt; Th & gt; Question & lt; / Th & gt; & Lt; Th & gt; Option & lt; / Th & gt; & Lt; / TR & gt; & Lt; C: forEach item = "$ {questionsModel.questionMap}" var = "currQue" varStatus = "queIndex" & gt; & Lt; TR & gt; & Lt; TD & gt; & Lt; Form: hidden path = "questionmap [$ {queIndex.count}]. Question" /> & Lt; Label & gt; Question: & lt; / Labels & gt; & Lt; C: out value = "$ {currQue.value.question}" /> gt; & Lt; Br / & gt; & Lt; / TD & gt; & Lt; TD & gt; & Lt; C: forEach item = "$ {currQue.value.optionMap}" var = "opt" varStatus = "optionIndex" & gt; & Lt; Form: hidden path = "questionmap [$ {queIndex.count}]. Optionmap [$ {optionIndex.count}]. OptionText" /> & Lt; Appearance: Hidden path = "questionmap [$ {queIndex.count}]. Optionmap [$ {optionIndex.count}]. OptionKey" /> & Lt; Form: radiobutton path = "question map [$ {queIndex.count}]. SelectedOptionKey" value = "$ {opt.value.optionKey}" label = "$ {opt.value.optionText}" /> & Lt; / C: foreach & gt; & Lt; / TD & gt; & Lt; / TR & gt; & Lt; / C: foreach & gt; & Lt; / Table & gt; & Lt; Input type = "submit" / & gt; & Lt; / Form: Form & gt; You can get binding and model like POST:
@RequestMapping (value = "/ questionPost", method = RequestMethod.POST) public String indices (@model attribute ("questionmodel") questionmodel questionmodel, binding result result) {System.out.println (questionmodel.graph quiz ()); Return "Redirect: /"; }
Comments
Post a Comment