2

我正在为表单上的文件上传而苦苦挣扎:使用 spring roo 更新。

对于创建部分,我在这里使用了 Jose Delgado 提供的 form:multi 标签。自定义 form:multi 标签将 enctype="multipart/form-data" 添加到表单中,效果很好。

问题是当您想为更新表单提供文件上传功能时。Spring Roo(也许是 spring mvc,我不知道)默认情况下会将 enctype="application/x-www-form-urlencoded" 设置为更新表单(form:update 标签)。如果我在上传表单中将 enctype 属性设置为 enctype="multipart/form-data",那么在提交表单时服务器将执行控制器的“create”方法而不是“udpate”方法...

知道我们如何(简单地)解决这个问题吗?我已经花了相当长的时间在这上面,但我发现自己没有灵感了(也许是因为这是一天的结束,也是:)。

谢谢你的帮助,

亲切的问候

4

1 回答 1

3

OK……看来RequestMapping有点问题。

无论出于何种原因,当 form:update 标记中的 multipart 属性设置为“true”时,方法参数设置为“POST”。

作为一种解决方法,我在 create 方法的开头检查了 _method 参数。如果它设置为“PUT”,我返回更新方法的值。

    @RequestMapping(method = RequestMethod.POST, produces = "text/html")
    public String create(@Valid ActionRequest actionRequest, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {

        // Work around dispatcher bug: if the multipart attribute of the form is set to true,
        // submission of the update form routes to create method
        String toto = httpServletRequest.getParameter("_method");

        if(httpServletRequest.getParameter("_method").equals("PUT")){
            return this.update(actionRequest,bindingResult,uiModel,httpServletRequest);
        }
   ...
}
于 2012-08-29T12:25:09.887 回答