简体   繁体   English

Struts2文件上传错误

[英]Struts2 file upload errors

I am a using struts2 file upload and my action class contains 3 private fileds with getter and setters 我正在使用struts2文件上传,并且我的操作类包含3个带有getter和setter的私有文件

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? 我们仅传递文件作为参数并将其绑定到myFile ,那么应用程序如何获取文件名和内容类型?

  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. 每当我使用myFileVariableName +“ FileName”(如果文件变量是myFile,文件名变量是myFileFileName,如果文件是xxx,那么文件名是xxxFileName),如果我对此格式进行了任何更改(即, ,myFileVariableName +“ FileName”),它为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". 要获取内容类型,我应该使用jst“ contentType”或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. 我假设,如果我使用单独的bean存储我的请求变量,则所有参数都将绑定到该bean变量。 But in the case of file upload only the file variable ie, myFile in this example only get and set in the bean. 但是在文件上传的情况下,仅文件变量,即本示例中的myFile仅在bean中获取和设置。 fileFileName and contentType are null. fileFileName和contentType为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. 如果直接在操作类中声明这些变量,则将获得这些值,但是每当使用单独的bean时,只有File变量可以获取和设置,而其他两个则为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. 如果使用ModelDriven,则发生相同的情况,我只能获取File变量,而其他两个变量为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. 我只是在struts.xml中扩展了“ struts-default”,并且没有用于文件上传的单独配置,因为它在我的问题中没有显示任何效果。

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. 对于文件上传的操作类 ,声明一个File变量来存储用户上传的文件,两个String变量来存储文件名和内容类型。 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. fileUpload拦截器将通过设置' X'ContentType()和设置' X'FileName() 自动注入上传的文件详细信息 ,确保方法名称拼写正确。

The file upload function is depends on the “fileUpload Interceptor“, make sure it is included in the Action's stack. 文件上传功能取决于“ fileUpload Interceptor”,请确保将其包含在操作的堆栈中。 The lucky is, the default stack is already includes the “fileUpload Interceptor“. 幸运的是,默认堆栈已包含“ fileUpload Interceptor”。

The fields userImageContentType and userImageFileName are optional. 字段userImageContentTypeuserImageFileName是可选的。 If setter method of these fields are provided, struts2 will set the data. 如果提供这些字段的setter方法 ,Struts2的将设置数据。 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 . 名称应为ContentTypeFileName

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. 在Struts 2中获取设置行为将值分配给变量,而不是属性值。 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. 许多Struts 2开发人员认为, set标记var =” msg”将通过setMsg()方法将值分配给关联的动作类。

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. 这是错误的,set标记将不会调用setMsg()方法,它将仅将“值”分配给名为“ msg”的变量,而不是操作的属性值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM