Spring DataBinding
2015-08-06 11:26
429 查看
1. 用@ModelAttribute注释方法
1.1
@ModelAttribute和@RequestMapping同时注释一个方法
这个方法的返回值不是视图名称,而是model属性的值。视图名称由RequestToViewNameTranslator根据请求转换为逻辑视图。
Model属性名称有@ModelAttribute(value="")指定,相当于在request中封装了key="account" , value=account。
2. 用@ModelAttribute注释方法参数
BookSearchCriteria criteria注释方法参数,参数criteria的值来源于criteria()方法中的model属性。
1.1
@ModelAttribute和@RequestMapping同时注释一个方法
<span style="font-size:14px;"> @RequestMapping(method = RequestMethod.GET) @ModelAttribute public Account register(Locale currentLocale) { Account account = new Account(); account.getAddress().setCountry(currentLocale.getCountry()); return account; }</span>
这个方法的返回值不是视图名称,而是model属性的值。视图名称由RequestToViewNameTranslator根据请求转换为逻辑视图。
Model属性名称有@ModelAttribute(value="")指定,相当于在request中封装了key="account" , value=account。
2. 用@ModelAttribute注释方法参数
@ModelAttribute public BookSearchCriteria criteria() { return new BookSearchCriteria(); } @ModelAttribute("categories") public List<Category> getCategories() { return this.bookstoreService.findAllCategories(); } /** * This method searches our database for books based on the given {@link BookSearchCriteria}. * Only books matching the criteria are returned. * * @param criteria the criteria used for searching * @return the found books * * @see com.apress.prospringmvc.bookstore.repository.BookRepository#findBooks(BookSearchCriteria) */ @RequestMapping(value = "/book/search", method = { RequestMethod.GET }) public Collection<Book> list(@ModelAttribute("bookSearchCriteria") BookSearchCriteria criteria) { return this.bookstoreService.findBooks(criteria); }@ModelAttribute("bookSearchCriteria")
BookSearchCriteria criteria注释方法参数,参数criteria的值来源于criteria()方法中的model属性。
相关文章推荐
- javafx
- java 资料收集
- java 资料收集
- Eclipse调试:F5、F6、F7、F8
- Spring MVC+分布式事务+动态数据源配置
- 转:myeclipse 上安装 Maven3 插件
- Java 并发专题 : Timer的缺陷 用ScheduledExecutorService替代
- springMVC学习笔记---静态文件的访问
- Birt报表集成MyEclipse Web Project
- java.io.FileNotFoundException:open failed: EACCES (Permission denied)
- java托盘开发界面记录
- MyEclipse中安装Testng插件
- java在src/test/resourse下读取properties文件
- Spring+Quartz实现定时任务的配置方法
- SpringMVC原理
- java异常
- eclipse启动出现“An Error has Occurred. See the log file”解决方法
- java中使用assert
- java类的访问权限
- ubuntu下的jdk安装