I need to pass a query-string parameter to a Spring command bean. I have just tried the following workaround.
<c:if test="${not empty param.id}">
<form:input path="id"/>
</c:if>
To me surprise, it automatically sets the value supplied on the query-string (something like http://localhost:8080/xxx/aaa.htm?id=100) to the corresponding command bean. It is what exactly I want to achieve but I'm completely unaware of why this happens.
Why and how does it automatically set the parameter to a corresponding property of a command bean?
In the command bean, this parameter is mapped to a property of type java.lang.Long. So, if I modify this parameter to a value that is not represented as a Long value like xxx, then I expect it to cause an appropriate exception to be thrown but it doesn't.
If I try to pass a string which is not a Long value (like xxx), then it results in - HTTP Status 400,
The request sent by the client was syntactically incorrect.
I'm quite unsure of this behaviour. Why does this happen?
When a user changes this parameter in URL which is not represented as Long, I want to show an appropriate user-friendly error message. For this to be so, I have registered several custom property editors (they work as they are) but for Spring to show an appropriate error message from a property file, an appropriate exception should be caused which is then consumed by Spring and an appropriate error message from the property file is displayed.
I'm using the Spring framework 3.2.0.
Edit:
The problem in the second section of the question (the one with HTTP Status 400) has been resolved. It was because I was not receiving the BindingResult parameter in the method which is mapped with the GET HTTP request in the Spring controller class which is necessary like,
@RequestMapping(method={RequestMethod.GET}, value={"admin_side/Country"})
private String showForm(@ModelAttribute("countryBean") CountryBean command, BindingResult errors, Map model)
{
//...
}
The BindingResult errors parameter was missing previously hence it failed to report errors. It now works as expected.
A question still remains as in the first section that I really don't understand. Why and how does this query-string parameter id is bound to the Spring command bean (automatically)? Kindly let me know.