我开始熟悉 GWTP。我试图在 Piriti 映射器的帮助下输出一个包含 JSON 值的表。它不是一个真正的项目代码,它只是一个尝试理解 GWTP,所以这可能不是最漂亮的解决方案(事实上,它不是一个肯定的)。以下是此过程中涉及的两位演示者:
(FirstPresenter
使用ProductListPresenter
,这是一个小部件,我不确定是否应该在这里使用小部件,但是,根据这个对话,小部件可能会起作用):
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
's 值。如果break;
未注释,则Product1
' 值在第一行输出两次,然后在第二行中,则表仅包含这些Product1
' 值。
我明白为什么会这样。但我还没有弄清楚如何做出正确的输出。如果有人能告诉我如何做正确的输出,或者,提供一个例子(或者告诉我哪个部分,例如小部件的使用,是非常错误的),那就太好了。