Reputation: 430
This registration form contains a file upload and textfields. Using Struts2 how do we submit the form parameters and also upload the file?
1. register.jsp
:
<s:form action = "Register" enctype="multipart/form-data" method="post">
<s:textfield name="uname" label = "User Name"/>
<s:password name ="password" label = "Password"/>
<s:file name="fileUpload" label="Select a File to upload" size="40" />
<s:submit/>
</s:form>
2. RegisterAction
:
package user.action;
import java.io.File;
public class RegisterAction
{
private String uname,password;
private File fileUpload;
private String fileUploadContentType;
private String fileUploadFileName;
//getter setters
public String execute() throws Exception
{
return "success";
}
}
3. struts.xml
:
</action>
<action name="Register" class="user.action.RegisterAction">
<interceptor-ref name="fileUpload">
<param name="allowedTypes">text/plain</param>
<param name="maximumSize">10240</param>
</interceptor-ref>
<result name="success">registered.jsp</result>
</action>
4. registered.jsp
:
User Name : <s:property value="uname"/>
File Name : <s:property value="fileUploadFileName"/>
Content Type : <s:property value="fileUploadContentType"/>
File : <s:property value="fileUpload"/>
User Name <s:property value="uname"/>
is not giving any output in
registered.jsp
. Should we use Apache file upload to parse the request to get the textfields value?
Upvotes: 2
Views: 4264
Reputation: 430
Thanks Friends for looking into the problem. I have got the answer. Actually I had done a mistake while adding the interceptor. I had missed
<interceptor-ref name="defaultStack"></interceptor-ref>
so my new action after change should be
<action name="Register" class="user.action.RegisterAction">
<interceptor-ref name="fileUpload">
<param name="allowedTypes">text/plain</param>
<param name="maximumSize">10240</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="success">registered.jsp</result>
</action>
Mr Roman is correct that we should add the file upload interceptor only if we need to override. If we don't still it will upload.
Upvotes: 0
Reputation: 1
No, you shouldn't. The Apache file upload is part of Struts2. The fileUpload
interceptor is already included to the defaultStack
, so you don't need to reference it in the action configuration. If you want to override parameters of this interceptor then
<action name="Register" class="user.action.RegisterAction">
<interceptor-ref name="defaultStack">
<param name="fileUpload.allowedTypes">text/plain</param>
<param name="fileUpload.maximumSize">10240</param>
</interceptor-ref>
<result name="success">registered.jsp</result>
</action>
As long as the required libraries are added to your project you will be able to take advantage of of the Struts 2 fileUpload capability.
Upvotes: 1
Reputation: 5749
check you setter methods local variable and in registered.jsp try this change
User Name <s:property value="uname"/>
with
<s:label name="xyz" value="%{uname}" />
Upvotes: 0