Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a Spring form command be a Map?

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?

like image 432
Steve Kuo Avatar asked Apr 09 '09 23:04

Steve Kuo


People also ask

What is spring form path?

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.

How spring form is used in JSP?

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.

What is command object in Spring MVC?

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.


1 Answers

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']" />
like image 63
cletus Avatar answered Nov 05 '22 05:11

cletus