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";
    }
}
View Code
<%@ 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>

 

 第二点: 数据校验的绑定

posted on 2015-04-06 18:58  ygshen  阅读(479)  评论(0编辑  收藏  举报

导航