I have a datepicker. What I want to achieve is: based on the date selected by user, the data for that day should be displayed on the jsp. I have used a serveResource function for displaying some data in some other form. Should I create another such function such that based on selected date, data is displayed for that particular data?
You are using serveResource
so you want to fetch data using ajax.
Unfortunately you can't have resourceURL
behave the same as actionURL
in otherwords you can't have multiple methods for serving resourceRequest
like you have to serve actionRequest
.
actionURL
to call different methods but would refresh the page which you don't want as I seeresourceURL
to let the serveResource
method know what you want to return and then in the serveResource
method of the portlet have switch-case
or if-else
to determine from where has this request come.Going with the second approach:
You jsp would look something like this (might not work as it is):
<aui:button value="Click to get today's data" onClick="fetchCurrentDateData()" />
<portlet:resourceURL var="currentDateDataResourceURL" >
<portlet:param name="whicData" value="currentDateData" />
</portlet:resourceURL>
<aui:script>
function fetchCurrentDateData() {
A.io.request(
currentDateDataResourceURL, {
dataType: 'text', // since you might want to execute a JSP and return HTML code
on: {
success: function(event, id, xhr) {
A.one("#theDivWhereNeedsToBeDisplayed").html(this.get('responseData'));
},
failure: function(event, id, xhr){
}
}
}
);
}
</aui:script>
Your serveResource
method would be something like this (I am assuming your portlet extends MVCPortlet
class of liferay):
public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) {
String strWhichData = ParamUtil.getString(resourceRequest, "whicData");
if (strWhichData.equals("currentDateData")){
// call service layer to fetch all the current date data
// this is a method in MVCPortlet if you are extending liferay's MVCPortlet
include("/html/myportlet/viewCurrentData.jsp", resourceRequest, resourceResponse);
} else if (strWhichData.equals("otherAjaxStuff")) {
// do you other stuff and return JSON or HTML as you see fit
} else {
// do your normal stuff
// can return JSON or HTML as you see fit
}
}
Note:
As a side-note you can try Spring MVC portlet which gives you the ability to have different methods for serving resourceRequest
.