Below code represents, How to create UUID using Liferay API with AJAX call :
In JSP File :
In Controller Class :
In JSP File :
<portlet:resourceURL var="generateUUID" id="generateUUID"/> <a href="javascript:void(0);" onclick="javascript:loadUUID();"> Generate UUID </a>In JSP File (<aui:script>) :
var generateUUID = loadUUID(); alert(generateUUID); function loadUUID(){ var uuid = ""; A.io.request('${generateUUID}', { method: 'POST', dataType: 'json', data: { generateUUID: true }, sync: true, on: { success: function() { var test = this.get('responseData'); uuid = test.uuid; }, failure: function() { alert('failure'); } } }); return uuid; }
In Controller Class :
private String generateUUID() { return UUID.randomUUID().toString(); }In Controller Class (ServeResource Method):
boolean generateUUID = ParamUtil.getBoolean(aResourceRequest, "generateUUID"); if (generateUUID) { aResourceResponse.getWriter().print( JSONFactoryUtil.createJSONObject().put("uuid", generateUUID().toString())); return; }