2009-02-26 33 views
53

我需要構建一個GWT應用程序,該應用程序將由具有特定URL參數的外部應用程序調用。GWT:在GET請求中捕獲URL參數

例如:

http://www.somehost.com/com.app.client.Order.html?orderId=99999

如何捕獲GWT應用程序中的orderId參數?

+0

我正在處理類似的問題,你如何gwt應用程序響應像orderID = 99999參數?我的意思是它如何處理這樣的參數? – xybrek 2011-08-21 00:57:51

+0

@xybrek如果你不考慮在GWT中提供的[歷史機制](http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsHistory.html)(它利用#來避免頁面重新加載),你會需要在主入口點的`onLoad()`方法中調用一個方法。每次調用頁面時,都會調用您的專用方法,在該方法中可以檢查和處理給定的參數。 – Dennis 2014-05-22 23:28:54

回答

83

嘗試,

String value = com.google.gwt.user.client.Window.Location.getParameter("orderId"); 
// parse the value to int 

附: GWT可以調用原生javascript,這意味着如果JavaScript可以做到這一點,GWT也可以做到這一點;例如在GWT,你可以寫

public static native void alert(String msg) 
/*-{ 
$wnd.alert("Hey I am javascript"); 
}-*/; 

在這種情況下,你甚至可以使用現有的JavaScript LIB查詢字符串中提取PARAM的值。

+1

@codemeit,把這個放在gwt代碼中?在主要入口點?例如,我有http://mygwtapp.com/process.html?query=123將被其他一些網絡應用程序調用,或者只是手寫在瀏覽器欄中。 – xybrek 2011-08-21 02:09:34

0

我建議y ou使用GWT MVP。 假設您的網址爲

http://www.myPageName/myproject.html?#orderId:99999

而在你AppController.java -

嘗試爲

...... 
    public final void onValueChange(final ValueChangeEvent<String> event) { 
    String token = event.getValue(); 

    if (token != null) { 
     String[] tokens = History.getToken().split(":"); 
     final String token1 = tokens[0]; 
     final String token2 = tokens.length > 1 ? tokens[1] : ""; 

     if (token1.equals("orderId") && tonken2.length > 0) { 
      Long orderId = Long.parseLong(token2); 
      // another your operation 
     } 
    } 
} 
........... 

另一種選擇,你還可以與使用Spring MVC。下面是一個例子...

// Below is in your view.java or presenter.java 

Window.open(GWT.getHostPageBaseURL() + "customer/order/balance.html?&orderId=99999", 
      "_self", "enable"); 

// Below code in in your serverside controller.java 

@Controller 
@RequestMapping("/customer") 
public class ServletController { 
@RequestMapping(value = "/order/balance.html", method = RequestMethod.GET) 
public void downloadAuctionWonExcel(@RequestParam(value = "orderId", required = true) final String orderId, 
    final HttpServletResponse res) throws Exception { 
    try { 
     System.out.println("Order Id is "+orderId); 
     // more of your service codes 
     } 
     catch (Exception ex) { 
     ex.printStackTrace(); 
     } 
    } 
} 
0

您可以使用ActivitiesPlaces做到這一點。當您爲頁面創建Place時,可以將orderId設置爲成員。當您創建與該地點相關聯的Activity(在ActivityMapper中)時,可以使用該成員的後綴。

唯一的限制是您無法將orderId作爲正常參數發送。你將不得不使用這種形式的網址:

127.0.0.1:60206/XUI.html?#TestPlace:orderId=1