我正在阅读 Spring in action,现在我正在阅读有关构建 Web 应用程序的章节,但我无法完全掌握一件事
例如,如果我们有这样的方法:
@RequestMapping(method=RequestMethod.GET, params="new")
public String createSpitterProfile(Model model) {
model.addAttribute(new Spitter());
return "spitters/edit";
}
这里我们将类的实例添加Spitter
到模型中,所以它被传递给这个表单
<div>
<h2>Create a free Spitter account</h2>
<sf:form method="POST" modelAttribute="spitter"
enctype="multipart/form-data">
<fieldset>
<table cellspacing="0">
<tr>
<th><sf:label path="fullName">Full name:</sf:label></th>
<td><sf:input path="fullName" size="15" /><br/>
<sf:errors path="fullName" cssClass="error" />
</td>
</tr>
<tr>
<th><sf:label path="username">Username:</sf:label></th>
<td><sf:input path="username" size="15" maxlength="15" />
<small id="username_msg">No spaces, please.</small><br/>
<sf:errors path="username" cssClass="error" />
</td>
</tr>
<tr>
<th><sf:label path="password">Password:</sf:label></th>
<td><sf:password path="password" size="30"
showPassword="true"/>
<small>6 characters or more (be tricky!)</small><br/>
<sf:errors path="password" cssClass="error" />
</td>
</tr>
<tr>
<th><sf:label path="email">Email Address:</sf:label></th>
<td><sf:input path="email" size="30"/>
<small>In case you forget something</small><br/>
<sf:errors path="email" cssClass="error" />
</td>
</tr>
<!--<start id="image_field"/>-->
<tr>
<th><label for="image">Profile image:</label></th>
<td><input name="image" type="file"/>
</tr>
<!--<end id="image_field"/>-->
<tr>
<th></th>
<td>
<sf:checkbox path="updateByEmail"/>
<sf:label path="updateByEmail"
>Send me email updates!</sf:label>
</td>
</tr>
<tr>
<th></th>
<td><input name="commit" type="submit"
value="I accept. Create my account." /></td>
</tr>
</table>
</fieldset>
</sf:form>
</div>
用户输入数据后,这些输入数据将绑定到某个Spitter
对象并由
@RequestMapping(method=RequestMethod.POST)
public String addSpitterFromForm(@Valid Spitter spitter,
BindingResult bindingResult,
@RequestParam(value="image", required=false) MultipartFile image) {
if(bindingResult.hasErrors()) {
return "spitters/edit";
}
spitterService.saveSpitter(spitter);
try {
if(!image.isEmpty()) {
validateImage(image);
saveImage(spitter.getId() + ".jpg", image);
}
} catch (ImageUploadException e) {
bindingResult.reject(e.getMessage());
return "spitters/edit";
}
return "redirect:/spitters/" + spitter.getUsername();
}
我将一些对象传递给将要填充的模型,但我很感兴趣这在幕后是如何工作的,因为当页面提供给用户时,有关对象的信息会丢失(或者我错了)?
1.添加的对象是否与请求的数据来时Spring动态创建的第二个对象createSpitterProfile
相同(相同的含义引用) ?Spitter
addSpitterFromForm
2.当用户输入进来的请求中addSpitterFromForm
,Spring如何知道表单中的信息应该被处理到Spitter
对象,我知道Spitter
方法签名中有对象,但是Spring如何知道数据应填充此对象属性。