春季文件上传混合形式 [英] Spring file upload in a mixed form
问题描述
我已经有了下面的实体:
@Entity
@RooJavaBean
@RooToString
@RooEntity
公共类SelniumFile {
@ManyToOne(targetEntity = ShowCase.class)
@JoinColumn
私人ShowCase showcase;
@Lob
@Basic(fetch = FetchType.LAZY)
private byte [] file;
@NotNull
私人字符串名称;
$ b 但我不确定如何在视图/控制器端实现它。我可以自由地将< form:input>
等spring-form标签与< input type = file ...> / code>?
我已经看到了漂亮的,但仍然需要一些帮助,以将其应用于我的具体情况。
更新:我想我的问题是严重的公式化。我想要做的是创建一个spring
我在老的spring文档中找到了一个很好的解释,并将其应用到Spring 3.0的新MVC中。基本上这意味着你需要在你的控制器@InitBinder方法中注册一个PropertyEditor。之后,所有内容都将按照预期运行(只要您已将MultiPartResolver添加到上下文并设置正确的表单编码)。
这里是我的示例:
$ $ $ $ $ $ $ $ c $ @ $ @RequestMapping(/ scriptfile / **)
@Controller
public class ScriptFileController {
//我们需要一个特殊的属性编辑器,知道如何将数据
//从请求绑定到一个byte []
@InitBinder
public void initBinder(WebDataBinder binder)
{
binder.registerCustomEditor(byte []。class,new ByteArrayMultipartFileEditor());
$ b @RequestMapping(value =/ scriptfile,method = RequestMethod.POST)
public String create(@Valid ScriptFile scriptFile,BindingResult result,ModelMap modelMap){
if(scriptFile == null)抛出新的IllegalArgumentException(需要scriptFile);
if(result.hasErrors()){
modelMap.addAttribute(scriptFile,scriptFile);
modelMap.addAttribute(showcases,ShowCase.findAllShowCases());
返回scriptfile / create;
}
scriptFile.persist();
返回redirect:/ scriptfile /+ scriptFile.getId();
}
}
I want to upload a file to my spring 3.0 applicatoin (created with roo).
I already have the following entity:
@Entity
@RooJavaBean
@RooToString
@RooEntity
public class SelniumFile {
@ManyToOne(targetEntity = ShowCase.class)
@JoinColumn
private ShowCase showcase;
@Lob
@Basic(fetch = FetchType.LAZY)
private byte[] file;
@NotNull
private String name;
}
But I am not sure how to implement it on the view/controller side. Can I freely mix spring-form tags like <form:input>
with normal tags like <input type=file ...>
?
I have seen the nice multipart upload section in the MVC-Documentation but still need a little help to apply it to my specific case.
Update: I think my question was badly formulated. What I wanted to do is create a spring
I found a very good explanation on how to do it in the older spring documentation and applied it to the new Spring 3.0 MVC. Basically this means you need to register a PropertyEditor in your controllers @InitBinder method. Afterwards everything will behave as expected (provided you have added MultiPartResolver to the context and set the correct form encoding). Here is my sample:
@RequestMapping("/scriptfile/**")
@Controller
public class ScriptFileController {
//we need a special property-editor that knows how to bind the data
//from the request to a byte[]
@InitBinder
public void initBinder(WebDataBinder binder)
{
binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
}
@RequestMapping(value = "/scriptfile", method = RequestMethod.POST)
public String create(@Valid ScriptFile scriptFile, BindingResult result, ModelMap modelMap) {
if (scriptFile == null) throw new IllegalArgumentException("A scriptFile is required");
if (result.hasErrors()) {
modelMap.addAttribute("scriptFile", scriptFile);
modelMap.addAttribute("showcases", ShowCase.findAllShowCases());
return "scriptfile/create";
}
scriptFile.persist();
return "redirect:/scriptfile/" + scriptFile.getId();
}
}
这篇关于春季文件上传混合形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!