I'm working on a Liferay portlet and have some problems. What I want to achieve is to access some data created when rendering the page in a java class and pass it to my JavaScript. I think my issue is my understanding of the scope of variables.
I have figured out how to pass a variable to JavaScript in the view.jsp file from these two:
Creating liferay portlet - how to pass data to view.jsp from Java class?
http://www.opensource-techblog.com/2012/08/creating-custom-liferay-mvc-portlet.html
What I have now:
The java-servlet class:
public class TestPortlet extends MVCPortlet {
@Override
public void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException {
renderRequest.setAttribute("test_variable_actionRequest_setAttribute", "TestValue");
super.doView(renderRequest, renderResponse);
}
}
My view.jsp:
<%@page%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<portlet:defineObjects />
<%
String testVariable = (String)renderRequest.getAttribute("test_renderRequest_setAttribute");
%>
<b><%= testVariable %></b>
<script type="text/javascript">
$(document).ready(function() {
myNamespace.control.init();
});
</script>
The printing of the testVariable works here, printing out "TestValue" as the only output.
My control.js:
var myNamespace = myNamespace || {};
myNamespace.control = (function($, OL, ns) {
"use strict";
function init() {
//How on earth do I access the variable here?
console.log(testVariable); // error: testVariable not defined
console.log(renderRequest.getAttribute("test_renderRequest_setAttribute")); // error: renderRequest not defined
}
// public interface
return {
init : init,
};
}());
What I'm thinking here is that I obviously am quite lost on how the scope of these variables and the renderRequest object works. I'm sure the solution is quite simple but I can't seem to find it.