Can a Spring form command be a Map? I made my command a Map by extending HashMap and referenced the properties using the ['property']
notation but it didn't work.
Command:
public class MyCommand extends HashMap<String, Object> {
}
HTML form:
Name: <form:input path="['name']" />
Results in the error:
org.springframework.beans.NotReadablePropertyException: Invalid property '[name]' of bean class [com.me.MyCommand]: Bean property '[name]' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
Is this not allowed or do I have incorrect syntax?
The 'path' attribute is the most important attribute for most of the tags in the library; it indicates what request-scoped property the input should bind to.
To use Spring's form tags in our web application, we need to include the below directive at the top of the JSP page. This form tag library comes bundled in spring-webmvc. jar and the library descriptor is called spring-form. tld.
Form Backing Object/Command Object This is a POJO that is used to collect all information on a form. It contains data only. It is also called a Command Object in some Spring tutorials. For example, an add a new car form page will have a Car form backing object with attribute data such as Year, Make and Model.
Springn MVC commands need to use JavaBeans naming conventins (ie getXXX() and setXXX()) so no you can't use a map for that.
One alternative is to have a bean with a single Map property ie:
public class MyCommand {
private final Map<String, Object> properties = new HashMap<String, Object>();
public Map<String, Object> getProperties() { return properties; }
// setter optional
}
Then you can do something like this (not 100% sure on the syntax but it is possible):
Name: <form:input path="properties['name']" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With