关闭

Liferay学习笔记(二)——第一个Liferay例子——Hello World

标签: includeportletmyeclipseapplicationencodingstring
3561人阅读 评论(1) 收藏 举报
分类:

第一个Liferay例子——Hello World

1、打开MyEclipse,新建一个Java Project,如:MyLiferay
2、在MyLiferay下新建webRoot子Folder,在webRoot下新建WEB-INF子Folder
3、右键项目名选择“Build Path—>Configure Build Path”,将Source选项下的Default output folder设置为MyLiferay/webRoot/WEB-INF/classes
    如下图:
        
        在Libraries选项下点击Add External JARs...,将liferay-portal-tomcat-6.0-5.2.3包下的tomcat6.0.18/lib/ext目录下的所有jar包添加到Libraries下
        如下图:
        
4、在webRoot文件夹下新建view.jsp页面,在页面上显示“Hello World!”
5、新建MyPortlet.java类,Package设置为:com.company.portal,继承javax.portlet.GenericPortlet抽象类;在MyPortlet.java中实现GenericPortlet的doDispatch、doEdit、
    doHelp、doView、init和ProcessAction这五个方法,具体内容如下: 
package com.jyyr.portal;
import java.io.IOException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
/**
*
* @CopyRight 2011
* @Create by admin on 2011-1-16 下午02:42:55
* @Function
*
* @Version 1.0
* @Update
*/
public class MyPortlet extends javax.portlet.GenericPortlet{
    protected String editJSP;
    protected String helpJSP;
    protected String viewJSP;
 
    private static Log _log = LogFactoryUtil.getLog(MyPortlet.class);
 
    @Override
    protected void doDispatch(RenderRequest request, RenderResponse response)
            throws PortletException, IOException {
        String jspPage = request.getParameter("jspPage");
        if(jspPage != null){
            include(jspPage,request,response);
        }else{
            super.doDispatch(request, response);
        }
    }
 
    @Override
    protected void doEdit(RenderRequest request, RenderResponse response)
            throws PortletException, IOException {
        if(request.getPreferences() == null){
            super.doEdit(request, response);
        }else{
            include(editJSP,request,response);
        }
    }
 
    @Override
    protected void doHelp(RenderRequest request, RenderResponse response)
            throws PortletException, IOException {
        include(helpJSP,request,response);
    }
 
    @Override
    protected void doView(RenderRequest request, RenderResponse response)
            throws PortletException, IOException {
        include(viewJSP,request,response);
        }
 
    @Override
    public void init() throws PortletException {
        this.editJSP = getInitParameter("edit-jsp");
        this.helpJSP = getInitParameter("help-jsp");
        this.viewJSP = getInitParameter("view-jsp");
    }
 
    @Override
    public void processAction(ActionRequest request, ActionResponse response)
            throws PortletException, IOException {
    }
 
    protected void include(String path,RenderRequest request,RenderResponse response)
            throws PortletException, IOException {
        PortletRequestDispatcher prd = getPortletContext().getRequestDispatcher(path);
        if(prd == null){
            _log.error(path+" is not a valid include");
        }else{
            prd.include(request, response);
        }
    }
}
6、在WEB-INF目录下新建portlet.xml文件,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd /opt/SUNWportal/dtd/portlet.xsd" version="2.0">
    <portlet>
        <portlet-name>myliferay</portlet-name>
        <display-name>Sample JSP</display-name>
        <portlet-class>com.company.portal.MyPortlet</portlet-class>
        <init-param>
            <name>view-jsp</name>
            <value>/view.jsp</value>
        </init-param>
        <expiration-cache>0</expiration-cache>
        <supports>
            <mime-type>text/html</mime-type>
        </supports>
        <portlet-info>
            <title>HelloWorld</title>
            <short-title>HelloWorld</short-title>
            <keywords>HelloWorld</keywords>
        </portlet-info>
        <security-role-ref>
            <role-name>administrator</role-name>
        </security-role-ref>
        <security-role-ref>
            <role-name>guest</role-name>
        </security-role-ref>
        <security-role-ref>
            <role-name>power-user</role-name>
        </security-role-ref>
        <security-role-ref>
            <role-name>user</role-name>
        </security-role-ref>
    </portlet>
</portlet-app>
7、在WEB-INF目录下新建liferay-portlet.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE liferay-portlet-app PUBLIC "-//Liferay//DTD Portlet Application 5.2.0//EN" "http://www.liferay.com/dtd/liferay-portlet-app_5_2_0.dtd">
    <liferay-portlet-app>
    <portlet>
        <portlet-name>myliferay</portlet-name>
        <instanceable>true</instanceable>
    </portlet>
    <role-mapper>
        <role-name>administrator</role-name>
        <role-link>Administrator</role-link>
    </role-mapper>
    <role-mapper>
        <role-name>guest</role-name>
        <role-link>Guest</role-link>
    </role-mapper>
    <role-mapper>
        <role-name>power-user</role-name>
        <role-link>Power User</role-link>
    </role-mapper>
    <role-mapper>
        <role-name>user</role-name>
        <role-link>User</role-link>
    </role-mapper>
</liferay-portlet-app>
8、在WEB-INF目录下新建liferay-display.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE display PUBLIC "-//Liferay//DTD Display 5.2.0//EN" "http://www.liferay.com/dtd/liferay-display_5_2_0.dtd">
<display>
    <category name="category.test">
        <portlet id="myliferay"></portlet>
    </category>
</display>
9、启动MyEclipse中的Tomcat,同时通过dos进入MyLiferay项目的webRoot目录下,输入:jar cvf MyLiferay.war WEB-INF view.jsp,将WEB-INF和view.jsp一起打包。如下图:
    
10、上步服务器启动后会弹出liferay的官网登陆页面,输入用户名:test@liferay.com;密码:test;进入账户
11、打开账户页面右上角的下拉列表,选择Add Application,如下图:
    
    然后点击Install More Applications,如下图:
    
    在弹出的页面中选择Upload File,添加之前打包生成的MyLiferay.war,然后点击Install,如下图:
    
    显示下面这个界面说明上传成功
    
 
    点击Back to Guest,重新选择Add Application,可以看到Undefined节点下出现了myfirstportal节点,拖动该节点到页面上即可显示view.jsp的
    页面内容。注:这里看到的节点名myfirstportal是在portlet.xml文件的<portlet-info></portlet-info>子标签中定义的;另外,重新登录后可以看
    到与Undefined同级的Test节点,其子节点中也有myfirstportal
    
    
12、到此,我们的第一个Liferay例子——Hello World已经完成。感兴趣的朋友可以尝试改变view.jsp的内容后会有什么效果。
下图是我写的一个简单的用户登录界面:
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:29042次
    • 积分:395
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:0篇
    • 译文:0篇
    • 评论:15条
    文章分类
    最新评论