Can I use Struts form beans to do database validation like if email exists in database or not, as a form bean is front end component? Is that one way of doing validation?
1 回答
Sure.
The validate
method is designed to allow the use of arbitrary business logic beyond what's easy to do in the XML validation file. Whether or not it's the best approach depends.
Another option is to create your own pluggable validator and continue to configure validation in XML.
Regardless of the approach you take, the DB access, and ideally the business logic, should not live in the form bean and/or pluggable validator, since that ties the logic to the ancient (and soon-to-be EOL'd) Struts 1 framework. By separating it out into framework-neutral services or utilities, it becomes easier to test, and easier to reuse.