java - 使用 GWTP 创建表

标签 java json html-table gwtp

我正在熟悉 GWTP。我试图输出一个包含 JSON 值的表,该表是在 Piriti 映射器的帮助下获取的。这不是一个真正的项目代码,它只是一种理解 GWTP 的尝试,所以这可能不是最漂亮的解决方案(事实上,它不是肯定的)。以下是此过程中涉及的两个演示者:

FirstPresenter(使用 ProductListPresenter,这是一个小部件,我不确定这里应该使用小部件,但是,根据 this conversation ,小部件可能会成功):

public class FirstPresenter extends
        Presenter<FirstPresenter.MyView, FirstPresenter.MyProxy> {

    public static final Object SLOT_RATE = new Object();
    public static final Object SLOT_PRODUCT = new Object();
    private IndirectProvider<ProductListPresenter> productListFactory;

    public interface MyView extends View {
        public Panel getListProductPanel();
    }

    @Inject ProductListPresenter productListPresenter;

    @ProxyCodeSplit
    @NameToken(NameTokens.first)
    public interface MyProxy extends ProxyPlace<FirstPresenter> {
    }

    @Inject
    public FirstPresenter(final EventBus eventBus, final MyView view,
            final MyProxy proxy, Provider<ProductListPresenter> productListFactory) {
        super(eventBus, view, proxy);

        this.productListFactory = new StandardProvider<ProductListPresenter>(productListFactory);
    }

    @Override
    protected void revealInParent() {
    }

    @Override
    protected void onBind() {
        super.onBind();
    }

    @Inject
    PlaceManager placeManager;

    @Override
    protected void onReset() {
        super.onReset();
        setInSlot(SLOT_PRODUCT, null);
        for (int i = 0; i < 2; i++) { //TODO: change hardcoded value 

            productListFactory.get(new AsyncCallback<ProductListPresenter>() {

                @Override
                public void onSuccess(ProductListPresenter result) {
                        addToSlot(SLOT_PRODUCT, result);
                }

                @Override
                public void onFailure(Throwable caught) {

                }
            });

        }
    }

}

ProductListPresenter:

public class ProductListPresenter extends
        PresenterWidget<ProductListPresenter.MyView> {

    @Inject ProductListPiritiJsonReader reader;

    public interface MyView extends View {
        public Label getNameLabel();
        public Label getCompanyLabel();
        public Label getSerialLabel();
        public Label getPricesLabel();
    }

    @Inject
    public ProductListPresenter(final EventBus eventBus, final MyView view) {
        super(eventBus, view);
    }

    @Override
    protected void onBind() {
        super.onBind();
    }

    @Override
    protected void onReset() {
        super.onReset();

        try {
            RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, "/jsongwtproject/products.json");
            rb.setCallback(new RequestCallback() {
                @Override
                public void onResponseReceived(Request request, Response response) {
                    ProductList productList = reader.read(response.getText());
                    for (Product product : productList.getProductList()) {
                        fetchDataFromServer();
                    }
                }
                @Override
                public void onError(Request request, Throwable exception) {
                    Window.alert("Error occurred" + exception.getMessage());
                }
            });
            rb.send();
        } 
        catch (RequestException e) {
            Window.alert("Error occurred" + e.getMessage());
        }
    }
    //Takes the JSON string and uses showProductListData(String jsonString)  method
    public void fetchDataFromServer() {
        try {
            RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, "/jsongwtpproject/products.json");
            rb.setCallback(new RequestCallback() {
                @Override
                public void onResponseReceived(Request request, Response response) {
                    showProductListData(response.getText());                    
                }
                @Override
                public void onError(Request request, Throwable exception) {
                    Window.alert("Error occurred" + exception.getMessage());
                }
            });
            rb.send();
        } 
        catch (RequestException e) {
            Window.alert("Error occurred" + e.getMessage());
        }
    }
    //Uses Piriti mappers to take JSON values
    private void showProductListData(String jsonString) {
        ProductList productList = reader.read(jsonString);
        for (Product product : productList.getProductList()) {
            StringBuffer priceSb = new StringBuffer();
            for (Double price : product.getPrices()) {
                priceSb.append(price + ", ");
            }

            getView().getNameLabel().setText(product.getName());
            getView().getCompanyLabel().setText(product.getCompany());
            getView().getSerialLabel().setText(product.getSerialNumber());
            getView().getPricesLabel().setText(priceSb.toString());
            //break;

        }
    }   

}

ProductListView.ui.xml:

<g:HTMLPanel>
<table border="1">
    <tr>
        <td><g:Label ui:field="nameLabel" /> </td>
        <td><g:Label ui:field="companyLabel" /> </td> 
        <td><g:Label ui:field="serialLabel" /> </td>
        <td><g:Label ui:field="pricesLabel" /> </td> 
    </tr>
    </table>
</g:HTMLPanel> 

目前 JSON 中有两个产品。

下面是这段代码发生的情况:第一行​​出现 Product1,然后它变成第一行包含 Product2 的值,然后它再次包含Product1 的值,然后是 Product2 的值,之后出现带有 Product1 的第二行,然后它变为包含Product2 的值,然后它再次包含 Product1 的值,然后再次包含 Product2 的值。

因此,有两个产品和两行,在此代码中,值更改了两次,但最终表中仅包含 Product2 的值。如果 break; 未注释,Product1 的值在第一行中输出两次,然后在第二行中输出,则该表仅包含这些 Product1 的值。

我明白为什么会这样。但我还没有想出如何做出正确的输出。如果有人能告诉我如何进行正确的输出,或者提供一个例子(或者告诉我哪一部分,例如小部件的使用,是非常错误的),那就太好了。

最佳答案

您的代码存在的问题是您的 ProductListView.ui.xml 中确实没有真正的表格。

当然如果从服务器上取回了两条记录,这部分代码会被调用两次:

getView().getNameLabel().setText(product.getName());
getView().getCompanyLabel().setText(product.getCompany());
getView().getSerialLabel().setText(product.getSerialNumber());
getView().getPricesLabel().setText(priceSb.toString());

第二次调用覆盖第一次调用的值。

改进代码的要点:

  1. 您可能想阅读有关创建真实表格 View 的 CellTable。
  2. 不要使用 PresenterWidget 本身作为数据持有者,而是创建 将传递给数据库并使用它来检索的 DTO 数据。

关于java - 使用 GWTP 创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10530152/

相关文章:

java - 如何使用 JsonReader 解析嵌套的键和值?

Java多个对象返回相同的信息

java - 如何解决 Hibernate 异常 "IllegalArgumentException occurred while calling setter"的原因?

objective-c - Cocoa-Touch - 如何解析本地 Json 文件

JQuery 从 html 表中获取值

html - DIV 中 TABLE 的 CSS 布局问题

javascript - 动态删除每一行,同时附加行索引

java - Super() 首先要做的事情

java - “Cannot find symbol”或 “Cannot resolve symbol”错误是什么意思?

java - 如何检查字符串中的所有“字符是否都被转义