简体   繁体   English

PrimeFaces如何验证上传的文件名?

[英]PrimeFaces How to validate uploaded file name?

Does any body know how to implement validation with validation message to uploadFile in PrimeFaces? 是否有任何机构知道如何使用验证消息对PrimeFaces中的uploadFile实施验证?

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: FileBean:

List<UploadedFile> uploadedFiles;

    public void handleFileUpload(FileUploadEvent event) {
        if (uploadedFiles == null) {
            uploadedFiles = new ArrayList<>();
        }
        uploadedFiles.add(event.getFile());
    }

uploadValidator.java 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." 我需要检查上传的文件名是否为拉丁语Unicode,如果没有 - 向用户显示“错误的文件名”。 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 managedBean 有很多方法可以从您的managedBean中显示消息

From your Entity 来自您的实体

entity.java entity.java

@NotEmpty(message = "{validation.msg.notNull}")
@NotBlank(message = "{validation.msg.notBlank}")
@Column(name = "code", unique = true)
private String code;

and in your page.xhtml 并在您的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 managedBean 使用此解决方案的大+如果p:inputTextNotEmptyNotBlank则信息甚至不会转到实体级别,并且您不必创建条件来验证p:inputText是否为NotEmptyNotBlank in您的managedBean

From your ManagedBean 从您的ManagedBean

page.xhtml page.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.java 并在您的ManagedBean.java中

public 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 . 您可以在Primefaces消息示例网站中找到最佳示例,并且growl消息也是显示消息Primefaces growl示例网站的好方法。

Hope that helped you. 希望对你有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM