本文介绍了PrimeFaces如何验证上传的文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 是否有任何机构知道如何在PrimeFaces中使用验证消息将uploadFile进行验证? 查看: < p:fileUpload id =upload fileUploadListener =#{fileBean.handleFileUpload} update =uploadsauto =truemultiple =trueskinSimple =true> < p> < h:messages id =messages/>< / p> < / p:fileUpload> FileBean : 列表与LT; UploadedFile的> UPLOADEDFILES; $ b $ public void handleFileUpload(FileUploadEvent event){ if(uploadedFiles == null){ uploadedFiles = new ArrayList<>(); } uploadedFiles.add(event.getFile()); } uploadValidator.java @FacesValidator(uploadValidator) public class UploadValidator implements Validator { @Override $ b $ public void validate(FacesContext上下文,UIComponent组件,Object值)抛出ValidatorException { Part file =(Part)value; FacesMessage message = null; $ b $尝试{ if(!file.getName()。matches(\\w +)) message = new FacesMessage(Wrong file name); if(message!= null&&!message.getDetail()。isEmpty())( message.setSeverity(FacesMessage.SEVERITY_ERROR); 抛出新的ValidatorException(message); } } catch(Exception ex){ throw new ValidatorException(new FacesMessage(ex.getMessage())); $ b我需要检查是否已上传文件名是拉丁文Unicode,如果不是 - 显示用户消息错误的文件名。我的代码不起作用。 谢谢。解决方案 > entity.java @NotEmpty(message ={validation.msg。 ) @NotBlank(message ={validation.msg.notBlank}) @Column(name =code,unique = true) private String code; 和您的page.xhtml < p:inputText id =code... /> < p:message for =code/> 如果 p:inputText 是 NotEmpty 和 NotBlank 这些信息甚至不会进入实体级别,如果 p:inputText 是 NotEmpty 和 NotBlank 在您的managedBean中 $ b 从您的ManagedBean b $ b page.xhtml < p:messages id =msgsglobalOnly =trueshowDetail = trueclosable =true/> < p:inputText id =code... /> < p:commandButton ... actionListener =#{managedBean.validate()}update =:msgs/> 和您的ManagedBean.java中的 public void validate(){ ... MyUtil.addErrorMessage(ERROR); ... $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $你可以在和咆哮信息也是一个很好的方法显示 Primefaces咆哮示例网站。 希望对你有帮助。 Does any body know how to implement validation with validation message to uploadFile in PrimeFaces?View: <p:fileUpload id="upload"fileUploadListener="#{fileBean.handleFileUpload}"update="uploads" auto="true" multiple="true" skinSimple="true"><f:validator validatorId="uploadValidator"/><p> <h:messages id="messages" /></p></p:fileUpload>FileBean:List<UploadedFile> uploadedFiles; public void handleFileUpload(FileUploadEvent event) { if (uploadedFiles == null) { uploadedFiles = new ArrayList<>(); } uploadedFiles.add(event.getFile()); }uploadValidator.java @FacesValidator("uploadValidator")public class UploadValidator implements Validator { @Override public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException { Part file = (Part) value; FacesMessage message=null; try { if (!file.getName().matches("\\w+")) message=new FacesMessage("Wrong file name"); if (message!=null && !message.getDetail().isEmpty()) { message.setSeverity(FacesMessage.SEVERITY_ERROR); throw new ValidatorException(message); } } catch (Exception ex) { throw new ValidatorException(new FacesMessage(ex.getMessage())); } }}I need to check if uploaded file name is on Latin Unicode and if not - show user a message "Wrong file name." My code doesn't work. No message is displayed no matter the file name.Thank you. 解决方案 There is many way's to make a message appear from your managedBeanFrom your Entityentity.java @NotEmpty(message = "{validation.msg.notNull}")@NotBlank(message = "{validation.msg.notBlank}")@Column(name = "code", unique = true)private String code;and in your page.xhtml <p:inputText id="code" ... /><p:message for="code" />The big + with this solution that if the p:inputText is NotEmpty and NotBlank the informations doesn't even go to the entity level, and you don't have to make a condition to verify if the p:inputText is NotEmpty and NotBlank in your managedBeanFrom your ManagedBeanpage.xhtml<p:messages id="msgs" globalOnly="true" showDetail="true" closable="true"/><p:inputText id="code" ... /><p:commandButton ... actionListener="#{managedBean.validate()}" update=":msgs" />and in your ManagedBean.javapublic void validate(){...MyUtil.addErrorMessage("ERROR");...}You can find the best example in the Primefaces messages example web site and the growl message is also a good way to show a message Primefaces growl example web site.Hope that helped you. 这篇关于PrimeFaces如何验证上传的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!