Spring 在同一控制器方法中处理同一类型的多个ModelAttribute
我有以下情况:Spring 在同一控制器方法中处理同一类型的多个ModelAttribute,spring,spring-mvc,modelattribute,Spring,Spring Mvc,Modelattribute,我有以下情况: @ModelAttribute("persons") public void addAttributes(Model model) { Person person = new Person() ; person.setAge(26); person.setFirstName("mars"); model.addAttribute("persons", person);
@ModelAttribute("persons")
public void addAttributes(Model model) {
Person person = new Person()
;
person.setAge(26);
person.setFirstName("mars");
model.addAttribute("persons", person);
}
@RequestMapping(value="/process-person")
public ModelAndView processPerson(@ModelAttribute Person person,@ModelAttribute ("persons")Person persons,ModelAndView modelAndView ) {
//
//ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("person-result-page");
modelAndView.getModel().remove("personObj");
modelAndView.addObject("pers", person);
----> modelAndView.addObject("pers2", persons);// persons holds the myname1 provided in input box
modelAndView.addObject("personObj", person);
return modelAndView;
}
如-->中所示,我希望此变量-persons保存从addAttributes()方法获得的值,但它采用的值与我从jsp页面输入的值相同:
<form:form method="POST" commandName="person-entity" action="process-person.html">
<table>
<tr>
<td><form:label path="firstName">Name:</form:label></td>
<td><form:input path="firstName" /></td>
</tr>
<tr>
<td><form:label path="age">Age:</form:label></td>
<td><form:input path="age" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Submit"/>
</td>
<td></td>
<td></td>
</tr>
</table>
</form:form>
<br/>
姓名:
年龄:
我看到了一些类似的问题,但没有一个解决了这个问题。在这里,我希望addAttribute方法中的值放在“persons”对象中,但它的值与form.jsp中提供的“person”的值相同
请帮帮我您什么时候遇到这个问题?我想当你按下提交按钮的时候?或者,当您只是向/process person执行get请求时,您是否遇到了麻烦?您是否查看了?我在单击指向“/process person”url的超链接时遇到了此问题。我已尝试重现您的问题。但是当我HTTP访问/process person时,那么
@modeldattribute person
和@modeldattribute(“persons”)person
是完全不同的实例。后者来自您的@ModelAttribute方法,因此我仍然不知道您的问题所在。这正是我所期望的,。。两个不同的实例,但我在两个参数中得到了相同的实例。