将业务规则存储在 XML 文档中,然后在 Java 中验证,如何?

发布于 2024-09-01 02:55:24 字数 629 浏览 6 评论 0 原文

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愿得七秒忆 2024-09-08 02:55:24

这个轮子已经被重新发明了很多次了!

您可能可以通过实现 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.

拒绝两难 2024-09-08 02:55:24

也许一个例子是:

  //PSEUDOCODE
  abstract class Validator {
    Map<String method, Set<Field> fields> validationMap;
    Map<String fieldName, Set<String> errorMessages> validationErrors;

 void parseXML() {
     validationMap.add("notNull", { " 'username', 'JavaPete' ", " 'email', '[email protected]' " }
     validationMap.add("max-length", { " 'username', 'JavaPete', '15' ", ... }
  }

 void validate() {
      for (String method : validationMap.keys) {
           for (Field field : validationMap.get(method) ) {
                 invoke(method, field);
           }
      }
  }

 void notNull(Field field) {
     if (field.getValue() == "") {
          validationErrors.add(field.getFieldName(), "Can't be null!");
     }
  }

注意我没有添加每个 Set<>地图中的属性正确,但这只是为了在这里指出一点。

这是一个有效的解决方案吗?强壮的?

Maybe an example would be:

  //PSEUDOCODE
  abstract class Validator {
    Map<String method, Set<Field> fields> validationMap;
    Map<String fieldName, Set<String> errorMessages> validationErrors;

 void parseXML() {
     validationMap.add("notNull", { " 'username', 'JavaPete' ", " 'email', '[email protected]' " }
     validationMap.add("max-length", { " 'username', 'JavaPete', '15' ", ... }
  }

 void validate() {
      for (String method : validationMap.keys) {
           for (Field field : validationMap.get(method) ) {
                 invoke(method, field);
           }
      }
  }

 void notNull(Field field) {
     if (field.getValue() == "") {
          validationErrors.add(field.getFieldName(), "Can't be null!");
     }
  }

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文