0

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.

1 Answer 1

1

That's the whole point of command beans: containing the parameters sent in the request. Parameter binding to form/command objects is at the core of ever MVC framework since Struts 1. Here's the relevant documentation section:

16.3.3.1 Supported method argument types

The following are the supported method arguments:

  • [...]
  • Command or form objects to bind request parameters to bean properties (via setters) or directly to fields, with customizable type conversion, depending on @InitBinder methods and/or the HandlerAdapter configuration. See the webBindingInitializer property on RequestMappingHandlerAdapter. Such command objects along with their validation results will be exposed as model attributes by default, using the command class class name - e.g. model attribute "orderAddress" for a command object of type "some.package.OrderAddress". The ModelAttribute annotation can be used on a method argument to customize the model attribute name used.
Sign up to request clarification or add additional context in comments.

1 Comment

That's exactly what I was trying to understand. Thank you very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.