Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
RE: Liferay 7.2 - Update view.jsp with new java code
Hi,
I have a navigation bars layout with several tabs that change the tab content div when the user click each tab. The way this is working is by quering the information for each tab in th "doView" method from the portlet and then passing this information on the renderRequest and rendering on the view.jsp. But I am afraid that in a near future, if for some reason I set a lot of tabs, the porltet performance will drop drastically since it will have to do a lot of queries (at least one for each tab).
My question is: it is possible to execute a javascript function when the user click the tab, that calls a java method to query the information for this tab and finally pass this information to the view.jsp and update the layout without refreshing the portlet?
Maybe I can do this with "serveResource"? Or a behaviour like this should be done another way?
I have a navigation bars layout with several tabs that change the tab content div when the user click each tab. The way this is working is by quering the information for each tab in th "doView" method from the portlet and then passing this information on the renderRequest and rendering on the view.jsp. But I am afraid that in a near future, if for some reason I set a lot of tabs, the porltet performance will drop drastically since it will have to do a lot of queries (at least one for each tab).
My question is: it is possible to execute a javascript function when the user click the tab, that calls a java method to query the information for this tab and finally pass this information to the view.jsp and update the layout without refreshing the portlet?
Maybe I can do this with "serveResource"? Or a behaviour like this should be done another way?
Yes, you can do this with serveResource.
We usually create Rest services instead.
https://portal.liferay.dev/docs/7-1/reference/-/knowledge_base/r/rest-template
While we vastly prefer that approach, to create rest services (like getProductList, addProduct, deleteProduct, ...) and call them from some frontend application (we usually create it with vue.js, but that's just an arbitrary choice). But this approach forces you to create a different kind of application. Portlets become more or less empty hulls and all the magic is done in javascript and through rest services.
For one tab (or maybe a few), the serveResource approach is quite fine. If you have a lot of things you want to do with ajax, well, then it becomes clumsy since you need to send everything to that serveResource method and dispatch from there.
We usually create Rest services instead.
https://portal.liferay.dev/docs/7-1/reference/-/knowledge_base/r/rest-template
While we vastly prefer that approach, to create rest services (like getProductList, addProduct, deleteProduct, ...) and call them from some frontend application (we usually create it with vue.js, but that's just an arbitrary choice). But this approach forces you to create a different kind of application. Portlets become more or less empty hulls and all the magic is done in javascript and through rest services.
For one tab (or maybe a few), the serveResource approach is quite fine. If you have a lot of things you want to do with ajax, well, then it becomes clumsy since you need to send everything to that serveResource method and dispatch from there.
Hi Christoph,
Thanks for your help! I was able to do this with the serveResource. For future reference I will explain on how I did this.
I added a function to my tab click:
Then the serveResource method is called:
And finally the success callback from my previous A.io.request is called with the responseData that I have passed form Java and I create my HTML string and append it to the correct place with the new information.
Thanks for your help! I was able to do this with the serveResource. For future reference I will explain on how I did this.
I added a function to my tab click:
<liferay-portlet:resourceurl var="updateTabContent" />
function selectTab(date) {
AUI().use('aui-io-request', function(A) {
A.io.request('${ updateTabContent }', {
method: 'post',
data: {
'<portlet:namespace />date': date,
},
on: {
success: function() {
updateTab(this.get('responseData'));
}
}
});
});
}
Then the serveResource method is called:
@Override
public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws IOException, PortletException {
/* Request API and creating my information */
PrintWriter printWriter = resourceResponse.getWriter();
printWriter.println(myInformation);
printWriter.flush();
super.serveResource(resourceRequest, resourceResponse);
}
And finally the success callback from my previous A.io.request is called with the responseData that I have passed form Java and I create my HTML string and append it to the correct place with the new information.
Thanks for all the readers in advance, it is always nice when someone posts sample code!
One thing though:
This is probably irrelevant for you since you wrote that you just want to send tab content, but it could be relevant for others who take your code as an example.
If the serveResource can be called with get requests too (e.g. if it has just simple parameters like some ids) and writes data, you should protect it from Cross Site Request forgery. Otherwise somebody could craft a get request, send it to you by email and when you click it and are authenticated, you execute the serveResource call with your permissions.
Liferay protects requests by sending the themedisplay.authToken parameter with each request and checking it.
One thing though:
This is probably irrelevant for you since you wrote that you just want to send tab content, but it could be relevant for others who take your code as an example.
If the serveResource can be called with get requests too (e.g. if it has just simple parameters like some ids) and writes data, you should protect it from Cross Site Request forgery. Otherwise somebody could craft a get request, send it to you by email and when you click it and are authenticated, you execute the serveResource call with your permissions.
Liferay protects requests by sending the themedisplay.authToken parameter with each request and checking it.
Copyright © 2025 Liferay, Inc
• Privacy Policy
Powered by Liferay™