Wednesday, March 19, 2008

java: An algorithm for converting an array to comma delimited/separated string/value (e.g. csv)

StringBuilder sb = new StringBuilder();

Long[] array = {new Long("1"), new Long("2"), new Long("3")};

for (int i = 0; i < array.length(); i++)
{
   if (i > 0) 
   {
       sb.append(", ");
   }

   sb.append(array[i]);
}

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

//output: 1, 2, 3

Tuesday, March 18, 2008

java: How to convert String array to Long array

String[] numbersString = {"1", "2", "3"};

List list = new ArrayList();

for (int i = 0; i <>
{
    Long tmp = Long.valueOf(s[i]);
    list.add(tmp); 
}    
//convert Long ArrayList to Long[] array 
Long[] numbersLong = (Long[])list.toArray(new Long[0]);

Saturday, March 15, 2008

java: How to put checkboxes in DisplayTag using Struts multibox


<display:table name="sessionScope.myFormBean.mySearchResultArrayList" id="myRecord">
<display:column title="Select">
<html:multibox property="selectedItems"
value='<%= (String)((com.abc.def.MyClass)myRecord).getAbc().toString() %>' />
</display:column>
</display:table>


Note:

myFormBean is the name of your Form Bean (i.e. MyFormBean.java)

mySearchResultArrayList is a collection getter in your MyFormBeanName.java. It will hold a collection of your MyClass.java

MyClass will represent each row in your DisplayTag (i.e. MyClass.java). Your mySearchResultArrayList will contain many MyClass.java

myRecord represents the reference to the MyClass object.

selectedItems is a String array property in your MyFormBeanName.java with a getter/setter. It will hold the values of the selected checkboxes. It will also be the name of the checkbox group in html.


MyClass.java


public class MyClass
{
private String abc;

public void setAbc(String abc)
{
this.abc = abc;
}

private String getAbc( )
{
return abc;
}
}



MyFormBean.java

public class MyFormBean
{
private String[ ] selectedItems;
private List mySearchResultArrayList;

public void setMySearchResultArrayList(List list)
{
mySearchResultArrayList = list;
String[] selectedItems = (String[]) mySearchResultArrayList.toArray(new String[0]);
}

public List get
MySearchResultArrayList( )
{
return
mySearchResultArrayList;
}

public String[ } getSelectedItems( )
{
return
selectedItems;
}

public void setSelectedItems(String[ ] selectedItems)
{
this.selectedItems = selectedItems;
}
}

javascript: How to select/deselect checkboxes

This function will select/deselect, tick/untick, check/uncheck all checkboxes belonging to a group. It can be used by a button to toggle a checkbox collection (e.g. Struts multibox in DisplayTag).


function toggle (checkboxes, isSelectAll)
{
if (checkboxes != null)
{
for (i = 0; i < checkboxes.length; i++)
{
checkboxes[i].checked = isSelectAll;
}
}
}


Usage: You can call this function on two buttons:
- select button: onclick="CheckboxUtil.toggle(document.formName.checkboxName, true)"
- deselect button: onclick="CheckboxUtil.toggle(document.formName.checkboxName, false)"


where:
- formName = name of the form.
- checkboxName = name of the checkbox group

Make sure all your checkboxes have the same checkboxName.

java: How to get DisplayTag sorting URL to work in Struts Tiles

Simply put requestURI with blank value onto the DisplayTag attribute.


<display:table name="sessionScope.myForm.searchResult"
id="record"
requestURI="">

</display:table>

java: How to convert a String ArrayList into String array

String[] stringArray = (String[]) stringArrayList.toArray(new String[0]);

java: The difference between the HTML comment <!-- --> and the JSP comment <%-- --%>

The HTML comment will show up in browser view source.

The JSP comment <%-- --%> will not show up in browser view source.

Monday, March 10, 2008

java: Why Log4J's Logger should be declared static final

Declaring Logger to be static final is recommended, particularly for long lived objects such as the Struts Action class
http://www.owasp.org/index.php/Poor_Logging_Practice:_Logger_Not_Declared_Static_Final

e.g.
private static final Logger LOGGER = Logger.getLogger(Donkey.class)

The Struts framework creates only one instance of an Action class for all client requests. e.g. FooAction and BarAction will only be created once.
Static means the Logger is not specific to a particular client request, but is shared by all requests.
Final means the Logger will only be instantiated once.

Logger is tread safe because it doesn't hold a state.