我可以在操作中使用 Struts2 验证器吗?
问题是我有一个动态表单。我根据一些数据构建它。我想在操作的 validate() 方法中使用验证器方法形式,以避免重写该代码。我试着写一些类似的东西
(new EmailValidator()).validate(email);
但它会抛出 NullPointerException。可能它需要一个更大的环境......
你能帮助我吗?
The problem is that I have a dynamic form. I build it according with some data. I'd like to use validators method form within the validate() method in the action to avoid rewriting that code. I tryed to write something like
(new EmailValidator()).validate(email);
But it throws a NullPointerException. Probably it need a gigger environment...
can you help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在本例中您使用了哪个 EmailValidator()?在struts2中,验证器是根据表单文本字段的字段名称使用的。它将调用要验证的字段的 getter 方法并将电子邮件正则表达式应用于该字段。我怀疑它在调用电子邮件字段的 getter 方法时得到 null,但得到 null,因此在对其应用正则表达式时抛出 nullpointerException。
干杯
Which EmailValidator() you used in this case? In struts2, the validator is used base on the form textfield's field name. It will call the getter method of the field to be validate and apply the email regex to it. I suspect that it get null when calling to the getter method of the email field but get null, hence throw out the nullpointerexception when apply the regex on it.
cheers
其实我今天也遇到了同样的问题。我确信该字段的 getter 方法不会返回 null。(实际上我尝试打印 getter 方法返回的值。它工作得很好)。那么问题还可能出在哪里呢?
Actually I came across a same problem today. I'm sure that my getter method for that field doesn't returns null.(Actually I tried printing the value returned by getter method.It works fine). So where else could the problem be?.