一个平常的portlet先,jsr286类型。
/Train01/WebContent/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"
version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"
id="com.ibm.train01.Train01Portlet.17cae2de04">
<portlet>
<portlet-name>Train01</portlet-name>
<display-name xml:lang="zh">Train01</display-name>
<display-name>Train01</display-name>
<portlet-class>com.ibm.train01.Train01Portlet</portlet-class>
<init-param>
<name>wps.markup</name>
<value>html</value>
</init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>view</portlet-mode>
</supports>
<supported-locale>zh</supported-locale>
<resource-bundle>com.ibm.train01.nl.Train01PortletResource</resource-bundle>
<portlet-info>
<title>Train01</title>
<short-title>Train01</short-title>
<keywords>Train01</keywords>
</portlet-info>
<portlet-preferences>
<preference>
<name>hehe</name>
<value>1</value>
</preference>
</portlet-preferences>
</portlet>
<default-namespace>http://Train01/</default-namespace>
</portlet-app>
在Portlet java类中加入serveResource(ResourceRequest request, ResourceResponse response)方法
/Train01/src/com/ibm/train01/Train01Portlet.java
package com.ibm.train01;
import java.io.*;
import javax.portlet.*;
/**
* A sample portlet
*/
public class Train01Portlet extends javax.portlet.GenericPortlet {
/**
* @see javax.portlet.Portlet#init()
*/
public void init() throws PortletException{
super.init();
}
/**
* Serve up the <code>view</code> mode.
*
* @see javax.portlet.GenericPortlet#doView(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
*/
public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
// Set the MIME type for the render response
response.setContentType(request.getResponseContentType());
PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher("/WEB-INF/pages/view.jsp");
rd.include(request,response);
// or write to the response directly
//response.getWriter().println("Train01#doView()");
}
/**
* Serve up the <code>edit</code> mode.
*
* @see javax.portlet.GenericPortlet#doEdit(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
*/
public void doEdit(RenderRequest request, RenderResponse response) throws PortletException, IOException {
// TODO: auto-generated method stub
}
/**
* Serve up the <code>help</code> mode.
*
* @see javax.portlet.GenericPortlet#doHelp(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
*/
protected void doHelp(RenderRequest request, RenderResponse response) throws PortletException, IOException {
// TODO: auto-generated method stub
}
/**
* Process an action request.
*
* @see javax.portlet.Portlet#processAction(javax.portlet.ActionRequest, javax.portlet.ActionResponse)
*/
public void processAction(ActionRequest request, ActionResponse response) throws PortletException, java.io.IOException {
// TODO: auto-generated method stub
}
@Override
public void serveResource(ResourceRequest request, ResourceResponse response)
throws PortletException, IOException {
//用printWriter直接输出
//response.setCharacterEncoding("UTF-8");
//PrintWriter writer = response.getWriter();
//writer.print("今天是:"+new Date());
String s = request.getPreferences().getValue("hehe", null);
System.out.println(s+"----------------------------------");
//用dispatcher().include()转发到jsp页面
getPortletConfig().getPortletContext().getRequestDispatcher("/WEB-INF/pages/resource.jsp").include(request, response);
System.out.println("=========================");
}
}
view模式下的页面
/Train01/WebContent/WEB-INF/pages/view.jsp
<%@page language="java"
contentType="text/html; charset=UTF-8" pageEncoding="utf-8" session="false"%>
<%--portlet标签的uri是要改成http://java.sun.com/portlet_2_0的,
这样子的是http://java.sun.com/portle不行的,不能使用下面的<portlet:resourceURL />这个标签的
--%>
<%@taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<%@taglib
uri="http://www.ibm.com/xmlns/prod/websphere/portal/v6.1/portlet-client-model"
prefix="portlet-client-model"%><portlet-client-model:init>
<portlet-client-model:require module="ibm.portal.xml.*" />
<portlet-client-model:require module="ibm.portal.portlet.*" />
</portlet-client-model:init>
<portlet:defineObjects />
<%
String s = renderRequest.getPreferences().getValue("hehe",null);
%>
<%=s %><br />
<a href="<portlet:resourceURL />">呵呵</a>
被resource的页面,可用来处理ajax请求,或者xxx。
/Train01/WebContent/WEB-INF/pages/resource.jsp
<%@page language="java"
contentType="text/html; charset=UTF-8" pageEncoding="utf-8" session="false"%>
<%@taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<%--听说这个
<portlet-client-model:init>是不被支持的。所以下面的taglib没什么用。
对比下view.jsp能看出来。--%>
<%@taglib
uri="http://www.ibm.com/xmlns/prod/websphere/portal/v6.1/portlet-client-model"
prefix="portlet-client-model"%>
<portlet:defineObjects />
<% //可以通过resourceRequest取到Preferences里面的值的。
String s = resourceRequest.getPreferences().getValue("hehe",null);
//通过request取到请求过来的参数什么的。
String queryMail = request.getParameter("queryMail");
String queryName = request.getParameter("queryName");
%>
<%=s %><br />
dlksfjsdlkfjsdlkfjlkj
被resource的也可以是servlet,其实和jsp是一个道理,个人觉得用jsp在修改返回数据的时候会更加方便些。
先定义个servlet
/Train01/WebContent/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0" 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_3_0.xsd">
<display-name>Train01</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>MyServlet.do</servlet-name>
<servlet-class>com.ibm.train01.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet.do</servlet-name>
<url-pattern>/MyServlet.do</url-pattern>
</servlet-mapping>
</web-app>
/Train01/src/com/ibm/train01/MyServlet.java(片段)
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
response.getWriter().write(new Date()+"");
}
然后把portlet类中的serveResource(ResourceRequest request, ResourceResponse response)
public void serveResource(ResourceRequest request, ResourceResponse response)
throws PortletException, IOException {
String s = request.getPreferences().getValue("hehe", null);
System.out.println(s+"----------------------------------");
getPortletConfig().getPortletContext().getRequestDispatcher("/MyServlet.do").include(request, response);
System.out.println("=========================");
}
方法里Dispatcher的路径改了就okkkk了。然后有个问题是呢。在自己的servlet里面怎么取到Preferences呢???
在serveResource里面request.setAttribute("h","nihao");
之后在 servlet里面 用HttpServletRequest类型的request.getAttribute("h")
是能拿到值的。为什么呢?