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这个开源 ...
随机推荐
- CI(CodeIgniter)学习第二讲
一.CI的文件结构: 了解CI的文件结构可以帮助我们快速的对CI框架有一个整体的认识,就好像我们去了一个陌生的城市一样,对你来讲周围的一切都是陌生和未知的,要想快速的了解这座城市,你可以买一张这座城市 ...
- 22. Generate Parentheses
https://leetcode.com/problems/generate-parentheses/ 题目大意:给出n对小括号,求出括号匹配的情况,用列表存储并返回,例如:n=3时,答案应为: [ ...
- HDU 3501 Calculation 2(欧拉函数)
Calculation 2 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submi ...
- WPF安装部署小结
开机启动 右击"MySetup">>"视图">>"注册表",在"HKEY_LOCAL-MACHINE&qu ...
- Jmeter -- 初体验
一.Jmeter参数 在命令行输入Jmeter --help得到以下信息: To run Apache JMeter in GUI mode:Double-click on the ApacheJMe ...
- Myeclipse6.5配置反编译插件
PS:jad.exe位置与Myeclipse6.5安装目录平行
- poj 1129 Channel Allocation
http://poj.org/problem?id=1129 import java.util.*; import java.math.*; public class Main { public st ...
- Qt之HTTPS登录(集成QNetworkAccessManager提前修改QSslConfiguration,然后post)
简述 HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版.即HTTP ...
- 八大排序c++可运行精简版,一目了然
#include <iostream> using namespace std; // 插入排序开始===================== void insert_sort(int a ...
- 在Linux CentOS 6.5 (Final)上安装git-1.9.0
CentOS 6.5 (Final)默认安装的git版本为1.7.1.3,而我们希望安装1.9.0版本.由于rpm安装库里没有1.9.0版本,因此我们需要找其它方法来安装. 网上有很多文章介绍了如何从 ...