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.
offcorse you can,you can use HTML tags,
<s:form action="fileUploadAction" method="post" enctype="multipart/form-data">
<input type="file" name="userImage" id="userImage"/>
</s:form>
you can use struts2-inceptors to do things for you like this.
<interceptors>
<interceptor name="mylogging"
class="com.alw.controller.MyLoggingInterceptor">
</interceptor>
<interceptor-stack name="loggingStack">
<interceptor-ref name="mylogging" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<action name="fileUploadAction"
class="xxx.xxx.FileUploadAction" method="filterUploadFile">
<interceptor-ref name="fileUpload">
<param name="maximumSize">2097152</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="success">JSP_Pagesxxx.jsp</result/>
</action>
and to perform business logic in your action class you need this.
public class FileUploadAction extends ActionSupport implements ServletRequestAware
{
private File userImage;
private String userImageContentType;
private String userImageFileName;
public String filterUploadFile()
{
if(UserImageFileName()!=null)
{
// perform your business logic
}
}
}
来源:https://stackoverflow.com/questions/16619507/struts-2-file-upload-without-struts-tags