0
votes

I am a using struts2 file upload and my action class contains 3 private fileds with getter and setters

private File myFile;
private String myFileFileName;
private String myFileContentType;

I have some douts to clarify

  1. We are passing only the file as parameter and bind it to the myFile, So how the application getting the file name and content type?

  2. whenever I use myFileVariableName + "FileName" (if the file variable is myFile then file name variable is myFileFileName, if file is xxx, then file name is xxxFileName), I am getting the output, if i make any change to this format (ie,myFileVariableName + "FileName"), It getting null. Is it mandatory to use this format? Can I change it to any name I desire? If so, then how?

  3. To get the content type, I should use jst "contentType" or myfileVariableName + "contentType". Is it also mandatory?

  4. I assume, if I use a separate bean to store my request variables, all the parameters is bind to that bean variable. But in the case of file upload only the file variable ie, myFile in this example only get and set in the bean. fileFileName and contentType are null. If I declare these variables directly in my action class, then I get the values, but whenever I use a separate bean, only File variable can get and set, and the other two are null. Why?

  5. If I use ModelDriven, the the same case happening, I can only get File variable and the other two variables are null. why?

I am only extending the "struts-default" in my struts.xml and no separate config for file upload, since it dont show any effect in my questions.

1

1 Answers

1
votes

Action class for the file upload, declare a File variable to store the user uploaded file, two String variables to store the file name and content type. The fileUpload interceptor will auto inject the uploaded file detail via set 'X' ContentType() and set 'X' FileName(), make sure the method name is spell correctly.

The file upload function is depends on the “fileUpload Interceptor“, make sure it is included in the Action’s stack. The lucky is, the default stack is already includes the “fileUpload Interceptor“.

The fields userImageContentType and userImageFileName are optional. If setter method of these fields are provided, struts2 will set the data. This is just to get some extra information of uploaded file. Also follow the naming standard if you providing the content type and file name string. The name should be ContentType and FileName.

For example if the file attribute in action file is private File uploadedFile, the content type will be uploadedFileContentType and file name uploadedFileFileName.

Get Set Behaviour in Struts 2 : Assign value to a variable, not property value. For example,

public class SetTagAction extends ActionSupport{

private String msg;

public String setMsg(String msg) {
    this.msg = msg;
}
<s:set var="msg" value="%{'this is a message'}" />

Many Struts 2 developers thought that the set tag var=”msg” will assign the value to the associated action class via setMsg() method.

This is wrong, the set tag will not call the setMsg() method, it will only assign the “value” to a variable named “msg“, not the action’s property value.