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这个开源 ...
随机推荐
- a 标签
a标签成为块元素后,宽度会百分百撑开,但高度不会,需要加高度.
- 十进制二进制之间的转化 PHP算法
[ 十进制转二进制 ] function test($var){ $func = function($i){ if($i < 2){ return $i; } $return['int'] = ...
- 检测.net framework 版本
项目中,自己要制作asp.net项目的安装文件,由于项目依赖于.net framework 3.5 sp1,故需检测环境是否符合要求,才能安装程序 度娘找到检测方案:各.net版本对应的安装补录下都有 ...
- 使用jekyll主题
github上面有很多基于jekyll的主题 https://github.com/theme4jekyll 使用起来也非常方便.直接clone到本地 这些主题,包括了很多模板,在新建博客的时候可以在 ...
- C++----练习--string 从文件中一个一个单词的读直到文件尾
从文件中读取单词.并每行显示一个: 1. #include<iostream> #include<string> #include<vector> int main ...
- Monkey学习笔记<四>:Monkey服务器命令
#使用如下命令将本地pc和手机连接起来 adb shell monkey --port 1080 adb forward tcp 1080:tcp 1080 telnet localhost 1080 ...
- Eclipse Clojure 开发插件
参考:http://doc.ccw-ide.org/documentation.html#install-as-plugin 安装Eclipse Clojure插件 这里安装的插件是Countercl ...
- jQuery UI 之 Bootstrap 快速入门
转载自(http://www.shouce.ren/example/show/s/6444) 1. 下载 这个页面是用来展示 jQuery UI Bootstrap 项目的 -- 我们将 Bootst ...
- LeeCode-Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1' bits it has For exampl ...
- Java根据年份算出所属的生肖。
一个小程序~ public String getYear(Integer year){ if(year<1900){ return "未知"; } Integer start ...