我已经继承了Struts 2应用程序,并且尝试对其进行一些增强。要求是一次上传多个文档。每个文档都需要:描述,文档类型,文档类别和所述文档。我的Form类有一个ID表示代表文档也应该关联的父记录,还有一个ArrayList of Document对象。由于我不知道将要上传多少文档,并且每个父对象的文档数量都不同,因此我需要一种动态允许文档集上传的方法。
我的表单属性最终看起来像这样:
documents[0].category
documents[0].inFile
documents[0].docType
documents[0].description
documents[1].category
documents[1].inFile
documents[1].docType
documents[1].description
但是,当执行“保存”操作时,我的文档集合始终为null。
我的表单类是通过这种方式设置的。
public final class JobdocuwareMultiForm extends BaseForm {
/**
*
*/
private static final long serialVersionUID = 1L;
private String _action;
private java.lang.String jobhdrId;
private ArrayList<JobdocForm> documents;
public String getAction() {
return _action;
}
public void setAction(String _action) {
this._action = _action;
}
private void addErrorIfBlank(ActionErrors errors, String fieldName,
String fieldValue, String message) {
if (fieldValue == null || fieldValue.trim().equals("")) {
errors.add(fieldName, new ActionMessage(message));
}
}
public void setDocuments(ArrayList<JobdocForm> documents){
this.documents = documents;
}
public JobdocForm getDocument(int index){
return documents.get(index);
}
public ArrayList<JobdocForm> getDocuments(){
return documents;
}
public String getJobhdrId () { return jobhdrId; }
public void setJobhdrId (String jobhdrId) { this.jobhdrId = jobhdrId; }
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
// do not vaildate if Delete Action
if (_action == null || _action.equals("Delete")) return null;
ActionErrors errors = new ActionErrors();
for (JobdocForm doc:documents) {
addErrorIfBlank(errors, "description", doc.getDescription(), "error.description.required");
addErrorIfBlank(errors, "doctype", doc.getDoctype(), "error.documenttype.required");
if (_action.equals("Create")) {
addErrorIfBlank(errors, "infile", doc.getInfile().getFileName(), "error.document.required");
}
}
// Perform validator framework validations
return errors;
}
/**
* Returns an array of keys, representing values that should not be
* populated for the current form instance. Subclasses that override
* this method to provide additional keys to be skipped should be
* sure to call <code>super</code>
*
* @return an array of keys to be skipped
*/
@SuppressWarnings("rawtypes")
protected ArrayList keysToSkip() {
ArrayList keysToSkip = super.keysToSkip();
return keysToSkip;
}
}
尝试找到解决方案时,我遇到了这个post,其中提到“应该确保在构造函数和reset方法中初始化List”。否则,您将得到意想不到的结果。由于我还是Struts 2的新手,所以我随时尝试添加
JobdocuwareMultiForm(){
super();
this.docuemnts = new ArrayList<JobdocuwareForm>();
}
它根本无法加载表格。我或者被重定向到我的登录屏幕,或者一直有用的An unexpected error has occured
消息。放置断点无济于事,因为它似乎无法进入Action类。