Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring窗体命令可以是映射吗?_Java_Spring_Spring Mvc - Fatal编程技术网

Java Spring窗体命令可以是映射吗?

Java Spring窗体命令可以是映射吗?,java,spring,spring-mvc,Java,Spring,Spring Mvc,Spring窗体命令可以是映射吗?我通过扩展HashMap使我的命令成为映射,并使用['property']符号引用属性,但它不起作用 命令: public class MyCommand extends HashMap<String, Object> { } 这是不允许的,还是我的语法不正确?Springn MVC命令需要使用JavaBeans命名协议(即getXXX()和setXXX()),所以不,您不能使用映射 一种替代方法是使用一个具有单个映射属性的bean,即: publ

Spring窗体命令可以是映射吗?我通过扩展HashMap使我的命令成为映射,并使用
['property']
符号引用属性,但它不起作用

命令:

public class MyCommand extends HashMap<String, Object> {
}

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

Springn MVC命令需要使用JavaBeans命名协议(即getXXX()和setXXX()),所以不,您不能使用映射

一种替代方法是使用一个具有单个映射属性的bean,即:

public class MyCommand {
  private final Map<String, Object> properties = new HashMap<String, Object>();

  public Map<String, Object> getProperties() { return properties; }
  // setter optional
}
公共类MyCommand{
私有最终映射属性=new HashMap();
公共映射getProperties(){return properties;}
//设置器可选
}
然后您可以这样做(语法不是100%确定,但这是可能的):

名称:

是的,它可以,但是。。。您需要将其引用为

e、 g


好的,我有一个适合我的解决方案,我使用MapUtils.lazyMap

//我的根域

    public class StandardDomain {

        private Map<String, AnotherDomainObj> templateMap= MapUtils.lazyMap(new HashMap<String, AnotherDomainObj>(),FactoryUtils.instantiateFactory(AnotherDomainObj.class));

        public Map<String, AnotherDomainObj> getTemplateContentMap() {
            return templateMap;
        }

        public void setTemplateContentMap(Map<String, AnotherDomainObj > templateMap) {
            templateMap = templateMap;
        }

    }
//在我的JSP中

<input type="text" value="testthis" name="templateMap['keyName'].propertyValue"/>

结合cletus和dbell的答案,我实际上可以让它工作,并希望与您共享解决方案(包括提交表单时绑定值,cletus解决方案报告了一个缺陷)

您不能直接使用映射作为命令,但是有另一个域对象需要包装惰性映射

public class SettingsInformation {

    private Map<String, SettingsValue> settingsMap= MapUtils.lazyMap(new HashMap<String, SettingsValue>(),FactoryUtils.instantiateFactory(SettingsValue.class));

    public Map<String, SettingsValue> getSettingsMap() {
        return settingsMap;
    }

    public void setSettingsMap(Map<String, SettingsValue > settingsMap) {
        this.settingsMap = settingsMap;
    }

}
提供模型的控制器方法如下所示:

    @RequestMapping(value="/settings", method=RequestMethod.GET)
public ModelAndView showSettings() {

    ModelAndView modelAndView = new ModelAndView("settings");

    SettingsDTO settingsDTO = settingsService.getSettings();
    Map<String, String> settings = settingsDTO.getSettings();

    SettingsInformation settingsInformation = new SettingsInformation();

    for (Entry<String, String> settingsEntry : settings.entrySet()) {
        SettingsValue settingsValue = new SettingsValue(settingsEntry.getValue());
        settingsInformation.getSettingsMap().put(settingsEntry.getKey(), settingsValue);
    }

    modelAndView.addObject("settings", settingsInformation);

    return modelAndView;
}
我验证了SettingsInformationbean实际上是用表单中的值填充的


谢谢你帮我做这件事;如果您有任何问题,请随时提问。

您的轴是正确的,但地图属性实际上没有绑定。问题是生成的代码类似于。因此,当您提交此表单时,没有任何内容将正确的值放入属性中。可以在JSP中绑定映射字符串值。map/object的整数值可以绑定吗?看起来语法现在对maps有效,至少在Spring 3.0.7Map中字符串值可以在JSP中绑定。映射/对象的整数值可以绑定吗?映射字符串值可以在JSP中绑定。可以绑定贴图/对象的整数值吗?
public class AnotherDomainObj  {

    String propertyValue="";

        public String getPropertyValue() {
           return propertyValue;
        }

        public void setPropertyValue(String propertyValue) {
           this.propertyValue = propertyValue;
     }



}
<input type="text" value="testthis" name="templateMap['keyName'].propertyValue"/>
public class SettingsInformation {

    private Map<String, SettingsValue> settingsMap= MapUtils.lazyMap(new HashMap<String, SettingsValue>(),FactoryUtils.instantiateFactory(SettingsValue.class));

    public Map<String, SettingsValue> getSettingsMap() {
        return settingsMap;
    }

    public void setSettingsMap(Map<String, SettingsValue > settingsMap) {
        this.settingsMap = settingsMap;
    }

}
public class SettingsValue {

private String value;

public SettingsValue(String value) {
    this.value = value;
}


public SettingsValue() {

}

public String getValue() {

    return value;
}

public void setValue(String propertyValue) {

    this.value = propertyValue;
}
    @RequestMapping(value="/settings", method=RequestMethod.GET)
public ModelAndView showSettings() {

    ModelAndView modelAndView = new ModelAndView("settings");

    SettingsDTO settingsDTO = settingsService.getSettings();
    Map<String, String> settings = settingsDTO.getSettings();

    SettingsInformation settingsInformation = new SettingsInformation();

    for (Entry<String, String> settingsEntry : settings.entrySet()) {
        SettingsValue settingsValue = new SettingsValue(settingsEntry.getValue());
        settingsInformation.getSettingsMap().put(settingsEntry.getKey(), settingsValue);
    }

    modelAndView.addObject("settings", settingsInformation);

    return modelAndView;
}
<form:form action="${actionUrl}" commandName="settings">
        <form:input path="settingsMap['exampleKey'].value"/>
        <input type="submit" value="<fmt:message key="settings.save"/>"/>
</form:form>
@RequestMapping(value="/settings", method=RequestMethod.POST)
public ModelAndView updateSettings(@ModelAttribute(value="settings") SettingsInformation settings) {
[...]
}