使用GWT生成UUID

cup*_*kob 14 gwt uuid

有没有办法用GWT生成UUID?

Ita*_*tto 4

您可以通过重用Javascript UUID 实现并使用 JSNI 包装它来在 GWT 中生成 UUID :

public native static String uuid() /*-{
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,
            function(c) {
                var r = Math.random() * 16 | 0, v = c == 'x' ? r
                        : (r & 0x3 | 0x8);
                return v.toString(16);
            });
}-*/;
Run Code Online (Sandbox Code Playgroud)

如果您只想在 GWT 元素中使用唯一 ID(而不是 UUID),请使用:

String id = DOM.createUniqueId();
Run Code Online (Sandbox Code Playgroud)