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. HDU 1026 Ignatius and the Princess I(BFS+优先队列)

    Ignatius and the Princess I Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &am ...

  2. 十、装饰(Decorator)模式 --结构模式(Structural Pattern)

    装饰(Decorator)模式又名包装(Wrapper)模式[GOF95].装饰模式以对客户端透明的方 式扩展对象的功能,是继承关系的一个替代方案. 装饰模式类图: 类图说明: 抽象构件(Compon ...

  3. BZOJ 3529 数表(莫比乌斯反演)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3529 思路:令F(i)为i的约数和, 1<=x<=n,1<=y<=m G(i ...

  4. ViewBag、ViewData和TempData使用方法、区别与联系

    一.区别与联系 ViewData 和 TempData 都可以传递弱类型数据,区别如下:TempData 只在当前 Action 中有效,生命周期和 View 相同:保存在Session中,Contr ...

  5. linux下mysql数据库的操作

    本文主要针对linux下mysql数据库的安装,以及数据库的创建和简单的数据库操作进行说明. ①.Mysql数据库的安装: 数据库的安装分为源码安装和rpm安装. 当然对于老手来说需要进行一些自定义的 ...

  6. HDU1754(线段树)

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  7. 如何将ASM中的数据文件复制到操作系统中

    环境:Red Hat 5.7 + Oracle 10.2.0.5.0 Rac+ASM 如果你的Oracle数据库系统使用正使用ASM自动存储管理,你可曾想过要窥视一下ASM中的数据文件,ASM是个黑匣 ...

  8. [转]ActiveMQ 即时通讯服务 浅析

    一. 概述与介绍 ActiveMQ 是Apache出品,最流行的.功能强大的即时通讯和集成模式的开源服务器.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provide ...

  9. Handshakes(思维) 2016(暴力)

    Handshakes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Sta ...

  10. IOS Custom NavigationItem --写titleView

    //先自己写一个titleView UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];//all ...