0

在我的控制器中,我有以下功能

@ModelAttribute("ABeanCV")
public AClass getCommand_Class()
{
AClass ABean=new AClass();
ABean.setQuestions(new AutoPopulatingList(ABean.class));
return ABean;
}

@RequestMapping(method = RequestMethod.GET, value="/AFormCV.htm")
protected String showNameForm(@ModelAttribute("ABeanCV") AClass command_Class,ModelMap model)
{
command_Class.getQuestions().get(52).setAns("123456789");
command_Class.getQuestions().get(52).setPrevAns("987654321");
model.addAttribute("Question",as);
command_Class.getQuestions().get(52).setCurrAns("435678543");
model.addAttribute("Question",as);
return "ACV";
}

@RequestMapping(method = RequestMethod.POST, value="/submitQuestionForm.htm")
protected String submitNameForm(@ModelAttribute("ABeanCV") AClass commandClassCV, ModelMap model )
{
System.out.println("Ans wer to q 52 : "+commandClassCV.getQuestions().get(52).getAns());
System.out.println("PREv Ans wer to q 52 : "+commandClassCV.getQuestions().get(52).getPrevAns());
System.out.println("PREv Ans wer to q 52 : "+commandClassCV.getQuestions().get(52).getCurrAns());
model.addAttribute("Question",as);
return "ACV";
}

我的 JSP

<form:form method="post" name="classForm" id="classForm" modelAttribute="ABeanCV" commandName="ABeanCV" action="submitQuestionForm.htm">

 here I have only 2 input values " ans and prevAns " but in my showNameForm function I am setting values for  " ans , prevAns and currAns " input values 

当我以表单 2 运行项目时,文件显示正常。提交表单后,当我尝试在 submitNameForm 函数中打印 3 个值时,只会打印那些显示在表单上的值,即。“ ans and prevAns ”。在 showNameForm 函数中设置的第三个值“currAns”打印 null。

提交表单时是否会覆盖 modelAttribute?我怎样才能获得“currAns”的价值?

任何帮助将不胜感激。

4

1 回答 1

0

是的。当您提交表单时,首先 ModelAttribute 将被 getCommand_Class() 方法覆盖,然后 AClass 中的 Question 字段将设置为 UI 上显示的值。这是因为控制器中使用 ModelAttribute 注释的所有方法都被执行以初始化 Model 对象。

你可以通过

  • 从 showNameForm 方法中删除 getCommand_Class() 方法和 ModelAttribute AClass。
  • 创建包含所需数据的 AClass 的新对象,并在 ModelMap 中设置 AClass。
  • 返回 ModelAndView 对象,以便 JSP 将获取 Model 对象。
于 2012-06-20T18:47:30.560 回答