Java GWT:在 GET 请求中捕获 URL 参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/590580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:36:23  来源:igfitidea点击:

GWT: Capturing URL parameters in GET request

javagwt

提问by Otavio

I need to build a GWT application that will be called by an external application with specific URL parameters.

我需要构建一个 GWT 应用程序,该应用程序将由具有特定 URL 参数的外部应用程序调用。

For example:

例如:

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

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

How do I capture the orderId parameter inside the GWT application?

如何在 GWT 应用程序中捕获 orderId 参数?

采纳答案by Ray Lu

Try,

尝试,

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

P.S. GWT can invoke native javascript which means if javascript can do the stuff, GWT can do it too; e.g. in GWT, you can write

PS GWT 可以调用原生 javascript,这意味着如果 javascript 可以做到这些,GWT 也可以做到;例如在 GWT 中,你可以写

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

In this case, you can even use existing javascript lib to extract param's value in the querystring.

在这种情况下,您甚至可以使用现有的 javascript 库来提取查询字符串中的参数值。

回答by supercobra

GWT has a facility to get params from the URL:

GWT 具有从 URL 获取参数的功能:

String value = Window.Location.getParameter("param");

Make sure your URLs are in the form of:

确保您的网址采用以下形式:

http://app.com/?param=value#placeinstead of http://app.com/#place&param=value

http://app.com/?param=value#place而不是http://app.com/#place¶m=value

In order to get all params in a map, use:

为了获取地图中的所有参数,请使用:

Map<String, List<String>> map = Window.Location.getParameterMap();

回答by Cataclysm

I suggest you to use GWT MVP. Assume that your url as

我建议你使用GWT MVP。假设您的网址为

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

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

And in your AppController.java--

在你的AppController.java 中——

Try as

尝试作为

    ......
    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
        }
    }
}
...........

Another option , you can also use with Spring MVC. Here is an example ...

另一种选择,您也可以与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();
        }
  }
}

回答by vlad_dd

You can use the Activitiesand Placesto do that. When you create the Place for your page, you can set the orderId as a member. This member can be used afterwords when you create the Activityassociated with the place (in ActivityMapper).

您可以使用ActivitiesPlaces来做到这一点。当您为您的页面创建 Place 时,您可以将 orderId 设置为成员。当您创建Activity与地点相关的(在ActivityMapper 中)时,可以在后记中使用此成员。

The only restriction is that you can't send the orderId as a normal parameter. You will have to use an url with this form :

唯一的限制是您不能将 orderId 作为普通参数发送。您将必须使用具有以下形式的网址:

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