Portlet插件开发说明文档

Portlet插件开发说明文档

概述

Liferay几种开发:

1、 Portlets

一个插件中可以有多个portletportlet开发可以用StrutsSpring MVCJava Server Faces (JSF)等框架,也可以使用Liferay特定的框架:MVCPortlet或者AlloyPortlet等。

可以将多种插件类型放到一个war包中,如将主题和布局合并在一起(待验证)。

2、 Themes(自定义外观和风格)

使用CSSVelocity模板来改变门户的外观和风格。

3、 Layout Templates

布局改变的是portlet在页面中的布局,而不是portlet的外观和风格,布局使用Velocity或者Freemarker模板。

4、 Hooks

Hook插件可以用来修改portal properties或者在启动、关闭、登录、注销、session创建和session注销时执行自定义action,除此之外,还有其它的一些功能,不在这里说明。

5、 Ext Plugins

Ext插件在修改Liferay核心时提供了最大的灵活性。

Portlet开发过程

创建工程图解

通过Liferay开发工具来创建








生成如下结构的工程:


配置文件说明

配置文件存放在docroot/WEB-INF文件夹中。

portlet.xmlJSR-286标准的portlet配置文件。

liferay-display.xml:Liferay特有的配置文件,描述了该portlet在添加应用时将展示在哪个类型下面。


liferay-portlet.xml:Liferay特有的配置文件,该文件描述了一些针对安装在Liferay门户服务器上的portletJSR-286标准)增加的配置(Liferay特有的)。例如,你可以设置某个portlet是否可以实例化多次(在同一个页面中相同的portlet是否可以添加多次)。详细信息可以查看位于Liferay源代码definitions文件夹中对应的DTD文件。

liferay-plugin-package.properties:Liferay特有的配置文件,该文件描述了Liferay热部署插件的一些信息。可以在这里配置依赖的jar包,如果一个portlet插件需要依赖一些liferay特有的jar包,可以在这里指定,热部署的时候,会复制这些指定的jar包到相应的目录下。这样就不需要自己引用这些jar包了,也就可以减少war包的体积了。

下面是对这些配置文件的一些说明:

docroot/WEB-INF/portlet.xml:



下面是对每个元素所代表的含义的一些总结:

portlet-name

该元素包含了portlet的标准名称,每个portlet名称在portlet应用程序(即portlet插件)中是唯一的。在liferay门户中,这也被称为portlet ID

display-name

The display-name type contains a short name that is intended to be displayed by tools. It is used by display-name elements. The display name need not be unique.

portlet-class

包含完整的类名,处理对portlet的调用。

init-param

portlet的初始化参数,包含成对的<name/><value/>子元素。

expiration-cache

过期时间定义portlet输出缓存在几秒钟后失效。-1表示输出不缓存。

supports

supports包含支持的MIME类型。表示为portlet支持的portle模式指定一个特定的内容类型。所有的portlet都必须支持view模式。

portlet-info

定义portlet的信息。

security-role-ref

该元素包含了对web应用程序代码中引用的安全角色的声明。特别是在liferay中,role-name表明了哪个角色可以访问该portlet

docroot/WEB-INF/liferay-portlet.xml:



除了标准的portlet.xml配置之外,还有可选的liferay独有的针对安装在Lifeary门户服务器上的java标准的portlet增加的一些配置。插件SDK会将描述信息设置为如下:

其中:

portlet-name:

必须与portlet.xml文件中的portle-name一致。

icon

portlet图标路径

instanceable

表示在同一页面上可以出现多个该portlet的实例。

header-portlet-css

在页面的<head>中包含的css文件。

footer-portlet-javascript

在页面最后</body>之前的js文件。

更多的元素可以查看位于Liferay源代码definitions文件夹中对应的DTD文件。

新建service.xml




点“Finish”后,WEB-INF目录下会出现新建的service.xml文件。



执行build-service,生成相应的service接口类、service实现类、hibernate映射、spring配置等文件。执行成功后,refresh下工程。


AJAX请求

AJAX前台请求url

<portlet:resourceURL var="url">

<portlet:param name="param1" value="value1"/>

<portlet:param name="param2" value="value2"/>

</portlet:resourceURL>

请求示例:

$.ajax({

type: 'POST',

   url: '<%=url %>',

   data: {"number":number},

   dataType: 'text',

   success: function(data){

   $("#<portlet:namespace/>datas").html(data);

   }

});

AJAX后台代码:

public void serveResource(ResourceRequest resourceRequest,

ResourceResponse resourceResponse) throws IOException,

PortletException {

int number = ParamUtil.getInteger(resourceRequest, "number");

HttpServletResponse response = PortalUtil.getHttpServletResponse(resourceResponse);

response.setContentType(ContentTypes.TEXT_PLAIN_UTF8);

response.setHeader(HttpHeaders.CACHE_CONTROL"no-cache");

PrintWriter pw = response.getWriter();

pw.write(""+(number+1)+"次请求。");

pw.close();

}

表单提交actionURL

<portlet:actionURL var="addURL"><portlet:param name="<%= actionRequest.ACTION_NAME %>" value="add" /></portlet:actionURL>

或者

<portlet:actionURL var="addURL" name="add"></portlet:actionURL>

<form action="<%=addURL %>" method="post" name="<portlet:namespace />fm2">

<input name="<%= Constants.CMD %>" type="hidden" value="edit" />

<input name="userName" type="text" value="test2" />

<input name="password" type="text" value="123@abc" />

<input type="submit" value="submit"/>

</form>

其中<%= ActionRequest.ACTION_NAME %>的值对应后台的方法名:

public void add(ActionRequest actionRequest,

ActionResponse actionResponse) throws Exception {

String cmd = ParamUtil.getString(actionRequest, Constants.CMD);

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

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

_log.info("cmd:"+cmd);

_log.info("userName:"+userName);

_log.info("password:"+password);

}

注意:不能使用renderURL

1renderRequest没有ACTION_NAME字段;

2renderURL标签没有name属性。

页面跳转renderURL

跳转url

<portlet:renderURL>

<portlet:param name="jspPage" value="/html/commodities/edit.jsp" />

</portlet:renderURL>

参数jspPage表示将要跳转到的页面。

示例如下:

<a href="<portlet:renderURL><portlet:param name="jspPage" value="/html/commodities/edit.jsp" /></portlet:renderURL>">edit</a>

name必须为jspPage,否则跳不过去

插件部署

插件部署有很多种方法,其中一种方法:

portlet插件部署到tomcat后,通过命令 jar –cvf commodities.war  *.* 打成war包(如果是WebLogic部署直接压缩成rar包即可)。

如果是在WebSphere上部署,需要在web.xml中加入下列一段话:

<context-param>

<param-name>com.ibm.websphere.portletcontainer.PortletDeploymentEnabled</param-name>

<param-value>false</param-value>

</context-param>

因为在这里我们需要使用Liferay自己的Portlet容器而不是WebSphere Application Server自带的,如果没有任何配置的情况下,WebSphere Application Server总是使用自己的Portlet容器。

其余部分与部署其他应用程序一样。

注意点

Ø 插件模式开发优点

从目前验证结果来看,插件模式开发更适用于独立的web工程,优点非常明显,只要遵循JSR,就可以任何Web方式开发,如sturts2spring mvc等,对liferay基本上没有依赖,耦合很低,可以非常方便部署、开发、迁移。

注:插件portlet可通过liferay的接口类(portal-service.jarext-portal-ext-service.jar)来调用相关代码。

Ø 插件模式开发缺点

在优点明显的同时,缺点也很明显。每个portlet都是独立的war包,代码之间的共享访问很难,同时不能直接调用liferay的实现类的jar包(portal-impl.jarext-portal-ext-impl.jar)中的代码


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值