我正在使用PrimeFaces fileUpload和多个上传选项。在我的项目中,我想在图片上传期间发送电子邮件通知。我的问题是当我上传10张图片意味着同时发送10封电子邮件通知。我想在上传10张图片时只发送一封电子邮件通知。我正在使用primefaces 3.0和jsf 2.0。我该如何解决?
我的jsf页面:
<p:fileUpload id="imaload" fileUploadListener="#{photoUploadAction.handleImage}"
mode="advanced" multiple="true" process="@form"
update="messages,@form"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/>
支持Bean:
public void handleImage(FileUploadEvent event) throws IOException, EmailException {
try {
photoUploadVO.setDisabled("false");
//BufferedImage image = ImageIO.read(in);
ImageIO.write(resize(bufferedImage, 400, bufferedImage.getHeight()), "jpg", new File(tmpFile));
flag = photoUploadDaoService.uploadPhotos(photoUploadVO);
// profileImageService.uploadPhotos(profileImageBean);
if (flag == true) {
if(!loginBean.getType().equals("ngo") && !loginBean.getType().equals("admin") &&
!loginBean.getType().equals("ngo_coordinator") ){
volName = getVolunteerName(photoUploadVO.getUsrId(),photoUploadVO.getUser_type());
lst = apDao.retreiveSetup();
notification = lst.get(0).activity_email.toString();
email = lst.get(0).approval_toEmail.toString();
if(notification.equalsIgnoreCase(tmp)){
ecs.sendPhotoNotiFication(email,photoUploadVO,volName);
}
}
FacesMessage msg = new FacesMessage("Successfully Uploaded");
FacesContext.getCurrentInstance().addMessage(null, msg);
} else {
FacesMessage msg = new FacesMessage("Failure", event
.getFile().getFileName() + " to uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
} catch (IOException e) {
e.printStackTrace();
FacesMessage error = new FacesMessage(
FacesMessage.SEVERITY_ERROR,
"The files were not uploaded!", "");
FacesContext.getCurrentInstance().addMessage(null, error);
}
}
This is my email notification method inside handle upload methos:
ecs.sendPhotoNotiFication(email,photoUploadVO,volName);
答案 0 :(得分:4)
重新设计bean,使文件上传处理程序方法仅捕获并记住某些集合中的所有上传文件。然后添加一个&#34; Save&#34;表单下方的按钮,该按钮将绑定到实际处理并保存所有这些上传文件并最终发送邮件的操作方法。如果将bean放在视图范围中,只要最终用户与同一视图交互,就可以重用同一个bean实例。然后,您可以只收集集合属性中的上传文件。
这样的事情:
@ManagedBean
@ViewScoped
public class Bean implements Serializable {
private List<UploadedFile> uploadedFiles;
@PostConstruct
public void init() {
uploadedFiles = new ArrayList<UploadedFile>();
}
public void upload(FileUploadEvent event) {
uploadedFiles.add(event.getFile());
}
public void save() {
for (UploadedFile uploadedFile : uploadedFiles) {
// Process them all here.
}
// Send only one email.
}
}
与
<p:fileUpload ... fileUploadListener="#{bean.upload}" />
<p:commandButton value="Save" action="#{bean.save}" />