網頁

2014年2月12日 星期三

Commons Beanutils使用getIndexedProperty()

訪問集合或陣列內bean的field,Apache Commons Beanutils提供了getIndexedProperty()來處理,透過BeanUtils的靜態方法,取得集合或陣列bean中的單一元素值的方法,利用這個方法,Mix可以很方便的取得field值。





Mix建立了1個bean:Bookstore,只有4個屬性,並實作getter/setter。
  1. id:唯一識別碼。
  2. name:名稱。
  3. magazines:存放Magazine的List。
  4. books:存放Book的map。

Bookstore


package org.openyu.java.commons.beanutils;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.openyu.java.commons.lang.Book;

/**
 * The Class Bookstore.
 */
public class Bookstore {

 /** The id. */
 private long id;

 /** The name. */
 private String name;

 /** The magazines. */
 private List<Magazine> magazines = new LinkedList<Magazine>();

 /** The books. */
 private Map<String,Book> books=new LinkedHashMap<String,Book>();

 /**
  * Instantiates a new bookstore.
  */
 public Bookstore() {
 }

 /**
  * Gets the id.
  *
  * @return the id
  */
 public long getId() {
  return id;
 }

 /**
  * Sets the id.
  *
  * @param id the new id
  */
 public void setId(long id) {
  this.id = id;
 }

 /**
  * Gets the name.
  *
  * @return the name
  */
 public String getName() {
  return name;
 }

 /**
  * Sets the name.
  *
  * @param name the new name
  */
 public void setName(String name) {
  this.name = name;
 }

 /**
  * Gets the magazines.
  *
  * @return the magazines
  */
 public List<Magazine> getMagazines() {
  return magazines;
 }

 /**
  * Sets the magazines.
  *
  * @param magazines the new magazines
  */
 public void setMagazines(List<Magazine> magazines) {
  this.magazines = magazines;
 }

 /**
  * Gets the books.
  *
  * @return the books
  */
 public Map<String, Book> getBooks() {
  return books;
 }

 /**
  * Sets the books.
  *
  * @param books the books
  */
 public void setBooks(Map<String, Book> books) {
  this.books = books;
 }


 /*
  * (non-Javadoc)
  *
  * @see java.lang.Object#toString()
  */
 @Override
 public String toString() {
  return new ToStringBuilder(this)
  //return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
    .append("id", id)
    .append("name", name)
    .toString();
 }
}





getIndexedProperty()

取得集合或陣列內bean指定名稱的field值。

 @Test
 public void getIndexedProperty() throws Exception {
  Bookstore bookstore = new Bookstore();
  //
  Magazine magazine = new Magazine();
  magazine.setId(1L);
  magazine.setName("OpenYu");
  magazine.setPrice(1000);
  bookstore.getMagazines().add(magazine);
  //
  Magazine magazine2 = new Magazine();
  magazine2.setId(2L);
  magazine2.setName("OpenYu2");
  magazine2.setPrice(2000);
  bookstore.getMagazines().add(magazine2);
  //
  Magazine magazine3 = new Magazine();
  magazine3.setId(3L);
  magazine3.setName("OpenYu3");
  magazine3.setPrice(2000);
  bookstore.getMagazines().add(magazine3);
  //
  String result = BeanUtils.getIndexedProperty(bookstore, "magazines[1]");
  System.out.println(result);
 }



console查看其結果。

Magazine[id=2,name=OpenYu2,author=<null>]



getMappedProperty()

取得map內bean指定名稱的field值。

 @Test
 public void getMappedProperty() throws Exception {
  Bookstore bookstore = new Bookstore();
  Book book = new Book();
  book.setId(1);
  book.setName("OpenYu");
  bookstore.getBooks().put("One", book);
  //
  Book book2 = new Book();
  book2.setId(2);
  book2.setName("OpenYu2");
  bookstore.getBooks().put("Two", book2);
  //
  Book book3 = new Book();
  book3.setId(3);
  book3.setName("OpenYu3");
  bookstore.getBooks().put("Three", book3);
  //
  String result = BeanUtils.getMappedProperty(bookstore, "books(Two)");
  System.out.println(result);
 }



console查看其結果。

Book[id=2,name=OpenYu2,author=<null>]









沒有留言:

張貼留言

Related Posts Plugin for WordPress, Blogger...