GWT:捕获GET请求中的URL参数

时间:2021-03-06 10:25:24

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参数?

4 个解决方案

#1


85  

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

P.S. 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库在querystring中提取param的值。

#2


19  

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#place instead of http://app.com/#place&param=value

http://app.com/?参数值= #的地方而不是http://app.com/ place&param =价值

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

为了在地图上得到所有的参数,请使用:

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

#3


0  

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

我建议您使用GWT MVP。假设您的url为

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

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

And in your AppController.java --

和你有一个。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();
        }
  }
}

#4


0  

You can use the Activities and Places to 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 Activity associated with the place (in ActivityMapper).

你可以利用这些活动和地方去做。当为页面创建位置时,可以将orderId设置为成员。当您创建与该位置关联的活动(在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作为常规参数发送。您将不得不使用这个表单的url:

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

#1


85  

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

P.S. 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库在querystring中提取param的值。

#2


19  

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#place instead of http://app.com/#place&param=value

http://app.com/?参数值= #的地方而不是http://app.com/ place&param =价值

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

为了在地图上得到所有的参数,请使用:

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

#3


0  

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

我建议您使用GWT MVP。假设您的url为

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

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

And in your AppController.java --

和你有一个。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();
        }
  }
}

#4


0  

You can use the Activities and Places to 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 Activity associated with the place (in ActivityMapper).

你可以利用这些活动和地方去做。当为页面创建位置时,可以将orderId设置为成员。当您创建与该位置关联的活动(在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作为常规参数发送。您将不得不使用这个表单的url:

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