如何处理Wicket自定义模型中抛出的异常? [英] How to handle exceptions thrown in Wicket custom model?

查看:101
本文介绍了如何处理Wicket自定义模型中抛出的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有自定义模型的组件(扩展了小门标准模型类)。当Wicket调用 getObject()时,我的模型会从数据库/ Web服务中加载数据。



此查找可能会失败原因有几个。我想通过在网页上显示一个很好的消息来处理这个错误。最好的方法是什么?

  public class MyCustomModel extends Model {

@Override
public String getObject()
try {
return Order.lookupOrderDataFromRemoteService();
} catch(Exception e){
logger.error(Silent ...);
//如何将它传播到组件/页面?
}
返回null;
}

请注意,该错误发生在与组件分离。

解决方案

您的模型可以实现IComponentAssignedModel,从而能够掌握所有的组件。 p>

但是我想知道您多久可以重用MyCustomModel?
我知道一些开发者主张创建独立的模型实现(通常在单独的包中)。虽然有一般情况下这是有用的(例如FeedbackMessagesModel),在我的经验中,更容易创建内部类,它们是特定于组件的。


I have a component with a custom model (extending the wicket standard Model class). My model loads the data from a database/web service when Wicket calls getObject().

This lookup can fail for several reasons. I'd like to handle this error by displaying a nice message on the web page with the component. What is the best way to do that?

public class MyCustomModel extends Model {

    @Override
    public String getObject() {
        try {
            return Order.lookupOrderDataFromRemoteService();
        } catch (Exception e) {
            logger.error("Failed silently...");
            // How do I propagate this to the component/page?
        }           
        return null;
}

Note that the error happens inside the Model which is decoupled from the components.

解决方案

Your model could implement IComponentAssignedModel, thus being able to get hold on the owning component.

But I wonder how often are you able to reuse MyCustomModel? I know that some devs advocate creating standalone model implementations (often in separate packages). While there are general cases where this is useful (e.g. FeedbackMessagesModel), in my experience its easier to just create inner classes which are component specific.

这篇关于如何处理Wicket自定义模型中抛出的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆