3 components here. init.jsp, view.jsp and the portlet, in this case I call it AjaxTestPortlet and it extends GenericPortlet. init.jsp contains the basic setup stuff like "page" and "taglib" imports for liferay jsps.
Here is the code for the view.jsp
<%@ include file="init.jsp" %>
<portlet:resourceURL var="testLink" id="test22" />
<h1>Ajax Test</h1>
<div id="<portlet:namespace />testDiv">
<label for="<portlet:namespace />name">Enter Name</label>
<div>
<input id="<portlet:namespace />name" name="usernameField" type="text" maxlength="255" value=""/>
</div>
<label for="<portlet:namespace />age">Enter Age</label>
<div>
<input id="<portlet:namespace />age" name="incidentField" type="text" maxlength="255" value=""/>
</div>
</div>
<button onClick="javascript:<portlet:namespace />ajaxTest()">Test Ajax</button>
<script type="text/javascript">
function <portlet:namespace />ajaxTest()
{
var url = '<%= testLink.toString() %>';
AUI().use( function(A){
var name1 = document.getElementById("<portlet:namespace />name").value;
var age1 = document.getElementById("<portlet:namespace />age").value;
A.io.request(url, {
data: {
name: name1,
age: age1
},
dataType: 'json',
on: {
failure: function(){
},
success: function(event, id, obj)
{
console.log("Ajax Test worked!!!!" + name1 + " and " + age1);
alert("Ajax Test Worked!!!!!" + name1 + " and " + age1);
}
}
});
});
}
</script>
And here is the code for the AjaxTestPortlet
import java.io.IOException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class AjaxTestPortlet extends GenericPortlet {
private static Log log = LogFactory.getLog(AjaxTestPortlet.class);
protected static String viewJSP = "";
public void init() throws PortletException {
viewJSP = getInitParameter("view-jsp");
}
public void processAction(ActionRequest aReq, ActionResponse aResp) throws IOException, PortletException {
}
public void doView(RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
include(viewJSP, renderRequest, renderResponse);
}
protected void include(String path, RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
PortletRequestDispatcher portletRequestDispatcher = getPortletContext()
.getRequestDispatcher(path);
if (portletRequestDispatcher == null) {
log.error(path + " is not a valid include!");
} else {
portletRequestDispatcher.include(renderRequest, renderResponse);
}
}
}