Spring中的DataBinding(一)
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(一)的更多相关文章
- Spring中的DataBinding(二) - Validation
@Controller@RequestMapping(value = "/custom/register")public class RegistrationController ...
- Velocity初探小结--Velocity在spring中的配置和使用
最近正在做的项目前端使用了Velocity进行View层的数据渲染,之前没有接触过,草草过了一遍,就上手开始写,现在又回头细致的看了一遍,做个笔记. velocity是一种基于java的模板引擎技术, ...
- Spring中Bean的作用域、生命周期
Bean的作用域.生命周期 Bean的作用域 Spring 3中为Bean定义了5中作用域,分别为singleton(单例).protot ...
- Spring中Bean的实例化
Spring中Bean的实例化 在介绍Bean的三种实例化的方式之前,我们首先需要介绍一下什么是Bean,以及Bean的配置方式. 如果 ...
- 模拟实现Spring中的注解装配
本文原创,地址为http://www.cnblogs.com/fengzheng/p/5037359.html 在Spring中,XML文件中的bean配置是实现Spring IOC的核心配置文件,在 ...
- Spring中常见的bean创建异常
Spring中常见的bean创建异常 1. 概述 本次我们将讨论在spring中BeanFactory创建bean实例时经常遇到的异常 org.springframework.beans.fa ...
- Spring中配置数据源的4种形式
不管采用何种持久化技术,都需要定义数据源.Spring中提供了4种不同形式的数据源配置方式: spring自带的数据源(DriverManagerDataSource),DBCP数据源,C3P0数据源 ...
- spring中InitializingBean接口使用理解
InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候会执行该方法. 测试程序如下: imp ...
- Quartz 在 Spring 中如何动态配置时间--转
原文地址:http://www.iteye.com/topic/399980 在项目中有一个需求,需要灵活配置调度任务时间,并能自由启动或停止调度. 有关调度的实现我就第一就想到了Quartz这个开源 ...
随机推荐
- centos搭建zabbix
安装一些依赖包,不同情况缺的包不同 yum install mysql-devel curl curl-devel net-snmp net-snmp-devel perl-DBI php-gd ph ...
- postgresql赋予/撤消 用户权限
(1)给予权限:grant grant select on 表名 to 用户名: (2)撤消权限:revoke revoke select on 表名 from ...
- 开机时候系统总是提醒Android系统更新
今天刷了个android的rom,平常没有经常刷机,对这个也不是特别了解. 但是刷完开机,显示系统升级,一开始都是18个app,后来捣鼓了几次,安装了几个常用的软件,居开机的时候,升级的app需要90 ...
- 【图解ASP.NET MVC运行机制理解-简易版】
很多盆友咨询ASP.NET MVC的机制.网上也有好多.但是都是相当深奥.看的云里雾里的.我今天抽空,整理个简易版本.把处理流程走一遍. 当然,这个只是处理请求的一部分环节.百度的面试题“客户端从浏览 ...
- 【转】 怎么刷入BOOT.IMG(刷机后开机卡在第一屏的童鞋请注意)-------不错不错
原文网址:http://bbs.gfan.com/android-3440837-1-1.html 之前呢,有好多机油问我关于刷机卡屏的问题,我解答了好多,但一一解答太费事了,在这里给大家发个贴吧.其 ...
- Windows多线程同步系列之一-----互斥对象
多线程同步之互斥对象 作者:vpoet mail:vpoet_sir@163.com 对卖票问题进行线程间同步,本文将在上文的基础上,使用互斥对象对线程进行同步. 首先看看windows API ...
- 多线程并发 synchronized对象锁的控制与优化
本文针对用户取款时多线程并发情境,进行相关多线程控制与优化的描述. 首先建立用户类UserTest.业务操作类SynchronizedTest.数据存取类DataStore,多线程测试类MultiTh ...
- telnet如何操作Memcached缓存系统?
4.(1)telnet操作Memcached 许多语言都实现了连接memcached的客户端,其中以Perl.PHP为主.仅仅memcached网站上列出的语言就有:• Perl • PHP • Py ...
- Ajax——ajax调用数据总结
在做人事系统加入批量改动的功能中,须要将前台中的数据传给后台.后台并运行一系列的操作. 通过查询和学习了解到能够通过ajax将值传入到后台,并在后台对数据进行操作. 说的简单点.就是ajax调用后台的 ...
- Android 官方文档:(二)应用清单 —— 2.26 <uses-permission>标签
syntax: <uses-permission android:name="string" android:maxSdkVersion="inte ...