gpt4 book ai didi

jsf - 如何将 Hibernate Validator 与 Primefaces 客户端站点验证 (CSV) 结合使用

转载 作者:行者123 更新时间:2023-12-04 19:39:23 25 4
gpt4 key购买 nike

我遵循了 Primefaces 5.1 指南,并且能够使客户端站点验证 (CSV) 与 Bean 验证 (JSR 349) 一起使用。我有

<context-param>
<param-name>primefaces.TRANSFORM_METADATA</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>primefaces.CLIENT_SIDE_VALIDATION</param-name>
<param-value>true</param-value>
</context-param>

通过此设置,标准 @Size@Pattern 无需往返服务器即可工作。但是,来自 Hibernate Validator 的注释,如 @Email 根本不起作用。 Primefaces 在他们的用户指南中将第三方注释描述为

When using 3rd party constraints like Hibernate Validator specific annotations, use BeanValidationMetadataMapper to define a ClientValidationConstraint for them.

BeanValidationMetadataMapper.registerConstraintMapping(Class<? extends Annotation>
constraint, ClientValidationConstraint clientValidationConstraint);

BeanValidationMetadataMapper.removeConstraintMapping(Class<? extends Annotation>
constraint);

这对我来说真的很神秘。有谁知道设置应该是什么样子?谢谢。

最佳答案

例如@NotBlank:

package com.algaworks.pedidovenda.util;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import org.hibernate.validator.constraints.NotBlank;
import org.primefaces.validate.bean.BeanValidationMetadataMapper;

import com.algaworks.pedidovenda.validation.NotBlankClientValidationConstraint;

@WebListener
public class AppContextListener implements ServletContextListener {

@Override
public void contextDestroyed(ServletContextEvent event) {
}

@Override
public void contextInitialized(ServletContextEvent event) {
System.setProperty("org.apache.el.parser.COERCE_TO_ZERO", "false");

BeanValidationMetadataMapper.registerConstraintMapping(NotBlank.class,
new NotBlankClientValidationConstraint());
}

}

和 ClientValidationConstraint 本身:

package com.algaworks.pedidovenda.validation;

import java.util.HashMap;
import java.util.Map;

import javax.validation.metadata.ConstraintDescriptor;

import org.hibernate.validator.constraints.NotBlank;
import org.primefaces.validate.bean.ClientValidationConstraint;

public class NotBlankClientValidationConstraint implements ClientValidationConstraint {

public static final String MESSAGE_ID = "{org.hibernate.validator.constraints.NotBlank.message}";

@SuppressWarnings("rawtypes")
@Override
public Map<String, Object> getMetadata(ConstraintDescriptor constraintDescriptor) {
Map<String, Object> metadata = new HashMap<String, Object>();
Map attrs = constraintDescriptor.getAttributes();

Object message = attrs.get("message");

if (!message.equals(MESSAGE_ID)) {
metadata.put("data-msg-notblank", message);
}

return metadata;
}

@Override
public String getValidatorId() {
return NotBlank.class.getSimpleName();
}

}

假设代码有效,因为它是从这里“借来的”:

Listener

ClientValidationConstraint

不要忘记:

PrimeFaces.validator.NotBlank = {

MESSAGE_ID : 'org.hibernate.validator.constraints.NotBlank.message',

validate : function(element, value) {
if (value === null || value === undefined || value.trim() === '') {
var msg = element.data('msg-notblank');
var label = element.data('p-label');
var context = PrimeFaces.util.ValidationContext;
var msgObj;

if (!msg) {
msgObj = context.getMessage(this.MESSAGE_ID, label);
} else {
var msgObj = {
summary : msg,
detail : msg
}
}

throw msgObj;
}
}

};

关于jsf - 如何将 Hibernate Validator 与 Primefaces 客户端站点验证 (CSV) 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26230848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com