Spring MVC将http params形式化为对象映射成员变量的模型

时间:2014-06-04 10:15:08

标签: java spring spring-mvc freemarker

我努力为这个问题命名。我试图解决的问题与描述here的问题非常相似,但是我想提交http请求参数,而不是bean列表,它代表模型对象中的一个映射,让框架(Spring)保重从http请求参数构建映射。有人可以建议最好的做法/最干净的方法吗?任何帮助将不胜感激。

目前我传递两个String数组,然后在保存到模型对象之前将它们转换为地图。我认为必须有一个更好的方法来做到这一点。

我使用Spring MVC和Freemarker进行视图渲染。

说明性代码:

模型对象:

public class Foo {
    private Map<String, String> barMap;
    // other member variables...
}

查看fremarker模板:

<#list foo.barMap?keys as currKey>
    <tr id="barList_${currKey_index}">
        <td><input name="key" type="text" value="${currKey}"/></td>
        <td><input name="value" type="text" value="${foo.barMap[currKey]}"/></td>
    </tr>
</#list>

控制器:

@RequestMapping(value = "/foo", method = RequestMethod.POST)
public String foo (Model model,
                   @RequestParam(value="key", required=false) String[] keys, 
                   @RequestParam(value="value", required=false) String[] values) {

    Foo foo = new Foo();
    Map<String, String> barMap = new HashMap<String, String>();

    if (keys != null && values != null && keys.length == values.length) {
        for (int i = 0; i < keys.length; i++) {
            if (keys[i] != null 
                && !keys[i].isEmpty() 
                && values[i] != null 
                && !values[i].isEmpty()) {

                barMap.put(keys[i], values[i]);
            }
        }
    }

    foo.setBarMap(map);

    return WebConstants.VIEW_FOO;
}

3 个答案:

答案 0 :(得分:5)

直接使用map来绑定

@RequestMapping(value = "/foo", method = RequestMethod.POST)
public String foo (@RequestParam Map<String,String> allRequestParams ,Model model){
}

并在Foo中为地图添加getter和setter。

然后地图会自动填充

答案 1 :(得分:1)

直接转换为Foo:

@RequestMapping(value = "/foo", method = RequestMethod.POST)
public String foo (@RequestBody Foo foo){
}
chang ethe形成了类似的东西

<input name="${currKey}" type="text" value="${foo.barMap[currKey]}"/>

你需要在表单中匹配foo结构,你可以通过调试来查看@ResponseBody Foo

的outtputed json所需的内容

答案 2 :(得分:1)

感谢Karibasappa G C和NimChimsky的回答帮助了我很多,并指出了我正确的方向 - 我对你们两个都赞不绝口。我最终使用了Spring的@ModelAtribute注释,这使得这个非常简单和最少的额外代码。请注意,html输入字段需要采用特定格式,name属性的格式必须为mapName [mapKey],value属性是您希望存储在地图中的关键mapName [mapKey]的值。

示例解决方案:

模型对象:

public class Foo {
    private Map<String, String> barMap;

    public Map<String, String> getTagMap() {
        return tagMap;
    }

    public void setTagMap(Map<String, String> tagMap) {
        this.tagMap = tagMap;
    }
}

查看Freemarker模板:

<#if foo ??>
    <#if foo.barMap ??>
        <#list foo.barMap?keys as barKey>
            <tr>
                <td>
                    <!-- this input field is for display purposes only -->
                    <input name="tagKey" type="text" value="${tagKey}"/>
                </td>
                <td>
                    <!-- this input field populates the map -->
                    <input name="tagMap[${tagKey}]" type="text" value="${foo.tagMap[tagKey]}"/>
                </td>
            </tr>
        </#list>
    </#if>
</#if>

控制器:

@RequestMapping(value = "/foo", method = RequestMethod.POST)
public String update (Model model, @ModelAttribute("foo") Foo foo) {
    // other code omitted
    return "foo";
}