使用的主要工具有:liferay-tomcat-
下載得到這兩個(gè)工具的zip文件夾,分別解壓到兩個(gè)目錄里,
在環(huán)境變量里把TOMCAT_HOME, AXIS_HOME分別指向這兩個(gè)文件夾根目錄。
具體配置方法分別參考
實(shí)現功能:在portlet里用SOAP訪(fǎng)問(wèn)Web Service,把得到的結果顯示在Portal界面
一、 配置Axis Server端
1. 簡(jiǎn)單的服務(wù)程序,類(lèi)似hello world,如下
package com.kevinGQ.service
HelloService.java
public class HelloService
{
public String sayHello(String name)
{
System.out.println("HelloService");
return "Axis say hello to "+name;
}
};
先把%AXIS_HOME%/webapps/axis文件夾放到%TOMCAT_HOME%/webapps/下,把編譯好的HelloService.class放到axis/WEB-INF/classes里。
在server-config.wsdd里添加如下XML片斷
<service name="HelloService" provider="java:RPC">
<parameter name="allowedMethods" value="sayHello"/>
<parameter name="className" value="com.kevinGQ.service.HelloService"/>
</service>
二、 寫(xiě)portlet的三個(gè)JSP文件(參照liferay里的weather portlet)
1. init.jsp
<%@ include file="/html/common/init.jsp" %>
<portlet:defineObjects/>
<%
PortletPreferences prefs = renderRequest.getPreferences();
String[] names = prefs.getValues("names", new String[0]);
%>
2. view.jsp
<%@ include file="/html/portlet/hello/init.jsp" %>
<%@ page import="com.kevinGQ.portlet.hello.util.*" %>
<%@ page import="com.kevinGQ.portlet.hello.model.Hello" %>
Welcome!
<table border="0" cellpadding="0" cellspacing="4" width="100%">
<tr>
<td align="center">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<%
for(int i = 0; i < names.length; i++)
{
Hello hello = HelloUtil.getHello(names[i]);
%>
<tr>
<td>
<font size="1" color="#0000FF"><%= names[i] %></font>
</td>
<td align="right">
<font size="1" color="#FF0000">
<%
if(hello != null){
%>
<%= hello.getHello() %>
<%
}else{
%>
<%= "null" %>
<%
}
%>
</font>
</td>
</tr>
<%
}
%>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
3 edit.jsp
<%@ include file="/html/portlet/hello/init.jsp" %>
<table cellpadding="4" cellspacing="0" border="0">
<form action="<portlet:actionURL><portlet:param name="struts_action" value="/hello/edit"/></portlet:actionURL>" method="post" name="<portlet:namespace />fm">
<input name="<portlet:namespace /><%= Constants.CMD %>" type="hidden" value="<%= Constants.UPDATE %>">
<tr>
<td align="center">
<table border="0" cellpadding="0" cellspacing="0">
<c:if test="<%= SessionMessages.contains(renderRequest, portletConfig.getPortletName() + \".doEdit\") %>">
<tr>
<td>
<font class="gamma" size="1"><span class="gamma-pos-alert"><%= LanguageUtil.get(pageContext, "you-have-successfully-updated-your-preferences") %></span></font>
</td>
</tr>
<tr>
<br>
</tr>
</c:if>
<tr>
<td>
<font class="gamma" size="2"><%= LanguageUtil.get(pageContext,"") %> </font>
</td>
</tr>
<tr>
<td>
<textarea class="form-text" cols="70" name="<portlet:namespace/>names" rows="10" wrap="soft"><%= StringUtil.merge(names, "\n") %></textarea>
</td>
</tr>
<tr>
<td>
<br>
</td>
</tr>
<tr>
<td align="center">
<input type="button" value="<bean:message key="save-settings" />" onClick="submitForm(document.<portlet:namespace />fm);">
</td>
</tr>
</table>
</td>
</tr>
</form>
</table>
三、 相關(guān)的java文件
1. HelloUtil.java
package com.kevinGQ.portlet.hello.util;
import com.kevinGQ.portlet.hello.model.Hello;
import com.liferay.portal.util.WebCacheException;
import com.liferay.portal.util.WebCachePool;
import com.liferay.portal.util.WebCacheable;
import com.liferay.cache.model.Cache;
public class HelloUtil {
public static Hello getHello(String name)
throws WebCacheException{
WebCacheable wc = new HelloConverter(name);
Cache cache = WebCachePool.get(
HelloUtil.class.getName() + name, wc);
return (Hello)cache.getObject();
}
}
2. HelloConverter.java 實(shí)現SOAP調用的主要類(lèi),加粗的代碼尤為重要,不寫(xiě)的話(huà)Axis Server端將會(huì )拋出錯誤。
package com.kevinGQ.portlet.hello.util;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.rpc.ParameterMode;
import org.apache.axis.encoding.XMLType;
import com.kevinGQ.portlet.hello.model.Hello;
import com.liferay.portal.util.WebCacheable;
import com.liferay.util.ConverterException;
import com.liferay.util.Time;
public class HelloConverter implements WebCacheable{
HelloConverter(String name){
_name = name;
}
public Object convert(String arg0) throws ConverterException {
Hello hello = new Hello();
try{
String endpoint = "http://localhost:80/axis/services/HelloService";
Service service = new Service();
Call call = (Call)service.createCall();
call.setSOAPActionURI("http://localhost:80/axis/services/HelloService#sayHello");
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setOperationName("sayHello");
call.addParameter("param1", XMLType.XSD_STRING, ParameterMode.IN);
call.setReturnType(XMLType.XSD_STRING);
String ret = (String) call.invoke(new Object[]{_name});
hello.setHello(ret);
}catch(Exception e){
throw new ConverterException(_name+" "+e.toString());
}
return hello;
}
public long getRefreshTime(){
return _refreshTime;
}
private long _refreshTime = Time.DAY;
private String _name;
}
3. Hello.java
package com.kevinGQ.portlet.hello.model;
import java.io.Serializable;
public class Hello implements Serializable{
public Hello(){
//empty
}
public Hello(String hello){
_hello = hello;
}
public String getHello(){
return _hello;
}
public void setHello(String hello){
_hello = hello;
}
private String _hello;
}
4. EditPreferencesAction.java 對portlet的preferences進(jìn)行處理的類(lèi)
package com.kevinGQ.portlet.hello.action;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.PortletPreferences;
import javax.portlet.ValidatorException;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.liferay.portal.struts.PortletAction;
import com.liferay.portal.util.Constants;
import com.liferay.util.ParamUtil;
import com.liferay.util.StringUtil;
import com.liferay.util.servlet.SessionErrors;
import com.liferay.util.servlet.SessionMessages;
public class EditPreferencesAction extends PortletAction{
public void processAction(
ActionMapping mapping, ActionForm form, PortletConfig config,
ActionRequest req, ActionResponse res)
throws Exception{
String cmd = ParamUtil.getString(req, Constants.CMD);
if(!cmd.equals(Constants.UPDATE)){
return;
}
PortletPreferences prefs = req.getPreferences();
String[] names = StringUtil.split(
ParamUtil.getString(req, "names"), "\n");
prefs.setValues("names", names);
try {
prefs.store();
}
catch (ValidatorException ve) {
SessionErrors.add(req, ValidatorException.class.getName(), ve);
return;
}
SessionMessages.add(req, config.getPortletName() + ".doEdit");
}
public ActionForward render(
ActionMapping mapping, ActionForm form, PortletConfig config,
RenderRequest req, RenderResponse res)
throws Exception {
return mapping.findForward(getForward(req, "portlet.hello.edit"));
}
}
以上四個(gè)類(lèi)經(jīng)過(guò)編譯后,放到%TOMCAT_HOME%/liferay/WEB-INF/classes/,如果沒(méi)有classes文件夾,可以自己新建一個(gè)。
四.相關(guān)部署描述文件的修改
1、修改portlet.xml
添加如下片斷
<portlet>
<portlet-name>70</portlet-name>
<display-name>Hello To</display-name>
<portlet-class>com.liferay.portlet.StrutsPortlet</portlet-class>
<init-param>
<name>edit-action</name>
<value>/hello/edit</value>
</init-param>
<init-param>
<name>view-action</name>
<value>/hello/view</value>
</init-param>
<expiration-cache>300</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>edit</portlet-mode>
</supports>
<resource-bundle>com.liferay.portlet.StrutsResourceBundle</resource-bundle>
<portlet-preferences>
<preference>
<name>names</name>
<value>Kevin</value>
<value>John</value>
</preference>
</portlet-preferences>
<security-role-ref>
<role-name>Power User</role-name>
</security-role-ref>
<security-role-ref>
<role-name>User</role-name>
</security-role-ref>
</portlet>
2、修改struts-config.xml
在<action-mapping>標簽下添加
<!-- Hello -->
<action path="/hello/edit" type="com.kevinGQ.portlet.hello.action.EditPreferencesAction">
<forward name="portlet.hello.edit" path="portlet.hello.edit" />
</action>
<action path="/hello/view" forward="portlet.hello.view" />
3、修改liferay-portal.xml
在<portlets>標簽下添加
<portlet id="70" struts-path="hello" narrow="true"/>
4、修改liferay-display.xml
在<display>標簽下添加
<category name="category.myportlet">
<portlet id="70"/>
</category>
5、修改tiles-defs.xml
在<tiles-definitions>標簽下添加
<!-- Hello -->
<definition name="portlet.hello.edit" extends="portlet_default">
<put name="portlet_content" value="/portlet/hello/edit.jsp"/>
</definition>
<definition name="portlet.hello.view" extends="portlet_default">
<put name="portlet_content" value="/portlet/hello/view.jsp" />
</definition>
6、修改language.properties文件
由于liferay-tomcat-
javax.portlet.title.70=HelloTo
category.myportlet=MyPortlet
要實(shí)現portlet的國際化,還需要創(chuàng )建一系列的properties文件,比如說(shuō)簡(jiǎn)體中文的porperties_zh_CN.properties不過(guò)里面的中文用unicode編寫(xiě),就不在這里說(shuō)明了。
以上是我的實(shí)踐所得,所有代碼經(jīng)過(guò)實(shí)驗證實(shí)可行,這個(gè)例子簡(jiǎn)單而又有代表性,自己動(dòng)手寫(xiě)寫(xiě),受益匪淺。
聯(lián)系客服