Monday, September 30, 2013

Spring: Inject comma separated values into a List

1. Define your properties in a properties file

gestures=rock, paper, scissors, lizard, spock

2. Ensure that you have your context property placeholder defined in your context xml

<context:property-placeholder location="classpath*:*.properties"/>

3. Finally in your java class, you can inject the value when you define the class member:

 @Value("#{'${gestures}'.split(',')}")
 private List<String> gameGestures;


Tuesday, September 17, 2013

Java String.trim() is dead

Ever had problems with spaces that were not removed by String.trim() ? 

This is because trim() only removes breaking white spaces. 


But sometimes, if you are copying from Microsoft products (like Note, for example) some white spaces get copied by default. These whitespaces are non-breaking white spaces, which will not be handled by trim().


To get around this, instead of using trim() use com.google.common.base.CharMatcher




return CharMatcher.WHITESPACE.trimFrom(" Hello   ");


returns "Hello" regardless of what kind of spaces are involved.


Also note that other java and Apache Commons methods like StringUtils.isBlank() might use only breaking white spaces. That is why I prefer to eschew these and use com.google.common.base where ever possible.

For a full list of white space chars, take a peek at http://www.theasciicode.com.ar/extended-ascii-code/non-breaking-space-no-break-space-ascii-code-255.html

Thursday, April 18, 2013

Spring: Java dynamically change select box options with ajax and jquery


First define the select box in JSP:

<form:select path="contactPhoneNumber" id="contactPhoneNumber">
<form:option value="" label="<Choose contact>"/>
<form:options items="${contactList}" itemLabel="fullName" itemValue="phoneNumber"/>
</form:select>


Define a JS to call the backend:
function updateContactList() {


$.ajaxPrefilter(function (settings, localSettings, jqXHR) {
var tok = $('input[name=CSRFToken]').val();
jqXHR.setRequestHeader("CSRFToken", tok);
});


$.ajax({
dataType: "json",
url: "/updateurl",
type: 'POST',
success: function (result, textStatus) {


cb = '';
$.each(result, function (i, data) {
cb += '';
});


$('#contactPhoneNumber option:gt(0)').remove();
$("#contactPhoneNumber").append(cb);
$("#contactPhoneNumber").selectbox();




},
error: function (xhr, ajaxOptions, thrownError) {


$('#contactPhoneNumber option:gt(0)').remove();
$("#contactPhoneNumber").selectbox();
}


});
}


Now create the controller method:
@ResponseBody
@RequestMapping(value = "/updateurl", method = RequestMethod.POST)
public String updateContactList(@ModelAttribute("orderForm") OrderForm orderForm,
BindingResult result, ModelMap model) {

// Note that the Contact object has member variables 
// fullName and phoneNumber
// and gettters and setters for each

List<Contact> contactList = fillInTheContacts();

return new Gson().toJson(contactList);

}


Thats it. When the JSON string is returned to the AJAX call, it parses it out in the .each loop and injects the HTML into the page using jQuery

So now your select box options should automatically be updated.