Wednesday, June 17, 2009

Tip: Eclipse auto-compile errors

If your Eclipse project has auto-compile errors popping up all over the place, and Eclipse can't even comprehend simple Java imports, make sure rt.jar, the Java run-time library, is in your project's Libraries path. Go to Project -> Properties -> Java Build Path -> Libraries and make sure rt.jar is there. If it isn't, add it. This fixed the problem for me.

Friday, June 12, 2009

Tips: Spring MVC and messages.properties

Tip #1:

If a message in your messages.properties file contains single quotes, you may find that these do not display properly. The trick is to use Unicode hex values instead. That said, avoid using \u0027, the Unicode hex value for apostrophe, as it may not work correctly. (It didn't for me.) Use \u2018 and \u2019 instead. These are, respectively, the left single quotation mark and the right single quotation mark.

So, for example, instead of...
errors.required.parameter=Required parameter '{0}' is not present.
... use...
errors.required.parameter=Required parameter \u2018{0}\u2019 is not present.

Tip #2:

If you pass a number parameter into a message in your messages.properties file, it will automatically be formatted as a number and commas will be inserted. For example, 12300 will be displayed as 12,300. If this number is an identifier, and you don't want commas to be inserted, convert the number to a string before passing it to your translation mechanism.

Example:

The message definition is:
errors.proposal.not.found=Proposal {0} not found.

Code:
Long proposalId = 227128;
String errorMessage= org.springframework.context.MessageSource.getMessage
("errors.proposal.not.found", new Object[] {
proposalId}, Locale.getDefault());

Error message:
Proposal 227,128 not found.

Change your code to:
org.springframework.context.MessageSource.getMessage
("errors.proposal.not.found", new Object[] {
proposalId.toString()}, Locale.getDefault());

Now the error message is:
Proposal 227128 not found.

Tuesday, June 9, 2009

User-friendly binding error messages in Spring MVC

In some cases, Spring's binding error messages are not fit to display to the user. How can we get around this? If we override SimpleFormController.onBindAndValidate() in our controller, in the hopes of modifying the BindException parameter accordingly, we'll be hard-pressed to find methods allowing us to do so.

One way to display user-friendly binding error messages is to override SimpleFormController.onBindAndValidate() as follows:

@Override
protected void onBindAndValidate(HttpServletRequest request, Object command,
BindException errors) throws Exception {
if (errors.hasErrors()) {
Map errorsMap = new HashMap();
List errorList = errors.getAllErrors();
for (FieldError error : errorList) {
String msg = "";
try {
msg = this.translate(error.getCode(), error.getArguments());
} catch (Exception e) {}
if (msg.contains("quantity")) {
msg = msg.replace(error.getField(), "quantity");
} else if (msg.contains("unitPrice")) {
msg = msg.replace(error.getField(), "unit price");
}
msg += ".";
errorsMap.put(error.getField(), msg);
}
request.setAttribute("errorsMap", errorsMap);
}
}
This creates a map of user-friendly error messages, keyed by the field names with which the error messages are associated. You can then use it in your JSP, possibly with the tag.