建立web工程,结构如图:
view.jsp
<portlet:actionURL/> 直接提交到myportlet的processAction方法,然后到render方法,另外,任何一个portlet刷新,每个portlet都会走render方法
<form action="<portlet:actionURL/>" method="post">
<input type="text" name="tet"/>
</form>
MyPortlet.java
package com.shrek;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.faces.event.ActionEvent;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class MyPortlet extends GenericPortlet {
public void init() throws PortletException {
editJSP = getInitParameter("edit-jsp");
helpJSP = getInitParameter("help-jsp");
viewJSP = getInitParameter("view-jsp");
}
public void doDispatch(RenderRequest req, RenderResponse res)
throws IOException, PortletException {
String jspPage = req.getParameter("jspPage");
System.out.println("jspPage=============="+jspPage);
_log.debug("jspPage======dd==");
_log.info("jspPage======dd==");
_log.error("jspPage======dd==");
if (jspPage != null) {
include(jspPage, req, res);
}
else {
super.doDispatch(req, res);
}
}
public void doEdit(RenderRequest req, RenderResponse res)
throws IOException, PortletException {
if (req.getPreferences() == null) {
super.doEdit(req, res);
}
else {
include(editJSP, req, res);
}
}
public void doHelp(RenderRequest req, RenderResponse res)
throws IOException, PortletException {
include(helpJSP, req, res);
}
public void doView(RenderRequest req, RenderResponse res)
throws IOException, PortletException {
include(viewJSP, req, res);
}
public void processAction(ActionRequest req, ActionResponse res)
throws IOException, PortletException {
List lst = new ArrayList();
lst.add(new Integer(1));
lst.add(new Integer(2));
lst.add(new Integer(3));
lst.add(new Integer(4));
req.setAttribute("lst", lst);
System.out.println("1654+6874631.203");
String param = req.getParameter("tet");
String index = req.getParameter("index");
System.out.println(param+"=============="+index);
_log.info(param+"=============="+index);
res.setRenderParameter("param", "test");
}
public void render(RenderRequest req, RenderResponse res)
throws IOException, PortletException {
if(req.getAttribute("lst")==null){
System.out.println("null--------------------------");
}else{
System.out.println("lst--------------------------"+((List)req.getAttribute("lst")).size());
}
if(req.getParameter("param")==null){
include(viewJSP,req,res);
}else{
include("/recieve.jsp",req,res);
}
}
protected void include(String path, RenderRequest req, RenderResponse res)
throws IOException, PortletException {
PortletRequestDispatcher prd =
getPortletContext().getRequestDispatcher(path);
if (prd == null) {
_log.error(path + " is not a valid include");
}
else {
prd.include(req, res);
}
}
protected String editJSP;
protected String helpJSP;
protected String viewJSP;
private static Log _log = LogFactory.getLog(MyPortlet.class);
}
liferay-display.xml
<?xml version="1.0"?>
<!DOCTYPE display PUBLIC "-//Liferay//DTD Display 5.1.0//EN" "http://www.liferay.com/dtd/liferay-display_5_1_0.dtd">
<display>
<category name="myPortlet">
<portlet id="1" />
</category>
</display>
liferay-portlet.xml
<?xml version="1.0"?>
<!DOCTYPE liferay-portlet-app PUBLIC "-//Liferay//DTD Portlet Application 5.1.0//EN" "http://www.liferay.com/dtd/liferay-portlet-app_5_1_0.dtd">
<liferay-portlet-app>
<portlet>
<portlet-name>1</portlet-name>
<icon>/icon.png</icon>
<instanceable>true</instanceable>
<header-portlet-css>/css/test.css</header-portlet-css>
<header-portlet-javascript>/js/test.js</header-portlet-javascript>
</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>
portlet.xml
<?xml version="1.0"?>
<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">
<portlet>
<portlet-name>1</portlet-name>
<display-name>Sample JSP</display-name>
<portlet-class>com.shrek.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>Sample JSP</title>
<short-title>Sample JSP</short-title>
<keywords>Sample JSP</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>