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
Liferay 7.2 - Increment java variable in javascript
Hi,
I have the following code on my view.jsp:
What I want to do is increment the pageNumber when I click "Next Page", and I will get my data according to this pageNumber. The problem is that doing like this, the pageNumber is not incrementing. The alert keep showing me the pageNumber = 1. How should I increment my java variable on javascript?
I have the following code on my view.jsp:
<% int pageNumber = 1; %>
<aui:container>
<span id="nextPage"> <liferay-ui:message key="next.page" /> </span>
</aui:container>
<script>
AUI().ready('aui-base', function(A) {
A.one('#nextPage').on('click', function() {
<% pageNumber++; %>
alert(<%= pageNumber %>);
addSomething();
});
});
function addSomething() {
<% for (int i = <%= pageNumber * 16 %>; i < pageNumber * 16; i++) %>
// will append some html
}
</script>
What I want to do is increment the pageNumber when I click "Next Page", and I will get my data according to this pageNumber. The problem is that doing like this, the pageNumber is not incrementing. The alert keep showing me the pageNumber = 1. How should I increment my java variable on javascript?
Fabio Carvalho:
<% int pageNumber = 1; %> <script> AUI().ready('aui-base', function(A) { A.one('#nextPage').on('click', function() { <% pageNumber++; %> alert(<%= pageNumber %>); addSomething(); }); }); </script>
What I want to do is increment the pageNumber when I click "Next Page", and I will get my data according to this pageNumber. The problem is that doing like this, the pageNumber is not incrementing. The alert keep showing me the pageNumber = 1. How should I increment my java variable on javascript?
It doesn't work this way. While the JSP is processed server side, it's generating output. Once that's done, you'll get HTML code that translates roughly to this:
<script>
AUI().ready('aui-base', function(A) {
A.one('#nextPage').on('click', function() {
alert(1);
addSomething();
});
});
</script>
During evaluation of your Javascript function, there's no communication between the server and the browser, and all the code limited by <% %> has already been evaluated, and the code in <%= %> has been embedded in the generated HTML output. Thus, the browser never ever sees your java variable and couldn't care less how the page was generated (look at the generated HTML code: There's no trace left. Depending on what actually happens, you'll (most likely) need to initialize your pageNumber to some value derived from a HTTP parameter, rather than always to 1. Or use a different approach - e.g. fully in Java or fully in Javascript.
Hi Olaf,
Ok, I understood. I modified my code a bit:
And in my Java class:
Ok, now I can get the pageNumber on my Java class and create new objects and set it as attribute.
Now, can I somehow refresh the container of my view.jsp to populate with those new dataSources? Maybe refresh the attribute on my view.jsp and recreate only the dataSources container.
Ok, I understood. I modified my code a bit:
<% List<datasource> dataSources = (List<datasource>) request.getAttribute("dataSources"); %>
<portlet:resourceurl var="changePageURL" />
<aui:container cssclass="data-sources-container">
<% for (int i = 0; i < dataSources.size(); i++) { %>
// populate according to my dataSources
<% } %>
</aui:container>
<aui:container cssclass="pagination-container">
<button type="button" onclick="nextPage()"> <liferay-ui:message key="next.page" /> </button>
</aui:container>
<script>
var pageNumber = 0;
function nextPage() {
pageNumber++;
AUI().use('aui-io-request', function(A){
A.io.request('${changePageURL}', {
method: 'post',
data: {
<portlet:namespace />pageAction: 'next',
<portlet:namespace />pageNumber: pageNumber
}
});
});
}
</script></datasource></datasource>
And in my Java class:
@Override
public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws IOException, PortletException {
int pageNumber = ParamUtil.getInteger(resourceRequest, "pageNumber");
for (int i = pageNumber * 16 + 1; i <= pageNumber * 16 + 16; i++)
dataSources.add(new DataSource(i));
resourceRequest.setAttribute("dataSources", dataSources);
super.serveResource(resourceRequest, resourceResponse);
}
Ok, now I can get the pageNumber on my Java class and create new objects and set it as attribute.
Now, can I somehow refresh the container of my view.jsp to populate with those new dataSources? Maybe refresh the attribute on my view.jsp and recreate only the dataSources container.
Copyright © 2025 Liferay, Inc
• Privacy Policy
Powered by Liferay™