开发可配置portlet--config.jsp

来源:互联网 发布:知乎 自己拍婚纱照 编辑:程序博客网 时间:2024/05/16 10:53

一、什么是可配置portlet

    其实portlet本来就是可以配置的,但我们的开发大部分情况下只使用view模式,edit和config模式一般没有使用,对于使用editor和config等模式的portlet,我们可以将他们称为可配置portlet。通过使用可配置portlet,可以做许多个性化定制。

应用场景:

1、如果在首页上有展现专题的地方,可以建立一个专题展现的portlet,这个地方要展现的内容为一个图片或多个图片,点击图片可以跳转相应的链接。但是专题可能需要变化,则这里可以添加一个config或edit模式来让管理员通过配置参数来定制。

2、如首页的新闻栏目,设计时是展现的A栏目,但是实际中用户可能有变化,需要换成其他栏目,同样可以通过可配置portlet来满足。

3、如提供RSS订阅,我们可以在配置项里面设置RSS的输出方法为标题或者是摘要或者是全文,标准可以为atom或者rss2.0等配置。

4、如有一个指标展现,用户需求为可定制的,用户A可以选择柱状图、用户B可以选择折线图、用户C可以选择饼图等。

包括但不限于以上场景,需要通过配置来适用不同的情况,为用户提供可配置选项的地方都可以使用可配置portlet。

可配置portlet的开发方式

可配置portlet的开发方式,我按数据的存储方式的不同,大概的分为两种。一种为使用PortletPreferences存储的,一种为自定义数据表结构存储的。

使用PortletPreferences存储的方式为将配置数据以键值对的形式存储于PortletPreferences的相关属性字段里面。以portlet的实例ID做为识别进行存储信息,适用于配置信息不算太复杂的场景。

如果配置信息比较复杂,推荐建立相关的数据库,将配置信息存储于数据库中。本文主要介绍存储于PortletPreferences中的方法,存储于数据库和普通的portlet的数据存储方法类似。

如果按模式的不同,又可以分为edit、config、help等不同的模式。这些对应于建立portlet时选用的modes的不同而不同,这里主要介绍config模式。其他模式类似。

使用Config模式

1、需要有一个建立好的portlet。portlet的创建,参考前面的portlet简述:http://www.huqiwen.com/2012/09/03/liferay-6-1-development-study-3-portlet-explicate/

2、在Liferay-portlet.xml里面找到此portlet的相关配置,在里面添加configuration-action-class元素,如下:

<portlet>

 <portlet-name>customjspportlet</portlet-name>

 <icon>/icon.png</icon>

 <configuration-action-class>xx.xxx.xxx.customjspportlet.CustomJspConfigurationAction</configuration-action-class>

 <instanceable>true</instanceable>  注:表示 是否 可实例多个

 <header-portlet-css>/css/main.css</header-portlet-css>

 <footer-portlet-JavaScript>

 /js/main.js

 </footer-portlet-javascript>

 <css-class-wrapper>customjspportlet-portlet</css-class-wrapper>

 </portlet>

3、建立CustomJspConfigurationAction类,此类继承自DefaultConfigurationAction即可。

4、重写其中的render方法。如下。

public Stringrender(PortletConfig portletConfig, RenderRequest renderRequest, RenderResponserenderResponse){

String portletId =renderRequest.getParameter("portletResource");

 PortletPreferences preferences =PortletPreferencesFactoryUtil.getPortletSetup(renderRequest, portletId);

renderRequest.setAttribute("customjspConfig_page_title",preferences.getValue("customjspConfig_page_title",StringPool.BLANK));

 renderRequest.setAttribute("customjspConfig_page_link",preferences.getValue("customjspConfig_page_link",StringPool.BLANK));

return"/html/CustomJspPortlet/config.jsp";

}

这个方法是点击portlet中的配置时进入的方法。在这个方法里面做以下几件事情。

1):我们获取到了当前portlet的PortletPreferences。

2):从PortletPreferences里面获取key为customjspConfig_page_title和customjspConfig_page_link的数据,并将他们放到request里面。

3):告诉Liferay我的配置页的JSP的路径是哪个。

5、编写config.jsp页面。config.jsp页面里面是我们要配置的一此参数信息,大部分情况下是一个展现的表单。主要内容可以参考如下(从项目中截取的部分代码,为说明问题已经简化):

<formaction="<liferay-portlet:actionURLportletConfiguration="true" />"name="<portlet:namespace />fm" id="<portlet:namespace/>fm" method="post">

<ul>

 <li>

 <span>标题:</span>

 <input tabindex="1"  type="text" name="<portlet:namespace/>customjspConfig_page_title" id="<portlet:namespace/>customjspConfig_page_title" value="<%=title%>" />

 </li>

 <li>

 <span>链接地址:</span>

 <input id='<portlet:namespace/>custom_page_link' name='<portlet:namespace/>customjspConfig_page_link' type="text" value="<%=link%>" />

 </li>

 <li>

 <input type="button"value="" onclick="<portlet:namespace/>saveConfig()">

 </li>

</ul>

</form>

这里的关键点为form的action,所有这种模式的可配置portlet的action都可以固定为:<liferay-portlet:actionURLportletConfiguration="true" />。

6、服务端保存配置信息的处理。步骤5中的action会进入步骤3建立的配置类的processAction方法。在上面的配置类里重写processAction方法。里面的内容如下:

public voidprocessAction(PortletConfig portletConfig, ActionRequest actionRequest,ActionResponse actionResponse)

 throws Exception {

 String portletResource = ParamUtil.getString(actionRequest,"portletResource");

PortletPreferencespreferences = PortletPreferencesFactoryUtil.getPortletSetup(actionRequest,portletResource);

if(Validator.isNotNull(preferences)) {

//从request里面取数据

 String title = ParamUtil.getString(actionRequest,"customjspConfig_page_title");

 String link =ParamUtil.getString(actionRequest, "customjspConfig_page_link");

//将数据以键值对的形式填充到preferences里面

 preferences.setValue("customjspConfig_page_title",title);

 preferences.setValue("customjspConfig_page_link",link);

//存储数据到数据库中,持久化数据

 preferences.store();

 SessionMessages.add(actionRequest,"success");

 }

 super.processAction(portletConfig,actionRequest, actionResponse);

 }

到这里已经完成了可配置portlet的配置部分的开发。

在view.jsp中使用配置数据

前面步骤开发的配置数据的目标是为了在view.jsp中使用,在view.jsp中使用可以在view.jsp的action中使用,也可以直接在view.jsp中直接提取,方法为:

PortletPreferencespreferences = renderRequest.getPreferences();

 String title =preferences.getValue("customjspConfig_page_title", StringPool.BLANK);

 String link =preferences.getValue("customjspConfig_page_link", StringPool.BLANK);

通过这样的方法即可取到前面的配置信息,取取的数据具体怎么展现,怎么使用,根据不同的业务场景有所不同。


1 0