geo*_*geo 35 annotations spring-mvc
假设我们有一个实体Person,一个控制器PersonController和一个edit.jsp页面(创建一个新的或编辑一个现有的人)
调节器
@RequestMapping(value = "/edit", method = RequestMethod.POST)
public String editPerson(@RequestParam("fname") String fname, Model model) {
if(fname == null || fname.length() == 0){
model.addAttribute("personToEditOrCreate", new Person());
}
else{
Person p = personService.getPersonByFirstName(fname);
model.addAttribute("personToEditOrCreate", p);
}
return "persons/edit";
}
@RequestMapping(value = "/save", method = RequestMethod.POST)
public String savePerson(Person person, BindingResult result) {
personService.savePerson(person);
return "redirect:/home";
}
Run Code Online (Sandbox Code Playgroud)
文件edit.jsp
<form:form method="post" modelAttribute="personToEditOrCreate" action="save">
<form:hidden path="id"/>
<table>
<tr>
<td><form:label path="firstName">First Name</form:label></td>
<td><form:input path="firstName" /></td>
</tr>
<tr>
<td><form:label path="lastName">Last Name</form:label></td>
<td><form:input path="lastName" /></td>
</tr>
<tr>
<td><form:label path="money">Money</form:label></td>
<td><form:input path="money" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Add/Edit Person"/>
</td>
</tr>
</table>
</form:form>
Run Code Online (Sandbox Code Playgroud)
我尝试上面的代码(不使用savePerson方法中的@ModelAttribute注释,它工作正常.为什么以及何时需要将注释添加到person对象:
@RequestMapping(value = "/save", method = RequestMethod.POST)
public String savePerson(@ModelAttribute("personToEditOrCreate") Person person, BindingResult result) {
personService.savePerson(person);
return "redirect:/home";
}
Run Code Online (Sandbox Code Playgroud)
Nei*_*gan 56
您不需要@ModelAttribute(参数)只使用Bean作为参数
例如,这些处理程序方法适用于这些请求:
@RequestMapping("/a")
void pathA(SomeBean someBean) {
assertEquals("neil", someBean.getName());
}
GET /a?name=neil
@RequestMapping(value="/a", method=RequestMethod.POST)
void pathAPost(SomeBean someBean) {
assertEquals("neil", someBean.getName());
}
POST /a
name=neil
Run Code Online (Sandbox Code Playgroud)
使用@ModelAttribute(方法)在每个请求中将默认数据加载到模型中 - 例如从数据库中加载,尤其是在使用时@SessionAttributes.这可以在一个Controller或一个中完成ControllerAdvice:
@Controller
@RequestMapping("/foos")
public class FooController {
@ModelAttribute("foo")
String getFoo() {
return "bar"; // set modelMap["foo"] = "bar" on every request
}
}
Run Code Online (Sandbox Code Playgroud)
转发到的任何JSP FooController:
${foo} //=bar
Run Code Online (Sandbox Code Playgroud)
要么
@ControllerAdvice
public class MyGlobalData {
@ModelAttribute("foo")
String getFoo() {
return "bar"; // set modelMap["foo"] = "bar" on every request
}
}
Run Code Online (Sandbox Code Playgroud)
任何JSP:
${foo} //=bar
Run Code Online (Sandbox Code Playgroud)
如果要将(方法)的结果用作默认值,请使用@ModelAttribute(参数):@ModelAttribute
@ModelAttribute("attrib1")
SomeBean getSomeBean() {
return new SomeBean("neil"); // set modelMap["attrib1"] = SomeBean("neil") on every request
}
@RequestMapping("/a")
void pathA(@ModelAttribute("attrib1") SomeBean someBean) {
assertEquals("neil", someBean.getName());
}
GET /a
Run Code Online (Sandbox Code Playgroud)
使用@ModelAttribute(参数)获取存储在flash属性中的对象:
@RequestMapping("/a")
String pathA(RedirectAttributes redirectAttributes) {
redirectAttributes.addFlashAttribute("attrib1", new SomeBean("from flash"));
return "redirect:/b";
}
@RequestMapping("/b")
void pathB(@ModelAttribute("attrib1") SomeBean someBean) {
assertEquals("from flash", someBean.getName());
}
GET /a
Run Code Online (Sandbox Code Playgroud)
使用@ModelAttribute(参数)获取存储的对象@SessionAttributes
@Controller
@SessionAttributes("attrib1")
public class Controller1 {
@RequestMapping("/a")
void pathA(Model model) {
model.addAttribute("attrib1", new SomeBean("neil")); //this ends up in session due to @SessionAttributes on controller
}
@RequestMapping("/b")
void pathB(@ModelAttribute("attrib1") SomeBean someBean) {
assertEquals("neil", someBean.getName());
}
}
GET /a
GET /b
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
75709 次 |
| 最近记录: |