Total Pageviews

2011/03/25

Implementing a simple toString() with Apache Commons

我們在Web開發的時候,JSP頁面參數是透過form bean傳遞,有時候需要印出form bean裡頭的valuedebug,大家可以在form bean中,override toString(),如以下寫法

public String toString() {

return ToStringBuilder.reflectionToString(this);

}

實作如下

public static void main(String[] args) {

// set value to value object

Nigw002PK vo = new Nigw002PK();

vo.setCarryDate("1");

vo.setCarrySeqNo("2");

vo.setCaseStus("3");

vo.setMtgTp("4");

// print value object by toString()

System.out.println(vo.toString());

}

印出結果入下:gov.fdc.nig.domain.Nigw002PK@63396339[mtgTp=4,caseStus=3,taxCd=,carryDate=1,carrySeqNo=2,vioNo=>]

Reference: http://blogs.bytecode.com.au/glen/2007/10/05/implementing-a-simple-tostring---with-apache-commons.html

No comments: