liferay7中自定义tag library

来源:互联网 发布:js教程pdf 编辑:程序博客网 时间:2024/06/17 19:33

参考git:

https://github.com/liferay/liferay-portal/tree/master/modules/apps/foundation/map/map-taglib/

http://www.codeyouneed.com/how-to-create-a-taglib-with-jsps-for-liferay/


1 生成标签库

按照portlet项目建立的方式,先建立一个portlet,之后修改bnd.bnd为:

Bundle-Name: tagTest
Bundle-SymbolicName: com.liferay.map.taglib
Bundle-Version: 1.1.3
Export-Package: com.liferay.map.taglib.servlet.taglib
Provide-Capability:\
    osgi.extender;\
        osgi.extender="jsp.taglib";\
        uri="http://liferay.com/tld/map";\
        version:Version="${Bundle-Version}"
Web-ContextPath: /map-taglib

build.gradle为:

dependencies {
    compile group: "com.liferay", name: "com.liferay.frontend.taglib", version: "2.0.0"
    compile group: "javax.servlet", name:"javax.servlet-api", version:"3.0.1"
    compile group: "com.liferay", name:"com.liferay.map.api", version:"2.0.0"
    compile group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "2.0.0"
    compile group: "com.liferay.portal", name: "com.liferay.util.taglib", version: "2.0.0"
    compile group: "javax.servlet.jsp", name: "javax.servlet.jsp-api", version: "2.3.1"
    compile group: "javax.portlet", name: "portlet-api", version: "2.0"
    compile group: "org.osgi", name: "org.osgi.service.component.annotations", version: "1.3.0"
}

标签库为:liferay-map.tld

<?xml version="1.0"?>

<taglib
    version="2.1"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
>
    <description>Provides the Liferay Map component tags, prefixed with <![CDATA[<code>liferay-map:</code>]]>. These tags are used for displaying and configuring Maps (e.g., Google Maps, OpenStreetMap, etc.).</description>
    <tlib-version>1.0</tlib-version>
    <short-name>liferay-map</short-name>
    <uri>http://liferay.com/tld/map</uri>
    <tag>
        <name>map-display</name>
        <tag-class>com.liferay.map.taglib.servlet.taglib.MapDisplayTag</tag-class>
        <body-content>JSP</body-content>

        <attribute>
            <name>name</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
 
    </tag>

</taglib>

此表示:如果外界引用,则需要

<%@ taglib uri="http://liferay.com/tld/map" prefix="liferay-map" %>

 <liferay-map:map-display   />     //此为标签的引用例子,属性未添加

形式如图:


对于gradle项目,如果需要加载对应的gradle,则只需build.gradle右键->Gradle->Refresh Gradle Project,之后刷新即可。

如果运行,则在eclipse的右面,build下,先clean,之后deploy即可


2 portlet项目建立

2.1 新建一个Liferay Workspace项目

2.2 选择新建Liferay Module Project项目,选择Build type为gradle-module,Project-Template Name为mvc-portlet,之后会自动生成相关的项目。

此时生成的bnd.bnd为:

Bundle-Name: tag4
Bundle-SymbolicName: tag4
Bundle-Version: 1.1.3

build.gradle为:

dependencies {
    compile group: "com.liferay", name: "com.liferay.frontend.taglib", version: "2.0.0"
    compile group: "javax.servlet", name:"javax.servlet-api", version:"3.0.1"
    compile group: "com.liferay", name:"com.liferay.map.api", version:"2.0.0"
    compile group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "2.0.0"
    compile group: "com.liferay.portal", name: "com.liferay.util.taglib", version: "2.0.0"
    compile group: "javax.servlet.jsp", name: "javax.servlet.jsp-api", version: "2.3.1"
    compile group: "javax.portlet", name: "portlet-api", version: "2.0"
    compile group: "org.osgi", name: "org.osgi.service.component.annotations", version: "1.3.0"
}


2.3 引用标签,

如在view.jsp中如下:

<%@ include file="/init.jsp" %>
<%@ taglib uri="http://liferay.com/tld/map" prefix="liferay-map" %>
<p>
    <b><liferay-ui:message key="tag4.caption"/></b>
   
    
    http://www.codeyouneed.com/how-to-create-a-taglib-with-jsps-for-liferay/
    
    <liferay-map:map-display  />
    
</p>


3 ServletContextUtil与bnd.bnd设置对照

ServletContextUtil中的osgi.web.symbolicname=com.liferay.frontendExtend.taglib为bnd.bnd的值为:Bundle-SymbolicName: com.liferay.frontendExtend.taglib

@Reference(
        target = "(osgi.web.symbolicname=com.liferay.frontendExtend.taglib)",
        unbind = "-"
    )

4 init.jsp设置

其中有一个

<%@ taglib uri="http://liferay.com/tld/frontend" prefix="liferay-frontend" %>

需修改为:

<%@ taglib uri="http://liferay.com/tld/frontendExtend" prefix="liferay-frontendExtend" %>


主要是对liferay7核心标签的修改,否则如果是引用的话,则此标签可以不用修改



项目参考,百度云(http://pan.baidu.com/s/1bp4FYwf),需要请留言