创建Liferay Portlet-如何将数据从Java类传递到view.jsp? [英] Creating liferay portlet - how to pass data to view.jsp from Java class?

查看:80
本文介绍了创建Liferay Portlet-如何将数据从Java类传递到view.jsp?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图仅通过名为view.jsp的JSP文件在liferay中创建portlet.我需要的是:

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

  1. 在加载portlet时,我想在生成数组的地方调用自定义Java类.

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

我需要将该数组传递给view.jsp.

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

该怎么做?

推荐答案

您是否已使用Liferay中的create.sh脚本创建了portlet?在这种情况下,我们将需要创建一个扩展MVCPortlet:

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 {

}

此外,您将必须更改WEB-INF/portlet.xml文件以指向其类.将下面的行替换为

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>

通过命名您的Portlet类:

by one naming your portlet class:

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

这只是设置.现在,最酷的部分是:代码!您应该创建一种用于处理Portlet视图的方法.此方法应命名为doView(),并具有两个参数:RenderRequestRendertResponse.此外,它还会引发一些异常,并将Portlet呈现委托给超类方法:

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);
}

但是,在呈现portlet之前,我们创建数组:

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

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

并将其放入作为参数接收的RenderRequest中.您应该给参数命名-在这种情况下,名称为"my-array":

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);
    }
}

我们是通过RenderRequest对象将值传递给JSP的.现在,在JSP中,我们应该导入" RenderRequest实例(以及其他对象),并将<portlet:defineObjects />标记添加到JSP的开头.之后,我们可以通过renderRequest对象的名称获取任何属性.请注意,方法getAttribute()返回Object,因此您应将其返回值转换为正确的类型:

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:

这篇关于创建Liferay Portlet-如何将数据从Java类传递到view.jsp?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆