Liferay7 BPM门户开发之25: Liferay7应用程序配置(APPLICATION CONFIGURATION)


首先有几个概念需要明确。
1、第一个概念是这里的应用程序配置不是写XML之类的配置文件,是类似字典的类型化配置
这意味着应用程序配置不只是一个字符串键值对的列表。值还可以有类型,如整数列表,字符串列表,一个网址,甚至可以使用自己的自定义类型。

2、第二个概念是模块性。在Liferay 7 中,应用程序是模块化的,组件只是一个有@组件注释的类,通常是一组属性提供元数据。

3、第三个概念是在不同的范围内具有相同的应用程序的能力有不同的配置。如果您的应用程序需要在不同的范围支持不同的配置,应用程序作用范围可以有:

  • System: 所有的应用程序范围;
  • Virtual Instance: 虚拟实例级别;
  • Site: 站点级别,即每个站点不同的配置;
  • Portlet Instance: Portlet实例级别,即每个Portlet实例都有不同配置;


实例

编写应用程序配置接口

package com.liferay.docs.exampleconfig;

import aQute.bnd.annotation.metatype.Meta;

@Meta.OCD(id = "com.liferay.docs.exampleconfig.ExampleConfiguration")
public interface ExampleConfiguration {

    @Meta.AD(
        deflt = "blue",
        required = false
    )
    public String favoriteColor();

    @Meta.AD(
       deflt = "red|green|blue",
       required = false
    )
    public String[] validColors();

    @Meta.AD(required = false)
    public int favoriteNumber();

}
Meta.AD 和 Meta.OCD是OSGi Metatype 的格式。更多信息见:
http://bnd.bndtools.org/chapters/210-metatype.html


MVCPortlet的实现
先添加@Component注解, configurationPid即应用程序配置接口
@Activate和@Modified是一个标准格式写法

package com.liferay.docs.exampleconfig;

import java.io.IOException;
import java.util.Map;

import javax.portlet.Portlet;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Modified;

import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;

import aQute.bnd.annotation.metatype.Configurable;

@Component(
    configurationPid = "com.liferay.docs.exampleconfig.ExampleConfiguration",
    immediate = true,
    property = {
        "com.liferay.portlet.display-category=category.sample",
        "com.liferay.portlet.instanceable=true",
        "javax.portlet.security-role-ref=power-user,user",
        "javax.portlet.init-param.template-path=/",
        "javax.portlet.init-param.view-template=/view.jsp",
        "javax.portlet.resource-bundle=content.Language"
    },
    service = Portlet.class
)
public class ExampleConfigPortlet extends MVCPortlet {

    @Override
    public void doView(RenderRequest renderRequest,
        RenderResponse renderResponse) throws IOException, PortletException {
        //设置renderRequest的属性,把className写到key
        renderRequest.setAttribute(
            ExampleConfiguration.class.getName(), _configuration);
        
        super.doView(renderRequest, renderResponse);
    }

    
    public String getFavoriteColor(Map colors) {
        return (String) colors.get(_configuration.favoriteColor());
    }

    @Activate
    @Modified
    protected void activate(Map<String, Object> properties) {
        _configuration = Configurable.createConfigurable(
        ExampleConfiguration.class, properties);
    }    

    private volatile ExampleConfiguration _configuration;

}

 

jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>

<%@ page import="com.liferay.docs.exampleconfig.ExampleConfiguration" %>

<%@ page import="com.liferay.portal.kernel.util.GetterUtil" %>

<portlet:defineObjects />

<liferay-theme:defineObjects />

<p>
<b>Hello from the Example Configuration portlet!</b>
</p>

<%
ExampleConfiguration configuration = (ExampleConfiguration) GetterUtil.getObject(
renderRequest.getAttribute(ExampleConfiguration.class.getName()));

String favoriteColor = configuration.favoriteColor();
%>

<p>Favorite color: <span style="color: <%= favoriteColor %>;"><%= favoriteColor %></span></p>

 

界面:


我暂时还想不出这种写法的好处是啥?

为啥不用枚举,静态类,或者XML的属性文件,就是为了有作用范围? 有知道答案的客官请点醒我。
这种注解+元数据+接口的写法实在是太奇特了。

在liferay自身的设置,也是应用这种配置方法,

比如UI设置,如图:


还有一个更复杂的例子:
https://dev.liferay.com/develop/tutorials/-/knowledge_base/7-0/implementing-configuration-actions

posted @ 2016-11-09 16:07  昕友软件开发  阅读(740)  评论(0编辑  收藏  举报
欢迎访问我的开源项目:xyIM企业即时通讯