我需要构建一个 GWT 应用程序,该应用程序将由具有特定 URL 参数的外部应用程序调用。
例如:
http://www.somehost.com/com.app.client.Order.html?orderId=99999。
如何在 GWT 应用程序中捕获 orderId 参数?
我需要构建一个 GWT 应用程序,该应用程序将由具有特定 URL 参数的外部应用程序调用。
例如:
http://www.somehost.com/com.app.client.Order.html?orderId=99999。
如何在 GWT 应用程序中捕获 orderId 参数?
尝试,
String value = com.google.gwt.user.client.Window.Location.getParameter("orderId");
// parse the value to int
PS GWT 可以调用原生 javascript,这意味着如果 javascript 可以做这些事情,GWT 也可以;例如在 GWT 中,你可以写
public static native void alert(String msg)
/*-{
$wnd.alert("Hey I am javascript");
}-*/;
在这种情况下,您甚至可以使用现有的 javascript 库在查询字符串中提取参数的值。
GWT 具有从 URL 获取参数的功能:
String value = Window.Location.getParameter("param");
确保您的 URL 采用以下形式:
http://app.com/?param=value#place而不是http://app.com/#place¶m=value
为了获取地图中的所有参数,请使用:
Map<String, List<String>> map = Window.Location.getParameterMap();
我建议你使用GWT MVP。假设您的网址为
在你的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();
}
}
}
您可以使用Activities
andPlaces
来做到这一点。当您为您的页面创建 Place 时,您可以将 orderId 设置为成员。Activity
当您创建与地点相关联时(在ActivityMapper中) ,可以在后记中使用该成员。
唯一的限制是您不能将 orderId 作为普通参数发送。您必须使用此表单的 url:
127.0.0.1:60206/XUI.html?#TestPlace:orderId=1