Dave
Dave

Reputation: 19150

What is the proper way to format an error message in Spring?

I'm using Spring 3.1.0.RELEASE. How do you properly format an error message in Spring? In my properties file, I have …

Incomplete.headers.fileData=The file is missing the following required header(s): %s

Then in my validation class, I have

public void validate(Object target, Errors errors) {
    ...
    final String missingHeadersStr = Joiner.on(",").join(missingHeaders);
    errors.rejectValue("fileData", "Incomplete.headers.fileData", new Object[] {missingHeadersStr}, "The file is missing some required headers.");

However, even though (through debugging) I've verified that the "missingHeadersStr" field is non-empty, all that displays on my JSP is

The file is missing the following required header(s): %s

What is the proper way to format an error message in Spring? For what its worth, I'm displaying them on the JSP like so

<form:errors path="fileData" cssClass="error" />

Upvotes: 0

Views: 1958

Answers (1)

JB Nizet
JB Nizet

Reputation: 691755

The documentation says:

errorArgs - error arguments, for argument binding via MessageFormat (can be null)

(emphasis mine).

You're giving a pattern usable with String.format(). But MessageFormat doesn't use that. The patter should be:

The file is missing the following required header(s): {0}

Upvotes: 3

Related Questions