Struts2 Hibernate独特的密钥验证
我在表中有一个字段为“mobileno”,该字段应该是唯一的。
使用 Struts2 进行验证的最佳方法是什么 - 休眠验证?
提前致谢。
问候,毗湿奴
I have a field say 'mobileno' in table that should be unique.
what is the best way for validation using Struts2 - hibernate validation?
Thanks in advance.
Regards, Vishnu
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在数据库中的列上放置唯一约束,但在关闭事务之前不会检查此约束。听起来您也想检查用户反馈的目的,因此唯一的方法是在输入值后进行搜索。 Struts 验证无法直接帮助解决此问题,因为您需要访问数据库。您可以使用单独的 Struts 操作来检查重复项。这可以通过 ajax 调用从网页调用。传入代码进行检查,Struts 可以将其路由到服务,Hibernate 将进行查找并根据搜索结果返回 true 或 false。
You could place a unique constraint on the column in the database, but then this won't be checked until you close the transaction. It sounds like you want to check for the purpose of user feedback as well, so the only way to do that is to do a search after the value is entered. Struts validation can't help with this directly since you will need to go to the database. You could have a separate Struts action for checking for duplicates. This could be called from the web page via an ajax call. Pass in the code to check, Struts can route it to a service, Hibernate will do the find and return true or false depending on the results of the search.