0

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.

Community
  • 1
  • 1
Plux
  • 414
  • 4
  • 15

1 Answers1

0

What you are doing above is not creating a javascript variable called testVariable. You're just printing the value of the Java variable testVariable in HTML as bold text but not into the javascript.

I think what you want is:

 <script type="text/javascript">
   var testVariable  = "<%= testVariable %>";
   $(document).ready(function() {
     myNamespace.control.init();
   });
 </script>

As for using *renderRequest.getAttribute("test_renderRequest_setAttribute")* in javascript, I don't know much about liferay, so I don't know if renderRequest is somehow defined on the javascript side via some javascript library that's part of liferay, but intuitively I would guess its only defined on the Java side.

developerwjk
  • 8,619
  • 2
  • 17
  • 33
  • Thanks for an answer. I think you are correct on that it is only accessible serverside during render phase. Isn't there a more common easy way to pass variables/objects etc from the servlet to be accessible by the JavaScript? What you write here does not strike me as recommended coding practices. Should I perhaps consider Ajax? – Plux Oct 05 '13 at 23:23
  • Cookies, Ajax, or this are the only ways I know of. – developerwjk Oct 07 '13 at 20:28