简体   繁体   English

在Tapestry Web应用程序中处理域异常的最佳方法是什么?

[英]What is the best way to handle domain exceptions in Tapestry web application?

I'm having some dilemmas on how to process and handle checked exceptions that are thrown by my domain objects while using Tapestry to create the web GUI as a presentation layer. 我在使用Tapestry创建Web GUI作为表示层时,如何处理和处理由我的域对象抛出的已检查异常,我遇到了一些困境。

Assume I have a domain object, Foo that throws FooException on one of its methods: 假设我有一个域对象, Foo在其中一个方法上抛出FooException

public class Foo {
    ...
    public void fooMethod throws FooException() {
        ...
    }
    ...
}

Now, assume I have a Tapestry page called Bar where Foo object is, for example, being edited with BeanEditor . 现在,假设我有一个名为Bar的Tapestry页面,例如,使用BeanEditor编辑Foo对象。

Now, to ensure illegal values are not passed to BeanEditor for creating Foo object, I can think of two basic ways to do that: 现在,为了确保非法值不会传递给BeanEditor来创建Foo对象,我可以想到两种基本方法:

  1. Tapestry field validation using @Validate annotation 使用@Validate注释进行Tapestry字段验证

    In this case, if we can filter and check inputs via regexes or by limiting values or doing similar actions made available by @Validate we'll get a nifty error message next to the field we're editing and the submit will fail, thus making the user think about what he wrote there and how to fix it. 在这种情况下,如果我们可以通过正则表达式过滤和检查输入,或者通过限制值或执行@Validate提供的类似操作,我们将在我们正在编辑的字段旁边收到一条漂亮的错误消息,提交将失败,从而使用户考虑他在那里写的内容以及如何解决它。

  2. Catching domain exception and doing actions based on it 捕获域异常并基于它执行操作

    I assume this scenario offers more options regarding what can and cannot be done. 我假设这种情况提供了更多关于可以做什么和不可以做什么的选择。 For example, if the used needs to enter URL and makes a mistake while doing so, the URL constructor will throw its own MalformedURLException . 例如,如果使用者需要输入URL并且在执行此操作时出错,则URL构造函数将抛出自己的MalformedURLException We can catch that exception in our Java code, but my question is, what to do next and how? 我们可以在Java代码中捕获该异常,但我的问题是,下一步该做什么以及如何做?

Does Tapestry offer any special mechanisms of handing domain exceptions (checked and/or unchecked), other than that exception window that pops up when things break apart? Tapestry是否提供了处理域异常(已检查和/或未检查)的任何特殊机制,除了在事情分解时弹出的异常窗口?

Are there any patterns on how to solve this particular problem? 有没有关于如何解决这个特定问题的模式?

Where do you draw the limit between common and ordinary exceptions like IndexOutOfBoundsException and some domain-specific ones like FooException ? 您在哪里绘制常见和普通异常(如IndexOutOfBoundsException和某些特定于域的异常(如FooException之间的限制?

:D :d

The best way is applying validations to fields. 最好的方法是将验证应用于字段。 One way is using @Validate. 一种方法是使用@Validate。 Another one is to use the Bean Validation (JSR 303) annotations by adding tapestry-beanvalidator, which is explained here: http://tapestry.apache.org/bean-validation.html . 另一个是通过添加tapestry-beanvalidator使用Bean Validation(JSR 303)注释,这里将解释: http ://tapestry.apache.org/bean-validation.html。

In addition, in Tapestry, all form field component trigger a "validate" event before the value is applied to the property. 此外,在Tapestry中,所有表单字段组件在将值应用于属性之前触发“验证”事件。

http://tapestry.apache.org/forms-and-validation.html explains it all. http://tapestry.apache.org/forms-and-validation.html解释了这一切。 For example, supposing you have a form field with at:id of "count", you could validate it by declaring an event handler method: 例如,假设您有一个at:id为“count”的表单字段,您可以通过声明一个事件处理程序方法来验证它:

void onValidateFromCount(Integer value) throws ValidationException {
    if (value == 13) throw new ValidationException("Thirteen is an unlucky number.");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM