Uploading a file using <s: file> in Struts2

I am working on struts2 application. I have the following in my jsp (say main.jsp) -

<s:file name="sampleDocument"/>
<s:submit action="uploadSample" value="Upload" />

      

See how my struts.xml is handling the action -

<action name=" uploadSample " class=" UploadFiles">                 
        <result name="success"> /main.jsp</result>
        <result name="input"> /error.jsp</result>
</action> 

      

Everything works well. As the file downloads. But as you saw I am calling the same jsp page (ie main.jsp) on SUCCESS. Its coming, but the <s:file>

text box is blank. I mean there is no filename that I am looking at before uploading. Does anyone know how to get this?

0


a source to share


3 answers


You need your action properties that reflect the parameter names of your JSP. Check out this documentation for an explanation of what properties to look for.



0


a source


you must define getter and setter functions for fileName. This ensures that the value is populated in the view after it returns from the activity.



0


a source


The item value <input type="file"/>

is cleared by default for security reasons.

See How to set a value for file input in HTML?

0


a source







All Articles