这是在GWT Java应用程序中加载视图/创建页面的正确方法

时间:2016-02-19 03:36:09

标签: java gwt

我现在正在查看我的代码,以确保它是正确和一致的。直接的方式我发现我以两种不同的方式加载视图: 一个是:

public HikeRecordView() {   

    //On load of page get the stored view data and create the page
    verticalPanel.addAttachHandler(new Handler() {
        public void onAttachOrDetach(AttachEvent event) {
            if (event.isAttached()) {

                rpc = (DBConnectionAsync) GWT.create(DBConnection.class);
                ServiceDefTarget target = (ServiceDefTarget) rpc;
                String moduleRelativeURL = GWT.getModuleBaseURL() + "MySQLConnection";
                target.setServiceEntryPoint(moduleRelativeURL);

                horizontalPanel_Existing.clear();
                verticalPanel.clear();

                AsyncCallback<ViewData> callback = new ViewDataHandler<ViewData>(HikeRecordView.this);
                rpc.getViewData(callback);  
            }
        }
    });
    initWidget(verticalPanel);
}

另一个是:

public PackHolidayView() {  
    rpc = (DBConnectionAsync) GWT.create(DBConnection.class);
    ServiceDefTarget target = (ServiceDefTarget) rpc;
    String moduleRelativeURL = GWT.getModuleBaseURL() + "MySQLConnection";
    target.setServiceEntryPoint(moduleRelativeURL);

    //On load of page render the page
    verticalPanel.addAttachHandler(new Handler() {
        public void onAttachOrDetach(AttachEvent event) {
            verticalPanel.clear();
            verticalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

            //On load of page get the Account Level and ID of the account holder.
            AsyncCallback<ViewData> callback = new ViewDataHandler<ViewData>(PackHolidayView.this);
            rpc.getViewData(callback);
        }
    });
    initWidget(verticalPanel);
}

它们似乎都工作,这是加载视图的最佳/推荐方法吗?

1 个答案:

答案 0 :(得分:1)

您应该考虑在Model,View和Presenter之间拆分代码。创建适当的图层。考虑使用GIN进行依赖注入。

阅读此处的文章:http://www.canoo.com/blog/2011/04/05/gwt-dependency-injection-recipes-using-gin/