Creating liferay portlet - how to pass data to view.jsp from Java class?

99封情书 提交于 2019-12-18 10:53:37

问题


I'm trying to create portlet in liferay with just only from a JSP file called view.jsp. What I need is:

  1. When the portlet loads, I want to call custom Java class where I generate an array.

  2. I need to pass that array to the view.jsp.

How to do that?


回答1:


Have you created your portlet with the create.sh script from Liferay? In this case, we will need to create a new portlet class that extends MVCPortlet:

public class ArrayPortlet extends MVCPortlet {

}

Also, you will have to change the WEB-INF/portlet.xml file to point to its class. Replace the line below by

 <portlet-class>com.liferay.util.bridges.mvc.MVCPortlet</portlet-class>

by one naming your portlet class:

<portlet-class>br.com.seatecnologia.arrayportlet.ArrayPortlet</portlet-class>

This is just setup. Now, the cool part: code! You should create a method for handling the view of the portlet. This method should be named doView() and has two parameters: a RenderRequest and a RendertResponse. Also, it throws some exceptions and delegate the portlet rendering to the superclass method:

public void doView(RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
    super.doView(renderRequest, renderResponse);
}

Before rendering the portlet, however, we create our array:

String[] array = new String[] { "foo", "bar", "baz" };

and put it in the RenderRequest received as parameter. You should give a name to the parameter - in this case, the name is "my-array":

renderRequest.setAttribute("my-array", array);

This is our class, complete:

public class ArrayPortlet extends MVCPortlet {
    @Override
    public void doView(RenderRequest renderRequest, RenderResponse renderResponse)
    throws IOException, PortletException {
        String[] array = new String[] { "foo", "bar", "baz" };
        renderRequest.setAttribute("my-array", array);
        super.doView(renderRequest, renderResponse);
    }
}

It is through the RenderRequest object that we pass values to the JSP. Now, in the JSP, we should "import" the RenderRequest instance (and other objects as well) adding the <portlet:defineObjects /> tag to the beginning of the JSP. After this, we can get any attribute from the renderRequest object through its name. Note that the method getAttribute() returns Object so you should cast its return value to the correct type:

<portlet:defineObjects />
<%
String[] anArrayFromMyPortlet = (String[])renderRequest.getAttribute("my-array");
%>

Now, you just use your retrieved object as you wish:

<ul>
<% for (String string : anArrayFromMyPortlet) { %>
<li><%= string %></li>
<% } %>
</ul>

This is the result of this code in my machine:




回答2:


Your question is quite vague, I recommend to get some introduction to portlet development as it seems to be about the basic understanding of the infrastructure/specification.

An attempt to answer your question by giving some pointers - apologies if they're also too vague:

  • init() is part of the portlet lifecycle and will be called once, when the portlet class is loaded.
  • You can pass values to your view by (for example) adding request attributes to your RenderRequest in doView()

of course this varies with the portlet framework that you're using - the methods mentioned above are from the underlying portlet specification (i.e. JSR-286)




回答3:


You can do this in several ways :

1) You can use renderRequest.setAttribute("my-array", array);

2) You can put your data into a session

request.getPortletSession().setAttribute("my-array", array);

In the second case don't forget to delete your element from the session when you don't need it any more.



来源:https://stackoverflow.com/questions/6894095/creating-liferay-portlet-how-to-pass-data-to-view-jsp-from-java-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!