我熟悉GWTP。我试图在Piriti映射器的帮助下输出一个包含JSON值的表。这不是一个真正的项目代码,它只是尝试理解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
' s值,然后再次Product2
,然后出现Product1
的第二行,然后它会更改为包含Product2
的值的第二行,然后再次包含Product1
1}}的值,然后再次Product2
。
因此,有两个产品和两行,并且在此代码中,值会更改两次,但最后该表仅包含Product2
的值。如果取消注释break;
,则Product1
的值在第一行输出两次,然后在第二行输出,则表格仅包含这些Product1
的值。
我明白为什么会这样。但我还没弄明白如何制作正确的输出。如果有人可以告诉我如何做正确的输出,或者,提供一个例子(或者告诉我哪个部分,例如小部件的使用,是非常错误的),那就太棒了。
答案 0 :(得分:1)
您的代码存在的问题是您的ProductListView.ui.xml中确实没有真正的表。
当然,如果从服务器检索到两条记录,则会将这部分代码调用两次:
getView().getNameLabel().setText(product.getName());
getView().getCompanyLabel().setText(product.getCompany());
getView().getSerialLabel().setText(product.getSerialNumber());
getView().getPricesLabel().setText(priceSb.toString());
第二个电话会覆盖第一个电话的价值。
改进代码的要点: