portlet初学习及HelloWorld例子
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了portlet初学习及HelloWorld例子相关的知识,希望对你有一定的参考价值。
1. 在myeclipse中新建一个web project,在src中新建如下类:
- package com.yoyo.portlet;
- import java.io.IOException;
- import javax.portlet.GenericPortlet;
- import javax.portlet.PortletException;
- import javax.portlet.PortletRequestDispatcher;
- import javax.portlet.RenderRequest;
- import javax.portlet.RenderResponse;
- public class TestPortlet extends GenericPortlet
- {
- public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException
- {
- response.setContentType("text/html");
- PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher("/index.jsp");
- rd.include(request, response);
- }
- public void doEdit(RenderRequest request, RenderResponse response) throws PortletException, IOException
- {
- response.setContentType("text/html");
- String jspName = getPortletConfig().getInitParameter("jspEdit");
- PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(
- jspName);
- rd.include(request, response);
- }
- }
2. 在WebRoot/WEB-INF下面新建一个portlet.xml的文件,里面内容如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd"
- version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd
- http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd">
- <portlet>
- <description>Test the portlet</description>
- <portlet-name>hello</portlet-name>
- <display-name>test portlet</display-name>
- <portlet-class>com.yoyo.portlet.TestPortlet</portlet-class>
- <expiration-cache>-1</expiration-cache>
- <supports>
- <mime-type>text/html</mime-type>
- <portlet-mode>VIEW</portlet-mode>
- <portlet-mode>EDIT</portlet-mode>
- </supports>
- <supported-locale>en</supported-locale>
- <portlet-info>
- <title>TEst the portlet</title>
- <short-title>test</short-title>
- <keywords>test,portlet</keywords>
- </portlet-info>
- </portlet>
- </portlet-app>
3. 修改WebRoot/WEB-INF下面的web.xml文件,内容如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.4"
- xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <servlet>
- <servlet-name>test</servlet-name>
- <servlet-class>org.apache.pluto.core.PortletServlet</servlet-class>
- <init-param>
- <param-name>portlet-name</param-name>
- <param-value>hello</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <!-- 下面的<servlet-name>test</servlet-name>中的test和上面的<servlet></servlet>中<servlet-name></servlet-name>的名字对应 -->
- <!-- 下面的<url-pattern>/PlutoInvoker/hello</url-pattern>中的hello则与portlet.xml中<portlet></portlet>中<portlet-name></portlet-name>的名字对应-->
- <servlet-mapping>
- <servlet-name>test</servlet-name>
- <url-pattern>/PlutoInvoker/hello</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
4. 将tomcat/shared/lib下面的jar包引到项目中来(我的里面已经放好了,是做其他项目用的到的,在这个中可能有些jar包不需要),这些jar包如下:castor-1.1.1.jar,commons-codec-1.3.jar,commons-httpclient-3.0.1.jar,commons-logging.jar,log4j-1.2.11.jar,pluto-container-1.1.6.jar,pluto-descriptor-api-1.1.6.jar,pluto-descriptor-impl-1.1.6.jar,pluto-taglib-1.1.6.jar,portlet-api-1.0.jar,simpleAuthClient.jar,xpp3_min-1.1.4c.jar,xstream-1.3.jar。
经过以上步骤,一个简单的portlet就建好了。下载pluto-current-bundle包,解压后启动bin目录下的startup.bat,打开http://localhost:8080/pluto/portal,登陆进去后,选择上面的Pluto Admin,新建一个页面,并在下面选择自己的portlet项目,这样就可以查看效果了。
以上是关于portlet初学习及HelloWorld例子的主要内容,如果未能解决你的问题,请参考以下文章