Spring表单命令可以是Map吗? [英] Can a Spring form command be a Map?

查看:101
本文介绍了Spring表单命令可以是Map吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring表单命令可以是Map吗?我通过扩展HashMap并使用 ['property'] 表示法引用属性来使我的命令成为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.

命令:

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?

这是不允许的还是我的语法不正确?

Is this not allowed or do I have incorrect syntax?

推荐答案

Springn MVC命令需要使用JavaBeans命名conventins(即getXXX()和setXXX()),所以不能为此使用地图。

Springn MVC commands need to use JavaBeans naming conventins (ie getXXX() and setXXX()) so no you can't use a map for that.

另一种方法是让一个具有单一Map属性的bean,即:

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
}

然后你可以这样做(不是100%肯定语法,但有可能):

Then you can do something like this (not 100% sure on the syntax but it is possible):

Name: <form:input path="properties['name']" />

这篇关于Spring表单命令可以是Map吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆