在Liferay Portal Professional里实现一个使用SOAP的portlet

使用的主要工具有: liferay-tomcat- 3.1.0 a xis-1.1 。

下载得到这两个工具的 zip 文件夹,分别解压到两个目录里,

在环境变量里把 TOMCAT_HOME, AXIS_HOME 分别指向这两个文件夹根目录。

具体配置方法分别参考

Liferay

Axis

实现功能:在 portlet 里用 SOAP 访问 Web Service ,把得到的结果显示在 Portal 界面

** 一、 ** ** 配置 Axis Server ** ** 端 **

1. 简单的服务程序,类似 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 片断

1<service name="HelloService" provider="java:RPC">
2<parameter name="allowedMethods" value="sayHello"></parameter>
3<parameter name="className" value="com.kevinGQ.service.HelloService"></parameter>
4</service>

** 二、 ** ** 写 portlet ** ** 的三个 JSP ** ** 文件(参照 liferay ** ** 里的 weather portlet ** ** ) **

** 1. ** ** init.jsp **

1@ include file="/html/common/init.jsp" 
1<portlet:defineobjects></portlet:defineobjects>
1 
2
3PortletPreferences prefs = renderRequest.getPreferences(); 
4
5String[] names = prefs.getValues("names", new String[0]); 

** 2. ** ** view.jsp **

1@ include file="/html/portlet/hello/init.jsp" 
1@ page import="com.kevinGQ.portlet.hello.util.*" 
1@ page import="com.kevinGQ.portlet.hello.model.Hello" 

Welcome!

1<table border="0" cellpadding="0" cellspacing="4" width="100%">
2<tr>
3<td align="center">
4<table border="0" cellpadding="0" cellspacing="0">
5<tr>
6<td>
7<table border="0" cellpadding="0" cellspacing="0" width="100%">

for(int i = 0; i &lt; names.length; i++)

{

Hello hello = HelloUtil.getHello(names[i]);

1
2<tr>
3<td>
4<font color="#0000FF" size="1">```
5= names[i] 
6```</font>
7</td>
8<td align="right">
9<font color="#FF0000" size="1">

if(hello != null){

= hello.getHello()

}else{

= "null"

}

1
2</font>
3</td>
4</tr>

}

1
2</table>
3</td>
4</tr>
5</table>
6</td>
7</tr>
8</table>

** 3 edit.jsp **

1@ include file="/html/portlet/hello/init.jsp" 
 1<table border="0" cellpadding="4" cellspacing="0">
 2<form action="&lt;portlet:actionURL&gt;&lt;portlet:param name=" struts_action"="" value="/hello/edit"></form>" method="post" name="<portlet:namespace></portlet:namespace>fm"&gt;
 3
 4<input name="&lt;portlet:namespace /&gt;```
 5= Constants.CMD 
 6```" type="hidden" value="```
 7= Constants.UPDATE 
 8```"/>
 9<tr>
10<td align="center">
11<table border="0" cellpadding="0" cellspacing="0">
12<c:if .doedit\")="" ```"="" test="```
13= SessionMessages.contains(renderRequest, portletConfig.getPortletName() + \">
14<tr>
15<td>
16<font class="gamma" size="1"><span class="gamma-pos-alert">```
17= LanguageUtil.get(pageContext, "you-have-successfully-updated-your-preferences") 
18```</span></font>
19</td>
20</tr>
21<tr>
22<br/>
23</tr>
24</c:if>
25<tr>
26<td>
27<font class="gamma" size="2">```
28= LanguageUtil.get(pageContext,"") 
29``` </font>
30</td>
31</tr>
32<tr>
33<td>
34<textarea class="form-text" cols="70" name="&lt;portlet:namespace/&gt;names" rows="10" wrap="soft">```
35= StringUtil.merge(names, "\n") 
36```</textarea>
37</td>
38</tr>
39<tr>
40<td>
41<br/>
42</td>
43</tr>
44<tr>
45<td align="center">
46<input save-settings"="" type="button" value="&lt;bean:message key=">" onClick="submitForm(document.<portlet:namespace></portlet:namespace>fm);"&gt;
47
48</input></td>
49</tr>
50</table>
51</td>
52</tr>
53
54</table>

** 三、 ** ** 相关的 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 ** ** 实现 SOAP ** ** 调用的主要类,加粗的代码尤为重要,不写的话 Axis Server ** ** 端将会抛出错误。 **

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;

}<

Published At
Categories with Web编程
Tagged with
comments powered by Disqus