我想将文件上传到我的 spring 3.0 应用程序(使用 roo 创建)。
我已经拥有以下实体:
@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;
}
但我不确定如何在视图/控制器端实现它。我可以自由混合弹簧形式的标签吗?<form:input>
与普通标签一样<input type=file ...>
?
我见过好看的分段上传部分在 MVC 文档中,但仍然需要一些帮助才能将其应用到我的具体案例中。
更新:我认为我的问题表述得很糟糕。我想做的是创造一个春天
我在旧的 Spring 文档中找到了关于如何执行此操作的非常好的解释,并将其应用到新的 Spring 3.0 MVC 中。基本上这意味着您需要在控制器 @InitBinder 方法中注册一个 PropertyEditor。之后,一切都会按预期运行(前提是您已将 MultiPartResolver 添加到上下文并设置正确的表单编码)。
这是我的样本:
@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();
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)