質問

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 maps to the action. More about how the action mapper works and translates the path to the ActionMapper interface.

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 class 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
        }
         }

}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top