Sort Multiple Fields With Collections.sort

25 03 2008

ComparatorChain chain = new ComparatorChain();
Comparator comp = new BeanComparator("firstname", new NullComparator(true));
chain.addComparator(comp,true);
comp = new BeanComparator("lastname", new NullComparator(true));
chain.addComparator(comp);
Collections.sort(customerlist,chain);

NullComparator(true) <– true places null values at the front of the list
chain.addComparator(comp,true); <– true reverse the list
customerlist = list of dynabeans





Custom Format Number

25 03 2008

DecimalFormatSymbols sym = new DecimalFormatSymbols();
sym.setGroupingSeparator('-');
NumberFormat accFormatter = new DecimalFormat("###,###,####,######",sym);
BigDecimal num = new BigDecimal("1111111111111111");
String formattedNum = accFormatter.format(num);

formattedNum = 111-111-1111-111111





Create List Of Dates With DynaBean

27 09 2007

Create list of dates (last 6 months) to populate listbox in .jsp.

// get search date listbox
DynaBean dateBean = new LazyDynaBean();
List<DynaBean> dateList = new ArrayList<DynaBean>();
SimpleDateFormat formatter = new SimpleDateFormat("MMM yyyy");
// display 6 months starting from current month
Calendar cal = Calendar.getInstance();
// get current date
cal.setTime(new Date());
for(int i=0; i<5; i++) {
dateBean = new LazyDynaBean();
dateBean.set("property", formatter.format(cal.getTime()));
dateBean.set("value", cal.getTime());
// add to list
dateList.add (dateBean);
// subtract 1 month
cal.add(Calendar.MONTH, -1);
}
// send to form bean (type="java.util.List")
PropertyUtils.setUtils(form, "datelist", dateList);

in .jsp


<bean:define id="dates" name="MyFormName" property="datelist"/>
<html:select property="s_valueDate" value='<%= request.getParameter("s_valueDate") %>'> <html:options collection="dates" property="value" labelProperty="property"/>
</html:select>





Set Not To Store Page In Cache

11 09 2007

<head>
<%
response.setHeader("Cache-Control","no-cache"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", 0); //prevents caching at the proxy server
%>
</head>





Javascript Alert Form Bean

4 09 2007

<logic:notEmpty name="LoginForm" property="javascriptAlertMsg">
<script>alert('<bean:write name="LoginForm" property="javascriptAlertMsg"/>')</script>
</logic:notEmpty>





Javascript Alert Messages From Message Resource

4 09 2007

alert('<bean:message key="MSG001"/>');

Validation Message:
var fieldname = '<bean:message key="fieldname.amount"/>';
var errorMsg = '<bean:message key=" errors.float" arg0="'+fieldname+'"/>';
alert(errorMsg);

In Message Resource:

MSG001 Hello world!
fieldname.amount Payment Amount
errors.float {0} is not a float





Call Struts Validation Using Javascript

29 08 2007

if(validateNameOfForm(document.forms[0])) {
document.forms[0].submit();
}