我不想立即坚持,但实际上是在会话中,在用户点击“上传并分配到订单”按钮后,我将开始真正坚持。 一些如何neinhe rin Chrome或Firefox附带附件添加事件, 然后 “上传并分配给订单”按钮使所有内容都为空,从而导致异常。
<body>
<h:form id="uploadFormId" enctype="multipart/form-data">
<rich:messages id="messages" style="font-weight:bold; color: DarkBlue; " />
<h:panelGroup id="panelGroupId">
<h:outputText value="Upload New Attachment" style="font-weight:bold" />
<br /><br />
<h:outputText value="Doc Id" />
<h:inputText id="DOCID" label="Doc Id:" value="#{uploadBean.docId}" required="true" style="margin-left: 10pt;"
validatorMessage="Doc Id must be an integer number." >
<!-- <f:validateLength minimum="5" maximum="20" /> -->
<!-- <f:validateRegex pattern="^\d+$" /> only in JSF 2.0 -->
</h:inputText>
<h:message for="DOCID" errorClass="error" />
<br />
<h:outputText value="Language" />
<h:selectOneMenu value="#{uploadBean.language}">
<f:selectItems value="#{uploadBean.languageOptions}"/>
</h:selectOneMenu>
<h:selectBooleanCheckbox id="defaultness_Checkbox" title="defaultness_Checkbox" value="#{uploadBean.defaultness}" style="margin-left: 10pt;" />
<h:outputText value="Is Default?" style="margin-left: 2pt;" />
<br /><br />
</h:panelGroup>
<h:outputText value="Attachment" for="richFileUploadId" />
<rich:fileUpload id="richFileUploadId"
acceptedTypes="pdf"
fileUploadListener="#{uploadBean.fileUploadListener}"
immediateUpload="true"
>
</rich:fileUpload>
<!--
<rich:fileUpload id="richFileUploadId"
acceptedTypes="pdf"
fileUploadListener="#{uploadBean.fileUploadListener}"
immediateUpload="true"
listHeight="50px" listWidth="300px"
allowFlash="false"
addControlLabel="Add file..." clearAllControlLabel="Clear all" clearControlLabel="Clear"
stopEntryControlLabel="Stop process" uploadControlLabel="Upload file"
>
<a4j:support event="onuploadcomplete"
process="panelGroupId"
render="attachmentUploadTableId, messages"
reRender="attachmentUploadTableId, messages"
/>
<f:facet name="label">
<h:outputText value="{_KB}KB from {KB}KB uploaded - {mm}:{ss}" />
</f:facet>
</rich:fileUpload>
-->
<br /><br />
<!-- reRender="attachmentUploadTableId, " -->
<!-- addControlLabel="Add file..." clearAllControlLabel="Clear all" clearControlLabel="Clear"
stopEntryControlLabel="Stop process" uploadControlLabel="Upload file" -->
<a4j:commandLink reRender="attachmentUploadTableId, messages"
process="panelGroupId"
value="Upload and assign to order"
action="#{uploadBean.uploadAssignToOrder}"
style=" border: 1px solid #999;
padding:1px 4px 1px 4px;
color: #333;
background-color: #e7e7e7;
text-decoration: none;
margin-right: 10px;
display: inline;
cursor:pointer; ">
</a4j:commandLink>
<br /><br />
<h:outputText value="Current Attachments" for="attachmentUploadTableId" style="font-weight: bold; " />
<rich:dataTable id="attachmentUploadTableId" value="#{uploadBean.fileList}" var="file">
<rich:column style="width: 50px; " >
<f:facet name="header">
<h:outputLabel value="File Name" />
</f:facet>
<h:outputText value="#{file.filename} " />
</rich:column>
<rich:column style="width: 30px; " >
<f:facet name="header">
<h:outputLabel value="Language" />
</f:facet>
<h:outputText value="#{file.language} " />
</rich:column>
<rich:column style="width: 20px; " >
<f:facet name="header">
<h:outputLabel value="Is Default" />
</f:facet>
<h:outputText value="#{file.defaultness ? '++' : '--'}" />
</rich:column>
<rich:column>
<f:facet name="header" style="width: 40px; " >
<h:outputLabel value="Details" />
</f:facet>
<a4j:commandLink action="#{uploadBean.deleteFile}" style="text-decoration:none; margin-left: 5pt;" value="Delete"
reRender="attachmentUploadTableId" >
<f:setPropertyActionListener target="#{uploadBean.chosenFile}"
value="#{file}" />
</a4j:commandLink>
<h:outputLabel/>
<a4j:commandLink action="#{uploadBean.viewFile}" style="text-decoration:none; margin-left: 5pt;" value="Preview">
<f:setPropertyActionListener target="#{uploadBean.chosenFile}"
value="#{file}" />
</a4j:commandLink>
</rich:column>
</rich:dataTable>
</h:form>
</body>
</html>
这里是对应的java类:
public class UploadBean implements Serializable {
private static final org.apache.log4j.Logger _logger = org.apache.log4j.Logger.getLogger(UploadBean.class);
@Autowired PdfService pdfService;
//private PdfService pdfService;
private String filename;
private String language;
private Integer docId;
private Boolean defaultness = Boolean.TRUE;
// BLOB Handling
// new ByteArrayInputStream(pdfFile.getPdf_data()), pdfFile.getPdf_data().length);
private byte [] pdf_data;
int i = 0;
private List<PdfFile> fileList = new ArrayList<PdfFile>();
private PdfFile chosenFile;
private List<SelectItem> languageOptions = initLanguageOptions();
private List<SelectItem> initLanguageOptions(){
List<SelectItem> list = new ArrayList<SelectItem>(3);
list.add(new SelectItem("en", "English"));
list.add(new SelectItem("de", "German"));
list.add(new SelectItem("fr", "French"));
list.add(new SelectItem("it", "Italian"));
return list;
}
/**
* get all files from the DB schema
* @return
*/
public List<PdfFile> getFileListByDocId() {
Integer docId = getDocId();
try {
fileList = pdfService.getPDFFilesOnDocId(docId);
} catch (Exception e) {
_logger.error(e);
}
return fileList;
}
/**
* get all files by DOC_ID from the DB schema
* @param docId
* @return
*/
public List<PdfFile> getFileListByDocId(Integer docId) throws Exception {
fileList.add( new PdfFile().setLanguage("en").setFilename("first_File").setId(i++) );
fileList.add( new PdfFile().setLanguage("de").setFilename("second_File").setId(i++) );
fileList.add( new PdfFile().setLanguage("fr").setFilename("third_File").setId(i++) );
//this.fileList = pdfService.getPDFFilesOnDocId(getDocId());
return fileList;
}
// ActionEvent event EL version
public String uploadAssignToOrder(){
//event.getComponent().getAttributes();
System.err.println("uploadAssignToOrder...");
_logger.error("uploadAssignToOrder...");
PdfFile file = new PdfFile();
try {
String fileName = JsfUtil.PDF_FILE_PREFIX + getDocId() + "_" + getLanguage();
Integer defaultness = Boolean.TRUE.equals(getDefaultness()) ? 1 : 0;
file.setFilename(fileName);
file.setDocId(docId);
file.setLanguage(language);
file.setDefaultness(defaultness);
file.setPdf_data(pdf_data);
pdfService.insertFile(file);
this.fileList = pdfService.getPDFFilesOnDocId(getDocId());
//fileList.add(file);
JsfUtil.addSuccessMessage("Pdf File attached for order Id:" + this.docId);
} catch (Exception e) {
e.printStackTrace();
JsfUtil.addErrorMessage("Pdf File could not be attached for order Id:" + this.docId);
}
return null;
}
public void fileUploadListener(UploadEvent event) {
InputStream is = null;
try {
JsfUtil.addSuccessMessage("uploadFile starting...");
System.out.println("uploadFile(UploadEvent event)...for Doc Id:" + this.docId);
_logger.info("In ActionListener of file upload for Doc Id:" + this.docId);
UploadItem item = event.getUploadItem();
_logger.info("item : " + item.getFileSize() + ", " + item.getContentType() + ", " + item.getData());
if(item.getFile() != null) {
_logger.info("File name and size : " + item.getFile().getPath() + ", " + item.getFile().getTotalSpace());
}
byte [] data = new byte[item.getFileSize()];
is = new FileInputStream(item.getFile());
is.read(data);
is.close();
// save the pdf_data of byte [] for the time being
this.setPdf_data(data);
// PdfFile file = new PdfFile();
// file.setLanguage(getLanguage()).setPdf_data(data).setFilename(item.getFileName()).setDefaultness(1);
// pdfService.saveFile(file);
// fileList.add(file);
System.out.println("uploadFile end...");
_logger.info("uploadFile end...");
} catch (Exception e) {
e.printStackTrace();
_logger.error(e.getMessage() + e);
} finally {
try {
is.close();
} catch (IOException IOE) {
is = null;
}
is = null;
}
}
public void setFileList(ArrayList<PdfFile> fileList) {
this.fileList = fileList;
}
public List<PdfFile> getFileList() {
//TODO
fileList.add( new PdfFile().setLanguage("en").setFilename("first_File").setId(i++) );
fileList.add( new PdfFile().setLanguage("de").setFilename("second_File").setId(i++) );
fileList.add( new PdfFile().setLanguage("fr").setFilename("third_File").setId(i++) );
return this.fileList;
}
public String deleteFile() {
try {
getFileListByDocId().remove(chosenFile);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public String viewFile() {
return null;
}
public PdfFile getChosenFile() {
return chosenFile;
}
public void setChosenFile(PdfFile chosenFile) {
this.chosenFile = chosenFile;
}
public PdfService getPdfService() {
return pdfService;
}
public void setPdfService(PdfService pdfService) {
this.pdfService = pdfService;
}
public String getLanguage() {
return this.language;
}
public void setLanguage(String language) {
this.language = language;
}
public List<SelectItem> getLanguageOptions() {
return languageOptions;
}
public Integer getDocId() {
return this.docId;
}
public void setDocId(Integer docId) {
this.docId = docId;
}
public String getFilename() {
return this.filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public byte[] getPdf_data() {
return this.pdf_data;
}
public void setPdf_data(byte[] pdf_data) {
this.pdf_data = pdf_data;
}
public Boolean getDefaultness() {
return this.defaultness;
}
public void setDefaultness(Boolean defaultness) {
this.defaultness = defaultness;
}
}
web.xml
<filter>
<filter-name>RichFaces Filter</filter-name>
<filter-class>org.ajax4jsf.Filter</filter-class>
<init-param>
<param-name>createTempFiles</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>maxRequestSize</param-name>
<param-value>50000000</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>RichFaces Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>