Spring 表单命令可以是 Map 吗?通过扩展 HashMap 并使用 ['property']
符号引用属性,我使我的命令成为一个 Map,但它不起作用。
命令:
public class MyCommand extends HashMap<String, Object> {
}
HTML 格式:
Name: <form:input path="['name']" />
错误结果:
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?
这是不允许的还是我的语法不正确?
最佳答案
Springn MVC 命令需要使用 JavaBeans 命名约定(即 getXXX() 和 setXXX()),所以不可以为此使用映射。
一种替代方法是让 bean 具有单个 Map 属性,即:
public class MyCommand {
private final Map<String, Object> properties = new HashMap<String, Object>();
public Map<String, Object> getProperties() { return properties; }
// setter optional
}
然后你可以做这样的事情(不是 100% 确定语法但它是可能的):
Name: <form:input path="properties['name']" />
关于java - Spring 表单命令可以是 Map 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/736186/