注意:在MyEclipse中创建portlet项目 首先:创建 File->New->Web Project (项目名:PlutoInvoker 借鉴原由的包servlet包,不能随便改动项目名)
其次:在项目上右击->MyEclipse->Add Portlet ->Add Portlet capabilites ->Finish
1. 此时在项目的src中新建如下类:
package org.apache.pluto; import java.io.IOException; import javax.portlet.GenericPortlet; import javax.portlet.PortletException; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; public class FirstPortlet extends GenericPortlet { /** * Helper method to serve up the mandatory view mode. */ protected void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException { response.setContentType("text/html"); response.getWriter().println("hello,this is my first portlet"); } }
2.在WEB-INF的portlet.xml添加内容如下:记住<portlet-name>FirstPortlet</portlet-name>
<?xml version='1.0' encoding='UTF-8' ?>
<portlet-app xmlns='http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd'
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' version='1.0'>
<portlet>
<portlet-name>FirstPortlet</portlet-name>
<display-name>FirstPortlet Portlet</display-name>
<portlet-class>org.apache.pluto.FirstPortlet</portlet-class>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>VIEW</portlet-mode>
</supports>
<portlet-info>
<title>FirstPortlet Pluto</title>
</portlet-info>
</portlet></portlet-app>
3.在WEB-INF的web.xml添加内容如下 :注意:①version=”2.5“必须与自动生成的一致。否则:会抛出NotFound...错误。②下面代码中的<servlet-name>firstportlet</servlet-name>与portlet.xml中的<portlet-name>FirstPortlet</portlet-name>一样。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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-app_2_5.xsd">
<servlet>
<servlet-name>firstportlet</servlet-name>
<servlet-class>org.apache.pluto.container.driver.PortletServlet</servlet-class>
<init-param>
<param-name>portlet-name</param-name>
<param-value>FirstPortlet</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>firstportlet</servlet-name>
<url-pattern>/PlutoInvoker/FirstPortlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
打开http://localhost:8080/pluto/portal,登陆进去后,选择上面的Pluto Admin,添加Add Page按钮左边框输入 导航子栏目名称后点击Add Page,新建一导航标签。并在下面的Portlet Applications下 选择刚刚添加的portlet项目,查看效果了。
由于MyEclipse10在导出war包是会出现错误在网上借鉴了一个方法: