没有struts标签的struts 2文件上传 [英] struts 2 file upload without struts tags
问题描述
< s:form action =doUploadmethod =post enctype =multipart / form-data>
< s:file name =uploadlabel =File/>
< s:submit />
< / s:form>
利用 struts 2提供的文件上传功能?如果没有struts 2标签,我可以实现相同的功能吗?如果是的话,我可以知道约定需要在行动中或在配置文件中实现相同吗?
, 您可以。然后动作使用的配置和约定保持不变。
如果使用 在form标签中,应该放置< form
标签,那么您应该将动作属性值与映射到动作的路径一起放置。有关动作映射程序如何工作的详细信息,并将路径翻译为 ActionMapping
,请参阅 here。
enctype =multipart / form-data
,所以Struts可以把http请求包装到 MultipartRequestWrapper
并解析表单数据。然后 fileUpload
拦截器将参数添加到 params
拦截器将文件属性注入处理上传的操作。
$ b code> input 标签应该对应于属性 File
类型的名称。它由上面的两个拦截器使用,最后通过OGNL注入对象。
如果需要多个文件上传,则应该更改属性类型以处理集合对象。看起来这是在示例中完成的。
Do I need to make use the struts tags
<s:form action="doUpload" method="post" enctype="multipart/form-data">
<s:file name="upload" label="File"/>
<s:submit/>
</s:form>
to make use of File upload functionality that struts 2 is providing ? Can I achieve the same functionality without struts 2 tags ? if yes can i know the conventions need to be incorporated in action or in configuration files to achieve the same ?
Shortly, yes, you can. Then the configuration and conventions used by the action remains the same.
If you use the <form
tag then you should place the action attribute value with the path that map to the action. More about how the action mapper works and translates the path to the ActionMapping
see here.
In the form tag you should place the enctype="multipart/form-data"
, so the Struts is able to wrap the http request to the MultipartRequestWrapper
and parse form data. Then fileUpload
interceptor adds parameters to the action context needed for the params
interceptor to inject the file properties to the action that handles uploading.
The name of the input
tag should correspond the name of the property File
type. It's used by both interceptors above and finally the object is injected via OGNL.
If you need multiple files upload then you should change the properties type to be able to handle a collection of objects. Look like it's done in this example.
这篇关于没有struts标签的struts 2文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!