Tag: validation

没有命名空间的xml的Java xsdvalidation

我想针对XSD架构validationXML文件。 XML文件根元素没有任何名称空间或xsi详细信息。 它没有属性,只有 。 我已经尝试了以下代码来自http://www.ibm.com/developerworks/xml/library/x-javaxmlvalidapi.html而没有运气,因为我收到cvc-elt.1: Cannot find the declaration of element ‘root’ SchemaFactory factory = SchemaFactory.newInstance(“http://www.w3.org/2001/XMLSchema”); File schemaFile = new File(“schema.xsd”); Schema xsdScheme = factory.newSchema(schemaFile); Validator validator = xsdScheme.newValidator(); Source source = new StreamSource(xmlfile); validator.validate(source); xml使用包含的命名空间头文件(通过xmlspy添加)validation正常,但我认为可以声明xml命名空间而无需手动编辑源文件? 编辑和解决方案: public static void validateAgainstXSD(File file) { try { SchemaFactory factory = SchemaFactory.newInstance(“http://www.w3.org/2001/XMLSchema”); File schemaFile = new File(“path/to/xsd”); Schema […]

Javavalidation框架

我曾经遇到过java的validation框架,在这里您编写了一个保护数据类型完整性的方法,并且该数据类型上的任何CRUD操作都自动调用此方法。 有谁知道这个框架是什么? 我只是想避免对附加到数据类型的每个CRUD方法进行重复validation。

PrimeFaces日历接受无效日期作为输入

我遇到的问题是PrimesFaces 3.4.1日历。 当使用通过按钮或输入字段焦点激活的弹出日期选择器时,您只能选择工作正常,快乐的日子的有效日期! 当您手动将日期添加到输入字段时,如果您添加一个无效的日期,PrimeFaces日历组件将其转换为有效日期然后发送它,这意味着后端validation是不行的。 以下一些有趣的翻译: 30/02/2012成为2/6/2014 322/05/2012成为5/10/2038 2012年1月14日成为2012年4月1日 要重现这种疯狂,请看看PrimeFaces日历展示 。 我已经看到使用readOnlyInput=’true’属性的解决方案,但这似乎只是防止在字段中输入字母而不是数字或斜杠。 以下是我实施的日历的一个实例: 解决方案明智我对任何建议持开放态度: 这是PrimeFaces中的常见问题吗? 有没有我可以用来解决它的技巧? 我可以使用JavaScript在发送之前validation日期还是完全阻止所有用户输入? 还有别的我还没有想到的! 在此先感谢,这已经引起了我好几周的问题!

Hibernate – 激活Bean Validation集成时出错

我正在尝试设置Hibernate。 但是当我尝试使用以下代码创建会话工厂时: Configuration configuration = new Configuration(); configuration.configure(); serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); 我收到错误: org.hibernate.cfg.beanvalidation.IntegrationException: Error activating Bean Validation integration at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.integrate(BeanValidationIntegrator.java:156) at org.hibernate.internal.SessionFactoryImpl.(SessionFactoryImpl.java:303) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1760) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) […]

为什么f:validateDoubleRange仅适用于@SessionScoped?

有人可以向我解释为什么我的例子中的Foo在到达validateDoubleRange类时总是为空? 最终结果是validation器的最小值始终为0.在outputText元素中,数字3在页面上显示正常。 如果我使用@SessionScoped而不是@ViewScoped来validation它是@ViewScoped 控制器: import java.io.Serializable; import java.math.BigDecimal; import javax.faces.bean.ManagedBean; import javax.faces.bean.ViewScoped; @ViewScoped @ManagedBean(name = “fooController”) public class FooController implements Serializable { private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(FooController.class); private static final long serialVersionUID = 1L; private Foo foo; private BigDecimal amount; private Long fooId; public Long getFooId() { return fooId; } public void setFooId(Long […]

Spring @Validated在服务层

HEJ, 我想在执行如下方法之前使用@Validated(group=Foo.class)注释来validation参数: public void doFoo(Foo @Validated(groups=Foo.class) foo){} 当我将此方法放在我的Spring应用程序的Controller中时,执行@Validated并在Foo对象无效时抛出错误。 但是,如果我在我的应用程序的Service层中的方法中放置相同的东西,则不会执行validation,即使Foo对象无效,该方法也会运行。 你不能在服务层使用@Validated注释吗? 或者我是否必须配置额外的东西以使其工作? 更新: 我在service.xml中添加了以下两个bean: 并用@Validate替换@Validate , @Null所示: public void doFoo(Foo @Null(groups=Foo.class) foo){} 我知道这是一个非常愚蠢的注释,但我想检查一下,如果我现在调用该方法并传递null,它将抛出违规exception。 那么为什么它执行@Null注释而不是@Validate注释呢? 我知道一个来自javax.validation而另一个来自Spring,但我不认为这与它有什么关系?

使用Luhn算法检查信用卡有效性

我尝试使用Luhn算法检查信用卡的validation,该算法的工作方式如下: 从右到左加倍每秒。 如果数字加倍会产生两位数字,请将两位数相加得到一位数字。 2 * 2 = 4 2 * 2 = 4 4 * 2 = 8 1 * 2 = 2 6 * 2 = 12(1 + 2 = 3) 5 * 2 = 10(1 + 0 = 1) 8 * 2 = 16(1 + 6 = 7) 4 * 2 = 8 […]

Autowired Repository在Custom Constraint Validator中为空

我有一个基于Spring的webapp。 我在我的控制器中使用了几个带注释@Repository,@ Transaction的存储库类。 那部分工作正常。 我创建了一个Custom Constraint Validator,它必须访问存储库。 现在我无法理解为什么Repository为null。 我尝试使用@Component注释来注释validation器。 我的基本包包含所有这些类是在xml的一部分。 那么我还应该做些什么来确保存储库dependency injection工作。 这就是我的Constraintvalidation器的样子。 public class DonorTypeExistsConstraintValidator implements ConstraintValidator { @Autowired private DonorTypeRepository donorTypeRepository; @Override public void initialize(DonorTypeExists constraint) { } public boolean isValid(DonorType target, ConstraintValidatorContext context) { System.out.println(“donorType: ” + target); if (target == null) return true; try { if (donorTypeRepository.isDonorTypeValid(target.getDonorType())) return true; } catch […]

JSR303自定义validation器被调用两次

我正在使用Spring MVC创建一个网站,并且为了持久性我使用Spring Data JPA和Hibernate 4作为我的JPA提供者。 目前正在使用Hibernate Validator处理validation。 我有一个问题,我的validation器被调用两次,我无法弄清楚为什么。 这是一个问题的主要原因是因为第二轮,依赖关系没有自动连接到validation器,我得到一个空指针exception。 以下是导致失败的呼叫序列: 提交注册表单,首先调用NotDefaultSectValidator并成功完成用户对象上的“whereDidYouHearAboutUs”字段。 接下来调用UniqueUsernameValidator并成功完成“用户名”字段validation。 控制器上的’addUserFromForm’方法启动,并在bindingResults对象中找不到任何错误。 然后在UserService类上调用’addUser’方法。 此方法到达’userRepository.save(user);’行 但之后不要立即运行’print.ln’行。 单步执行此行将返回到“NotDefaultSectValidator”断点。 这是第二次完成,我重新输入第二个validation器’UniqueUsernameValidator’。 这里我得到一个空指针exception,因为出于某种原因,Spring第二次无法在DAO中自动assembly。 任何人都可以阐明为什么validation器被调用两次,特别是为什么要跨越’userRepository.save(user);’ 回到这些validation器? 非常感谢 这是我的user.java类 package com.dating.domain; import java.util.HashSet; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.PrePersist; import javax.persistence.PreUpdate; import javax.persistence.Table; […]

Java Bean Validation(JSR303)约束涉及几个bean属性之间的关系

假设我有以下简单的java bean: class MyBean { private Date startDate; private Date endDate; //setter, getters etc… } JSR 303中是否有一种机制来创建一个自定义validation器来validationstartDate必须在endDate之前的约束? 在我看来,这是一个常见的用例,但我找不到这种多属性关系约束的任何例子。