54

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

For example:

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

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

Sri Harsha Chilakapati
  • 11,744
  • 6
  • 50
  • 91
Otavio
  • 1,157
  • 4
  • 12
  • 19
  • I am working on a similar problem, how does you gwt app reponds to the parameter like, orderID=99999? I mean how does it handle such parameter? – quarks Aug 21 '11 at 00:57
  • @xybrek If you do not consider the provided [History mechanism](http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsHistory.html) in GWT (which makes use of the # to avoid page reloading), you will need to place a method in your main entry point which is invoked in the main entry point's `onLoad()` method. Every time the page is called it will invoke your dedicated method in which you can inspect and process the given parameters. – Dennis May 22 '14 at 23:28

4 Answers4

87

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

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.

walen
  • 7,103
  • 2
  • 37
  • 58
Ray Lu
  • 26,208
  • 12
  • 60
  • 59
  • 1
    @codemeit, where to put this in the gwt code? In the main entry point? For example, I have http://mygwtapp.com/process.html?query=123 that will be called by some other web app, or just hand written in the browser bar. – quarks Aug 21 '11 at 02:09
19

GWT has a facility to get params from the 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

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

Map<String, List<String>> map = Window.Location.getParameterMap();
supercobra
  • 15,810
  • 9
  • 45
  • 51
  • 1
    This method `Window.Location.getParameter("param")` works best for my GWT application since it can be placed everywhere. – eQ19 Mar 29 '15 at 18:19
1

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

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

And in your 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 ...

// 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();
        }
  }
}
Cataclysm
  • 7,592
  • 21
  • 74
  • 123
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).

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 :

127.0.0.1:60206/XUI.html?#TestPlace:orderId=1
Eduardo Briguenti Vieira
  • 4,351
  • 3
  • 37
  • 49
vlad_dd
  • 11
  • 1