在Spring的控制器方法中使用哪个签名

在Spring的控制器方法中使用哪个签名

本文介绍了在Spring的控制器方法中使用哪个签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在定义Spring的Controller方法时有很多可能的签名.我很困惑应该在哪种情况下使用哪一个.例如,我有以下情况.我必须将文件上传到服务器,我已经为此写了一张表格下面是HTML

There are lots of possible signature while defining Spring's Controller method. I'm confused which one should i use and in which circumstances.For example, I have below scenario.I have to upload a file to server i have written a form for thatBelow is HTML

<form action="uploadimage?id=${pencil.id}" method="post">
<table border="0">
    <tr>
        <td style="color:maroon;font-weight:bold;">Change/Upload Image</td>
        <td><input type="file" name="image" /></td>
        <td><input type="submit" value="upload" /></td>
        <td><input type="hidden" name="pencilid" value="${pencil.id}" /></td>
    </tr>
</table>
</form>

为此,我正在编写控制器方法.使用@ModelAttribute时会引发异常,表示无法将bean MaltipartFile实例化为其接口,而使用@RequestParam时则会返回400错误代码

For this I'm writing controller method. While using @ModelAttribute it throws exception saying can't instantiate bean MaltipartFile as its an Interface and while using @RequestParam it returns 400 error code

使用@requestParam

using @requestParam

@RequestMapping(value="/uploadimage",method=RequestMethod.POST)
public ModelAndView uploadImage(@RequestParam ("pencilid") String id,@RequestParam("file") MultipartFile file)
{
    System.out.println("In Controller");
    Pencil pencil=null;
    PencilService pencilService=ServiceFactory.getPencilService();
    pencil=pencilService.getPencil(Integer.parseInt(id));
    ModelAndView model= new ModelAndView("pencilview","pencil",pencil);
    model.addObject("id",id);
    //MultipartFile file=(MultipartFile)param.get("image");
    System.out.println(file.getName());
    return model;
}

使用@ModelAttribute

Using @ModelAttribute

@RequestMapping(value="/uploadimage",method=RequestMethod.POST)
public ModelAndView uploadImage(@ModelAttribute ("pencilid") String id,@ModelAttribute("file") MultipartFile file)
{
    System.out.println("In Controller");
    Pencil pencil=null;
    PencilService pencilService=ServiceFactory.getPencilService();
    pencil=pencilService.getPencil(Integer.parseInt(id));
    ModelAndView model= new ModelAndView("pencilview","pencil",pencil);
    model.addObject("id",id);
    //MultipartFile file=(MultipartFile)param.get("image");
    System.out.println(file.getName());
    return model;
}

请让我知道我误会了哪里在我的Sping-servlet.xml中,我已将multipartResolver定义为

Kindly let me know where i'm mistakingin my Sping-servlet.xml i have defined multipartResolver as

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

     <!-- setting maximum upload size -->
    <property name="maxUploadSize" value="100000" />

</bean>

让我知道是否还有其他要求.

Let me know if anything else is required.

推荐答案

我认为您的表单应具有如下的属性enctype:

i think your form should have an attribute enctype like the following :

 enctype="multipart/form-data"

如果要上传文件,请对文件和其他具有的表单字段使用@RequestParam,如下所示:

if you are uploading file , you Use the @RequestParam for your file and for the other form fields that you have , like the following :

 @RequestMapping(value="/upload", method=RequestMethod.POST)
    public @ResponseBody String handleFileUpload(@RequestParam("name") String name,
            @RequestParam("file") MultipartFile file){

    }

在此示例中,该示例来自spring 文档handleFileUpload采用两个参数,第一个是普通的String name,即普通字段,第二个是file MultipartFile,即您要上传的文件.

in this example that is from the spring documentation , the handleFileUpload takes two parameters , the first is a normal String name , that is a normal field , the second is a file MultipartFile that is the file you want to upload .

希望有帮助.

这篇关于在Spring的控制器方法中使用哪个签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 04:31