Chinaunix首页 | 论坛 | 博客
  • 博客访问: 688591
  • 博文数量: 118
  • 博客积分: 2933
  • 博客等级: 少校
  • 技术积分: 1779
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-27 10:16
文章存档

2013年(35)

2012年(27)

2011年(23)

2010年(33)

分类: 系统运维

2011-12-14 10:40:53

       第一个Liferay例子——Hello World
 
 
 
打开MyEclipse,新建一个Java Project,如:MyLiferay
 
2 在MyLiferay下新建webRoot子Folder,在webRoot下新建WEB-INF子Folder
 
右键项目名选择“Build Path—>Configure Build Path”,将Source选项下的Default output folder设置为MyLiferay/webRoot/WEB-INF/classes
 
4   在Libraries选项下点击Add External JARs...,将liferay-portal-tomcat-6.0-5.2.3包下的tomcat6.0.18/lib/ext目录下的所有jar包添加到Libraries下
 
如下图所示:
 
 
 
5  在webRoot文件夹下新建view.jsp页面,在页面上显示“Hello World!”
 
 
6 新建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);
        }
    }
}
7 在WEB-INF目录下新建portlet.xml文件,内容如下:
xml version="1.0" encoding="UTF-8"?>
<portlet-app xmlns="" xmlns:xsi="" xsi:schemaLocation=" /opt/SUNWportal/dtd/portlet.xsd" version="2.0">
    <portlet>
        <portlet-name>myliferayportlet-name>
        <display-name>Sample JSPdisplay-name>
        <portlet-class>com.company.portal.MyPortletportlet-class>
        <init-param>
            <name>view-jspname>
            <value>/view.jspvalue>
        init-param>
        <expiration-cache>0expiration-cache>
        <supports>
            <mime-type>text/htmlmime-type>
        supports>
        <portlet-info>
            <title>HelloWorldtitle>
            <short-title>HelloWorldshort-title>
            <keywords>HelloWorldkeywords>
        portlet-info>
        <security-role-ref>
            <role-name>administratorrole-name>
        security-role-ref>
        <security-role-ref>
            <role-name>guestrole-name>
        security-role-ref>
        <security-role-ref>
            <role-name>power-userrole-name>
        security-role-ref>
        <security-role-ref>
            <role-name>userrole-name>
        security-role-ref>
    portlet>
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" "">
<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一起打包。如下图:
 

Microsoft Windows XP [版本 5.1.2600]

 

C:\Documents and Settings\Administrator>d:

 

D:\>cd program

 

D:\program>cd Liferay

 

D:\program\Liferay>cd MyLiferay

 

D:\program\Liferay\MyLiferay>cd webRoot

 

D:\program\Liferay\MyLiferay\webRoot>jar cvf MyLiferay.war WEB-INF view.jsp

标明清单(manifest)

增加:WEB-INF/(读入= 0) (写出= 0)(存储了 0%)

增加:WEB-INF/classes/(读入= 0) (写出= 0)(存储了 0%)

增加:WEB-INF/classes/com/(读入= 0) (写出= 0)(存储了 0%)

增加:WEB-INF/classes/com/company/(读入= 0) (写出= 0)(存储了 0%)

增加:WEB-INF/classes/com/company/portal/(读入= 0) (写出= 0)(存储了 0%)

增加:WEB-INF/classes/com/company/portal/MyPortlet.class(读入= 3042) (写出= 1330

)(压缩了 56%)

增加:WEB-INF/liferay-display.xml(读入= 284) (写出= 192)(压缩了 32%)

增加:WEB-INF/liferay-portlet.xml(读入= 863) (写出= 287)(压缩了 66%)

增加:WEB-INF/portlet.xml(读入= 1391) (写出= 448)(压缩了 67%)

增加:view.jsp(读入= 832) (写出= 448)(压缩了 46%)

 

D:\program\Liferay\MyLiferay\webRoot>

 

 

 

10 上步服务器启动后会弹出liferay的官网登陆页面,用系统自带的管理员登陆,进入账户

 

 

11 打开账户页面右上角的下拉列表,选择Add Application,如下图:

 然后点击Install More Applications,如下图:
 
 
点击Back to Guest,重新选择Add Application,可以看到test节点下出现了myfirstportal节点,拖动该节点到页面上即可显示view.jsp的
 
12 到此,我们的第一个Liferay例子——Hello World已经完成。感兴趣的朋友可以尝试改变view.jsp的内容后会有什么效果。
 
 
 
 
阅读(5239) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~