问题
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?
回答1:
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
.
- So either you can use
actionURL
to call different methods but would refresh the page which you don't want as I see - OR, else you can set a parameter in the
resourceURL
to let theserveResource
method know what you want to return and then in theserveResource
method of the portlet haveswitch-case
orif-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
.
回答2:
You can pass parameter under resourceURL and check that parameter under serveResorce method. Based on that parameter call your function or code
i.e
<portlet:resourceURL var="userdetail" >
<portlet:param name="userinfo" value="true"></portlet:param>
</portlet:resourceURL>
来源:https://stackoverflow.com/questions/16811854/display-data-on-jsp-based-on-selected-date-from-liferay-datepicker