DataBinding在Spring中应用。

第一点:使用ModelAttribute指定带绑定的数据内容

很重要的属性:@ModelAttribute([value=""])可以用在Controller中的方法中,那么此方法的返回将被添加到Model Map中,作为当前这个Controller的Data被绑定到Jsp页面中。

public class BookSearchController {

    // Injection的方式到一个Service
@Autowired
public BookSearchService bookSearchService; //在Jsp中映射到一个<select><option></option></select>标签作为Items内容
@ModelAttribute(value = "categories")
public List<BookCategory> getBookCategory(){
return bookSearchService.GetBookCategories();
} // 在Jsp中作为搜索条件的绑定
@ModelAttribute
public BookSearchCreatia getBookSearchCretia(){
return new BookSearchCreatia();
}
// 作为/book/search的映射action被调用,此处的BookSearchCretia将从JSP的Form中根据控件的title映射到参数的属性中
@RequestMapping(value = "/book/search", method = RequestMethod.GET)
public Collection<Book> List(BookSearchCreatia creatia) {
// 返回将用作JSP页面中的结果Render
return bookSearchService.SearchBooks(creatia);
}
}

当然@ModelAttribute也可以用在Controller 方法的参数中,此时方法执行时会首先查看当前Controller的Model中是否有相同类型的数据存在,如果不存在就会调用默认的构造函数生成一个对象放在Model中。从这一点上看他们的作用差不多,只不过@ModelAttribute单独作为Annoation作用在方法上的时候对绑定数据的构造更加灵活一些

// 可以去除之前的那个@ModelAttribute做用的那个方法

@RequestMapping(value = "/book/search", method = RequestMethod.GET)
public Collection<Book> List(@ModeAttribute BookSearchCreatia creatia) {
// 返回将用作JSP页面中的结果Render
return bookSearchService.SearchBooks(creatia);
}

接着我们将上面的Data 绑定到JSP中的控件中。 这里需要分析一下JSP中的几个关键的tag 库。

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>这个是Form库,主要用在一些Form相关的控件上。如<form:lable>, <form:lable>、<form:select>等。可以通过指定Path 或者ItemList来绑定数据源。注意Path的作用,它告诉Databinding绑定的对象 我当前控件显示的值对应这你哪个属性。他对于改变Databing对象的值起着很重要的作用

<%@ taglib prefix="spring" uri=".../core"%> 这个是Spring的Core Tag,主要用在一些逻辑控制以及Message显示

<%--
Created by IntelliJ IDEA.
User: ygshen
Date: 2015/3/30
Time: 19:41
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> //此处引入Spring的核心库和Form库
<%@ taglib prefix="spring" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<title>Book Search Page</title>
</head>
<body>
// 此处绑定Controller中给出的Data=bookSearchCretia
<form:form method="get" modelAttribute="${bookSearchCreatia">
<table>
<tr>
<td>Book Title <form:input path="title"></spring:input></td>
</tr>
<tr>
<td>Category: <form:select path="category.categoryId" items="${categories}" itemValue="categoryId"
itemLabel="categoryName"></spring:select></td>
</tr>
<tr>
<button id="search" value="">Search</button>
</tr>
</table>
</form:form> // 此处将返回的查询数据显示
<c:if test="${not empty bookList}">
<table>
<tr>
<td> Title</td>
<td> Author</td>
<td>Category</td>
</tr>
<spring:forEach items="${bookList}" var="book"> <tr>
<td><a href="<c:url value="/book/details/${book.id}" />"> ${book.title} </a></td>
<td>${book.author}</td>
<td>${book.category.categoryName}</td>
</tr>
</spring:forEach>
</table>
</c:if>
</body>
</html>

当点击上面每一本Book的 Title时候跳转到一个Book详细信息的页面。

@Controller
public class BookDetailController {
@Autowired
public BookSearchService bookSearchService; @RequestMapping(value = "/book/details/{id}",method = RequestMethod.GET)
public String FindBook(Model model,
@PathVariable int id,
HttpServletRequest request)
{
BookSearchCreatia creatia=new BookSearchCreatia();
creatia.setId(id); model.addAttribute(bookSearchService.FindBook(creatia));
return "book/bookdetail";
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags/form" %> <html>
<head>
<title>Book Detail</title>
</head>
<body>
<table>
<tr><td>Title</td><td>Author</td><td>Category</td></tr>
<c:if test="${not empty book}">
<tr><td>${book.title}</td><td>${book.author}</td><td>${book.category.categoryName}</td></tr>
</c:if>
</table>
</body>
</html>

第二点: 数据校验的绑定

Spring中的DataBinding(一)的更多相关文章

  1. Spring中的DataBinding(二) - Validation

    @Controller@RequestMapping(value = "/custom/register")public class RegistrationController ...

  2. Velocity初探小结--Velocity在spring中的配置和使用

    最近正在做的项目前端使用了Velocity进行View层的数据渲染,之前没有接触过,草草过了一遍,就上手开始写,现在又回头细致的看了一遍,做个笔记. velocity是一种基于java的模板引擎技术, ...

  3. Spring中Bean的作用域、生命周期

                                   Bean的作用域.生命周期 Bean的作用域 Spring 3中为Bean定义了5中作用域,分别为singleton(单例).protot ...

  4. Spring中Bean的实例化

                                    Spring中Bean的实例化 在介绍Bean的三种实例化的方式之前,我们首先需要介绍一下什么是Bean,以及Bean的配置方式. 如果 ...

  5. 模拟实现Spring中的注解装配

    本文原创,地址为http://www.cnblogs.com/fengzheng/p/5037359.html 在Spring中,XML文件中的bean配置是实现Spring IOC的核心配置文件,在 ...

  6. Spring中常见的bean创建异常

    Spring中常见的bean创建异常 1. 概述     本次我们将讨论在spring中BeanFactory创建bean实例时经常遇到的异常 org.springframework.beans.fa ...

  7. Spring中配置数据源的4种形式

    不管采用何种持久化技术,都需要定义数据源.Spring中提供了4种不同形式的数据源配置方式: spring自带的数据源(DriverManagerDataSource),DBCP数据源,C3P0数据源 ...

  8. spring中InitializingBean接口使用理解

    InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候会执行该方法. 测试程序如下: imp ...

  9. Quartz 在 Spring 中如何动态配置时间--转

    原文地址:http://www.iteye.com/topic/399980 在项目中有一个需求,需要灵活配置调度任务时间,并能自由启动或停止调度. 有关调度的实现我就第一就想到了Quartz这个开源 ...

随机推荐

  1. CentOS6.4x64安装mysql5.6.23(rpm)

    #查看已安装的的mysql shell>rpm -qa|grep -i mysql #根据上条命令的结果卸载mysql shell>rpm -e -nodeps mysql* #下载mys ...

  2. 利用树莓派跑python爬虫的简单教程——从无到有

    因为学校项目的原因入手了树莓派,到手先折腾了两天,发现网上的教程大都是拿他搭建服务器,mail,或者媒体服务器之类,对于在学校限时的宽带来说有点不太现实,不过低功耗适合一直开着的确启发了我.所以想到拿 ...

  3. Effective Java设定游戏 - 就是爱Java

    首先,我们先设定游戏,一个网页游戏的基本场景,主角拥有各种能力,但一开始数值都只有系统初始,随着故事的发展,会接触到各种不同的场景,获得提升角色的道具与装备,来参与更高难度的任务. 阅读全文>& ...

  4. FileMode枚举

    FileMode枚举是一个简单枚举,用于指定操作系统打开文件的方式. 枚举成员 成员值 描述 CreateNew 1 指定操作系统应创建新文件,如果文件存在则引发异常. Create 2 指定操作系统 ...

  5. CentOS、Ubuntu、Debian三个linux比较异同

    Linux有非常多的发行版本,从性质上划分,大体分为由商业公司维护的商业版本与由开源社区维护的免费发行版本. 商业版本以Redhat为代表,开源社区版本则以debian为代表.这些版本各有不同的特点, ...

  6. 百度地图LV1.5实践项目开发工具类bmap.util.jsV1.2

    /** * 百度地图使用工具类-v1.5 * * @author boonya * @date 2013-7-7 * @address Chengdu,Sichuan,China * @email b ...

  7. 网易云课堂_C++开发入门到精通_章节8:设计模式

    课时44设计模式简介 设计模式简介 面向对象设计的第一个原则:针对接口编程,而不是针对实现编程 接口->指针 实现->实例 若已存在一个类Class A,现在希望复用Class A,则有以 ...

  8. PHP Database ODBC 之 ODBC

    ODBC 是一种应用程序编程接口(Application Programming Interface,API),使我们有能力连接到某个数据源(比如一个 MS Access 数据库). 创建 ODBC ...

  9. php使用PDO方法详解

    PDO::exec:返回的是int类型,表示影响结果的条数. 复制代码 代码如下: PDOStatement::execute 返回的是boolean型,true表示执行成功,false表示执行失败, ...

  10. IOS拷贝文件到沙盒

    - (void)copyFileFromResourceTOSandbox { //文件类型 NSString * docPath = [[NSBundle mainBundle] pathForRe ...