日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Spring - Java/J2EE Application Framework 应用框架 第 4 章 属性编辑器,数据绑定,校验与BeanWeapper(Bean封装)

發(fā)布時間:2025/3/21 java 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring - Java/J2EE Application Framework 应用框架 第 4 章 属性编辑器,数据绑定,校验与BeanWeapper(Bean封装) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

第?4?章?屬性編輯器,數據綁定,校驗與BeanWeapper(Bean封裝)

4.1.?簡介

是否需要對業(yè)務邏輯進行驗證是一個常見的問題。 有關這一點存在兩種截然想法的回答,Spring提出的驗證模式(和數據綁定)對兩者都不排斥。 驗證應該是可以定制化的并且能夠依附于任何的驗證框架而不應該被強制綁定在Web層。 基于以上原因,Spring提供了一個Validator接口,這個接口可以被應用于應用程序的任何一個層面 (表示層或者邏輯層等 譯者注)

數據綁定可以使用戶輸入與應用程序的域模型進行動態(tài)綁定(或者用于處理用戶輸入對象)。 針對這一點Spring提供了所謂的DataBinder(數據綁定)。 DataBinder和Validator組成了驗證包(validation),它主要被用于MVC結構, 除此之外也可以被用于其他的地方。

BeanWrapper是Spring架構的一個基本組件,它在很多地方都是很有用的。 然而,你可能很少直接使用BeanWrapper。 由于這是一篇參考文檔,所以我們覺得對此稍作解釋還是有必要的。 因為今后你也許進行對象與數據之間的綁定操作,到時就會用到BeanWrapper 了。

Srping大量的使用了PropertyEditors(屬性編輯)。 它屬于JavaBeans規(guī)范的一部分。就像我們上面提到的BeanWrapper一樣, PropertyEditors和BeanWrapper以及DataBinder三者之間有著密切的聯(lián)系。

4.2.?使用DataBinder進行數據綁定

DataBinder構建于BeanWrapper之上。[2].

4.3.?Bean處理與BeanWrapper

org.springframework.beans包遵循Sun發(fā)布的JavaBeans標準。 一個JavaBean是一個簡單的包含無參數構造函數的類,并且包含seter和getter屬性方法。 例如prop屬性對應setProp(...)方法和getProp()方法. 如果需要了解JavaBeans的詳細信息可以訪問Sun的網站(java.sun.com/products/javabeans).

這個包中一個非常重要的概念就是BeanWrapper?接口以及它的實現(BeanWrapperImpl)。 根據JavaDoc中的說明,BeanWrapper提供了設置和獲得屬性值的功能(單個的或者是批量的), 可以獲得屬性描述、查詢只讀或者可寫屬性。而且,BeanWrapper還支持嵌套屬性, 可以不限制嵌套深度的進行子屬性的設置。所以,BeanWrapper支持標準JavaBeans的PropertyChangeListeners?和VetoableChangeListeners。除此之外,BeanWrapper還提供了設置索引屬性的支持。 一般情況下不在應用程序中直接使用BeanWrapper而是使用DataBinder和BeanFactory。 從BeanWrapper的名字就可以看出:它封裝了一個bean的行為,比如設置和取出屬性值。

The way the BeanWrapper works is partly indicated by its name:?it wraps a bean?to perform actions on that bean, like setting and retrieving properties.

4.3.1.?設置和提取屬性以及嵌套屬性

設置和提取屬性可以通過使用重載的setPropertyValue(s)和getPropertyValue(s)?方法來完成。 在Srping的JavaDoc中對它們有詳細的描述。有關這兩個方法的一些約定習慣如下所列:

表?4.1.?屬性示例

語法解釋
name指出與?getName()?或?isName()?和?setName()相關的name信息
account.name提供嵌套屬性的name,比如?getAccount().setName()?或getAccount().getName()方法
account[2]Indicates the?third?element of the indexed property?account. Indexed properties can be of type?array,?list?or other?naturally ordered?collection

在下面的例子中你將看到一些使用BeanWrapper設置屬性的例子。

注意:如果你不打算直接使用BeanWrapper這部分不是很重要。 如果你僅僅使用DataBinder?和BeanFactory或者他們的擴展實現, 你可以跳過這部分直接閱讀PropertyEditors的部分。

考慮下面的兩個類:

public class Company {private String name;private Employee managingDirector;public String getName() { return this.name; }public void setName(String name) { this.name = name; } public Employee getManagingDirector() { return this.managingDirector; }public void setManagingDirector(Employee managingDirector) {this.managingDirector = managingDirector;} }

public class Employee {private float salary;public float getSalary() {return salary;}public void setSalary(float salary) {this.salary = salary;} }

下面的代碼顯示了如何接收和設置上面兩個類的屬性 :?Companies?and?Employees

Company c = new Company(); BeanWrapper bwComp = BeanWrapperImpl(c); // setting the company name... bwComp.setPropertyValue("name", "Some Company Inc."); // ... can also be done like this: PropertyValue v = new PropertyValue("name", "Some Company Inc."); bwComp.setPropertyValue(v);// ok, let's create the director and tie it to the company: Employee jim = new Employee(); BeanWrapper bwJim = BeanWrapperImpl(jim); bwJim.setPropertyValue("name", "Jim Stravinsky"); bwComp.setPropertyValue("managingDirector", jim);// retrieving the salary of the managingDirector through the company Float salary = (Float)bwComp.getPropertyValue("managingDirector.salary");

4.3.2.?內建的(PropertyEditors)和類型轉換

Spring大量的使用了PropertyEditors。有時候它比對象自身描述屬性更加容易。 比如,當我們轉換一個日期類型(date)時,可以把它描述成一個用戶更容易理解的樣子。 這一過程可以通過注冊一個自定義編輯器來實現。 java.beans.PropertyEditor.注冊一個自定義編輯器告訴BeanWrapper我們將要把屬性轉換為哪種類型。 你可以從Sun的JavaDoc中?java.beans包了解有關java.beans.PropertyEditor的細節(jié)。

下面是幾個在Spring中設置屬性的例子

  • 使用PropertyEditors設置Bean屬性。 當你使用在XML文件中聲明的java.lang.String作為bean的屬性時, Spring將使用ClassEditor來嘗試獲得類對象的參數

  • 在Spring MVC架構中傳輸HTTP請求參數,將使用各種PropertyEditors, 因此你可以綁定各種CommandController的子類。 Spring提供了很多內建的屬性編輯器來讓這些操作變得簡單。 所有這些都列在下面的表格中,你也可以從org.springframework.beans.propertyeditors包中找到它們:

Spring has a number of built-in PropertyEditors to make life easy. Each of those is listed below and they are all located in the?org.springframework.beans.propertyeditors?package:

表?4.2.?Built-in PropertyEditors

ClassExplanation
ClassEditorParses Strings representing classes to actual classes and the other way around. When a class is not found, an IllegalArgumentException is thrown
FileEditorCapable of resolving Strings to?File-objects
LocaleEditorCapable of resolving Strings to?Locale-objects and vice versa (the String format is [language]_[country]_[variant], which is the same thing the toString() method of Locale provides
PropertiesEditorCapable of converting Strings (formatted using the format as defined in the Javadoc for the java.lang.Properties class) to?Properties-objects
StringArrayPropertyEditorCapable of resolving a comma-delimited list of String to a String-array and vice versa
URLEditorCapable of resolving a String representation of a URL to an actual?URL-object

Spring使用java.beans.PropertyEditorManager來為屬性編輯器設置搜索路徑, 這一點時必須的。搜索路徑同時包括了sun.bean.editors,這個類包含了前面提到的PropertyEditors ,顏色以及所有原始類型。

4.3.3.?其他特性

除了前面提到的特性,下面還有一些有價值的特性。

  • ? 確定可讀能力和可寫能力:使用isReadable()?和?isWritable()方法你可以確定一個屬性是否為可讀或者可寫。

  • ? 獲得屬性描述(PropertyDescriptors):使用getPropertyDescriptor(String)?和?getPropertyDescriptors()方法可以獲得屬性的描述(java.beans.PropertyDescriptor),有時候這會派上用場。

from:?http://docs.huihoo.com/spring/zh-cn/validation.html

總結

以上是生活随笔為你收集整理的Spring - Java/J2EE Application Framework 应用框架 第 4 章 属性编辑器,数据绑定,校验与BeanWeapper(Bean封装)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。