Reputation: 73
I have a form bean as follows
public class QuestionPageBean
{
private String statement;
private String options;
// getters and setters
}
Below is the GET request handler which creates an object 'question' and sets its field 'statement'. It also populates a list of values to be shown as check-box for 'options' field. The page displays 'statement' and a list of check-boxes.
@RequestMapping(value = "/question", method = RequestMethod.GET)
public ModelAndView initForm()
{
logger.debug("Received request to show question page");
List<String> optionList = new ArrayList<String>();
ModelAndView model=new ModelAndView("question");
QuestionPageBean question=new QuestionPageBean();
question.setStatement("This is the question");
optionList.add("option1");
optionList.add("option2");
optionList.add("option3");
optionList.add("option4");
model.addObject("optionList", optionList);
model.addObject("command", question);
return model;
}
Below is the POST method handler for the page where user selects the value of 'options' field from the check boxes and submits the form. The problem is that ModelAttribute 'question' gets the value for 'options' but 'statement' remains null.
How can I get the value of 'statement' field (which is set when page loads through GET method above) in below controller. I there any spring mvc construct?
@RequestMapping(value = "/question", method = RequestMethod.POST)
public void questionFormSubmit(@ModelAttribute("question")QuestionPageBean question)
{
logger.debug("Question page subimtted");
System.out.println(question.getStatement()); // prints null
System.out.println(question.getOptions()); // prints correct value selected by user
}
Upvotes: 1
Views: 689
Reputation: 1539
Statement field will not be populated unless it is passed from form. Create hidden field for statement inside form and then it should be populated.
Upvotes: 2