Jumat, 14 November 2008

Displaytag pakai decorator

< name="accgroups" defaultsort="1" defaultorder="descending" requesturi="#" class="datatbl rowObject" pagesize="20" export="true" decorator="my.ListAccountStructureDecorator">
< name="basic.empty.showtable" value="true">
< name="export.excel.filename" value="groupstructure.xls">
< name="export.csv.filename" value="groupstructure.csv">
< name="export.pdf" value="false">
< property="accountType" title="Account Type" sortable="true" headerclass="sortable">
< property="accountNumber" title="Account Number" sortable="true" headerclass="sortable">
< property="accountName" title="Company Name" sortable="true" headerclass="sortable">
< property="acronym" title="Acronym" sortable="true" headerclass="sortable">
< /display:table>

Decorator:
public class ListAccountStructureDecorator extends TableDecorator {

public String getAccountNumber() {

AccGroupStructureReport data = (AccGroupStructureReport)getCurrentRowObject();

return data.getAccount().getAccountNumber();
}

public String getAccountName() {

AccGroupStructureReport data = (AccGroupStructureReport)getCurrentRowObject();

return data.getAccount().getAccountName();

}

public String getAccountType() {

AccGroupStructureReport data = (AccGroupStructureReport)getCurrentRowObject();

return data.getAccType().toString();
}
}

contoh penggunaan struts.xml pada Struts2

< ?xml version="1.0" encoding="UTF-8"?>
< !DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<>
< name="backoffice_sampel" extends="default" namespace="/backoffice/sampel">
< name="*@crud_*" method="{2}" class="my.sample.{1}Action">
< name="input">/jsp/sampel/{1}/view.jsp< /result >
< name="list">/jsp/sampel/{1}/list.jsp< /result >
< name="error">/jsp/sampel/{1}/error.jsp< /result >
< name="add">/jsp/sampel/{1}/add.jsp< /result >
< name="edit">/jsp/sampel/{1}/edit.jsp< /result >
< name="success" type="redirect-action">{1}@list< /result >
< name="redirection" type="redirect-action">
< name="actionName">${toClassName}@crud_${toMethodName}< /param>
< name="namespace">/backoffice/sampel< /param >
< /result >
< /action >
< /package>
< /struts>

Kamis, 13 November 2008

Ubah String ke Date

Kita akan mengubah String dengan format MM/yyyy ke format Date DD/MM/yyyy.

Contoh:
Input String 02/2008
Output Date 01 Feb 2008

Cuplikan code:

//deklarasikan variabel formatter
DateFormat formatter = new SimpleDateFormat("MM/yyyy");
Date eDate;

Method :
public void convertEndDate(String eDate) {

try {
this.eDate = (Date)formatter.parse(eDate);

} catch (ParseException e) {
e.printStackTrace();
}
}

Menjumlahkan BigDecimal

Berikut ini adalah contoh untuk menjumlahan/total dari BigDecimal

final int NUM_DIGITS = 2;
MathContext precision = new MathContext(NUM_DIGITS);

BigDecimal result = new BigDecimal(0.00, precision);

for(int i = 0;i < cReport.size();i++){
result = result.add(cReport.get(i).getBalance(), precision);
}