本文介绍了struts2无法上传文件.无效的栏位值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我尝试在struts2中上传文件时,在我的视线中出现验证错误:字段上载"的字段值无效.在JBoss控制台中,我得到:
When I try upload file in struts2 I get in my view a validation error: Invalid field value for field "upload".And in JBoss console I get:
10:37:56,140 WARN [OgnlValueStack] Error setting expression 'upload' with value '[Ljava.lang.String;@17c7b19'
ognl.MethodFailedException: Method "setUpload" failed for object bm.action.content.SaveNewBannerAction@13ebb53 [java.lang.NoSuchMethodException: bm.action.content.SaveNewBannerAction.setUpload([Ljava.lang.String;)]
at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1289)
at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:1478)
at ognl.ObjectPropertyAccessor.setPossibleProperty(ObjectPropertyAccessor.java:85)
at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:162)
...
...
Caused by: java.lang.NoSuchMethodException: bm.action.content.SaveNewBannerAction.setUpload([Ljava.lang.String;)
at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1230)
/-- Encapsulated exception ------------\
java.lang.NoSuchMethodException: bm.action.content.SaveNewBannerAction.setUpload([Ljava.lang.String;)
at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1230)
at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:1478)
at ognl.ObjectPropertyAccessor.setPossibleProperty(ObjectPropertyAccessor.java:85)
at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:162)
我的jsp:
<div class="v4DDfiltering">
<s:fielderror/>
<s:form id="saveNewBannerForm" name="saveNewBannerForm"
action="SaveNewBannerAction" theme="simple" method="POST" enctype="multipart/form-data">
...
<div class="OneFilteringRow">
<s:label for="upload" value="Plik:*" />
<s:file id="upload" name="upload"/>
</div>
...
<div class="OneFilteringRow">
<s:url id="saveNewBannerUrl" action="SaveNewBannerAction" />
<sj:a href="%{saveNewBannerUrl}" targets="messagesDiv"
formIds="saveNewBannerForm">
Save
</sj:a>
</div>
</s:form>
</div>
我的动作类SaveNewBannerAction.java
My action class SaveNewBannerAction.java
public class SaveNewBannerAction extends BaseAction {
private static final long serialVersionUID = 1L;
private static final Log LOG = LogFactory.getLog(SaveNewBannerAction.class);
private boolean validate = true;
private File upload;// The actual file
private String uploadContentType; // The content type of the file
private String uploadFileName; // The uploaded file name
private String fileCaption;// The caption of the file entered by user
...
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getFileCaption() {
return fileCaption;
}
public void setFileCaption(String fileCaption) {
this.fileCaption = fileCaption;
}
@Override
public void validate() {
LOG.info("Enter: validate()");
LOG.info("VALIDATE FIELDS");
...
if(upload == null) {
addFieldError("upload",
"Proszę uzupełnić pole: plik.");
validate = false;
} else {
if(!uploadContentType.contains("image")) {
addFieldError("upload",
"Wprowadzono niepoprawny format pliku. Plik musi być obrazkiem.");
validate = false;
}
}
LOG.info("Exit: validate()");
}
感谢您的帮助.
推荐答案
您的jsp应该是这样的:
Your jsp should like this:
<div class="v4DDfiltering">
<s:fielderror/>
<s:form id="saveNewBannerForm" name="saveNewBannerForm"
action="SaveNewBannerAction" theme="simple" method="POST" enctype="multipart/form-data">
...
<div class="OneFilteringRow">
<s:label for="upload" value="Plik:*" />
<s:file id="upload" name="upload"/>
</div>
...
<div class="OneFilteringRow">
<sj:submit targets="messagesDiv"/>
</div>
</s:form>
</div>
OR:
<div class="v4DDfiltering">
<s:fielderror/>
<s:form id="saveNewBannerForm" name="saveNewBannerForm"
action="SaveNewBannerAction" theme="simple" method="POST" enctype="multipart/form-data">
...
<div class="OneFilteringRow">
<s:label for="upload" value="Plik:*" />
<s:file id="upload" name="upload"/>
</div>
...
</s:form>
<sj:a targets="messagesDiv" formIds="saveNewBannerForm">Save</sj:a>
</div>
这篇关于struts2无法上传文件.无效的栏位值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!