10

Spring フォーム コマンドをマップにすることはできますか? HashMap を拡張してコマンドをマップにし、表記法を使用してプロパティを参照しました['property']が、機能しませんでした。

指示:

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?

これは許可されていませんか、それとも構文が正しくありませんか?

4

4 に答える 4

12

Springn MVC コマンドは、JavaBeans 命名規則 (つまり、getXXX() および setXXX()) を使用する必要があるため、そのためにマップを使用することはできません。

1 つの代替方法は、単一の Map プロパティを持つ 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']" />
于 2009-04-09T23:08:39.630 に答える
3

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

}

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 に実際にフォームの値が入力されていることを確認しました。

これを手伝ってくれてありがとう。ご不明な点がございましたら、お気軽にお問い合わせください。

于 2011-07-18T11:10:01.357 に答える
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;
        }

    }

//私の2番目のドメイン

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"/>
于 2011-03-07T20:56:35.353 に答える
-1

はい、できますが...次のように参照する必要があり<form:input path="name[index].key|value"/> ます

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

于 2009-04-09T23:21:17.183 に答える