XML 规则文档示例:
<user>
<username>
<not-null/>
<capitals value="false"/>
<max-length value="15"/>
</username>
<email>
<not-null/>
<isEmail/>
<max-length value="40"/>
</email>
</user>
如何实现?我从头开始,我目前拥有的是一个 User 类和一个 UserController,它将 User 对象保存在 de DB 中(通过 Service 层和 Dao 层),基本的 Spring MVC。 我不能使用 Spring MVC 验证,但是在我们的模型类中,我必须使用 XML 文档,以便管理员可以更改规则
我认为我需要一种模式,可以根据什么动态构建算法是由 XML 规则文档提供的,但除了大量的 if 语句之外,我似乎想不出其他任何东西。
我还没有任何用于解析的东西,我不确定如何将其与验证过程的实际实现耦合(分离)。
Example XML Rules document:
<user>
<username>
<not-null/>
<capitals value="false"/>
<max-length value="15"/>
</username>
<email>
<not-null/>
<isEmail/>
<max-length value="40"/>
</email>
</user>
How do I implement this? I'm starting from scratch, what I currently have is a User-class, and a UserController which saves the User object in de DB (through a Service-layer and Dao-layer), basic Spring MVC. I can't use Spring MVC Validation however in our Model-classes, I have to use an XML document so an Admin can change the rules
I think I need a pattern which dynamically builds an algorithm based on what is provided by the XML Rules document, but I can't seem to think of anything other than a massive amount of if-statements.
I also have nothing for the parsing yet and I'm not sure how I'm gonna (de)couple it from the actual implementation of the validation-process.
发布评论
评论(2)
这个轮子已经被重新发明了很多次了!
您可能可以通过实现 Spring 的 Validator 接口,但您可能需要实现或找到一种方法将验证基于通过其他方式加载的规则。
我 Google 搜索并找到了许多描述如何使用公共验证的页面在春天。作为奖励,一些人描述了使用 valang。如果您的管理员可以在部署之前编辑验证规则,那么其中之一可能就足够了。
如果您确实想制定自己开发的 XML 业务规则,您可能需要使用 Apache Digester< 来解析规则/a>.您可能希望将规则加载到您自己的 Spring Validator 接口实现所使用的数据结构中。
如果规则在部署后必须更改,您当然需要添加一种机制来刷新规则。但至少 Digester 可能仍然会提供帮助。
This wheel has been reinvented so many times!
You likely can use Spring MVC validation by implementing Spring's Validator interface, but you may need to implement or find a way to base the validation on rules loaded by another means.
I googled and located many pages describing how to use commons validation in Spring. As a bonus, some describe using valang. If your admin can edit the validation rules prior to deployment, one of these might well suffice.
If you really want to make a home-grown XML business rules, you might want to parse the rules using Apache Digester. You probably want to load the rules into a data structure used by your own implementation of the Spring Validator interface.
If the rules have to be changeable after deploy, you'll of course need to add a mechanism to refresh the rules. But at least Digester will still probably help there.
也许一个例子是:
注意我没有添加每个 Set<>地图中的属性正确,但这只是为了在这里指出一点。
这是一个有效的解决方案吗?强壮的?
Maybe an example would be:
Notice I didn't add each Set<> property in the Maps correctly, but it's just to make a point here.
Would this be a valid solution? robust?