2009-04-09 25 views
9

春季表單命令是一個Map嗎?我通過擴展HashMap並通過使用['property']表示法引用屬性來使我的命令成爲Map,但它不起作用。春季表單命令可以是一個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? 

這是不允許的或我有不正確的語法?

回答

11

Springn MVC命令需要使用JavaBeans命名conventins(即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 
} 

然後,你可以做這樣的事情(肯定不是100%的語法,但它是可能的):

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

你的sintaxis是正確的,但是地圖屬性並沒有實際綁定。問題是生成的代碼類似於。所以,當你提交這個表單時,沒有什麼東西把正確的價值放在屬性中。 – sinuhepop 2010-06-03 16:34:00

+0

Map字符串值可以在JSP中綁定。地圖/對象的整數值可以綁定嗎? – 2012-04-04 14:06:26

+0

看起來像語法現在適用於地圖,至少在Spring 3.0.7 – altumano 2012-06-21 13:54:29

-1

是的,但它可以...您需要將其作爲參考<form:input path="name[index].key|value"/> 例如

<form:input path="name[0].value"/>

+0

什麼是0或索引? – 2009-04-09 23:31:20

1

好吧,我有一個對我工作的解決方案,我用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; 
     } 

    } 

//我的第二個域

public class AnotherDomainObj { 

    String propertyValue=""; 

     public String getPropertyValue() { 
      return propertyValue; 
     } 

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



} 

//在我的JSP

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

結合克萊圖斯的答案,dbell我居然可以讓它工作,並願與您(包括提交表單,報告克萊圖斯解決方案的一個缺陷,當值的結合)

不能共享的解決方案直接使用的地圖作爲命令,然而有一個需要包裝一個懶惰地圖

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; 
    } 

} 

SettingsValue另一個域對象是實際包的值的類。

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) { 
[...] 
} 

我驗證了SettingsInformation bean實際上是用表單中的值填充的。

感謝您幫助我解決這個問題;如果你有任何問題隨時問。

相關問題