当我尝试在struts2中上传文件时,我在视图中看到了验证错误:字段“上传”的字段值无效。 在JBoss控制台中,我得到:
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
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()");
}
我将非常感谢你的帮助。
答案 0 :(得分:2)
你的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">
<sj:submit targets="messagesDiv"/>
</div>
</s:form>
</div>
或者:
<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>