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

来源:百度文库 编辑:神马文学网 时间:2024/04/28 20:00:31
使用的主要工具有:liferay-tomcat-3.1.0  axis-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片断




二、             写portlet的三个JSP文件(参照liferay里的weather portlet)
1.         init.jsp
<%@ include file="/html/common/init.jsp" %>

<%
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!











<%
for(int i = 0; i < names.length; i++)
{
Hello hello = HelloUtil.getHello(names[i]);
%>




<%
}
%>

<%= names[i] %>


<%
if(hello != null){
%>
<%= hello.getHello() %>
<%
}else{
%>
<%= "null" %>
<%
}
%>




3     edit.jsp
<%@ include file="/html/portlet/hello/init.jsp" %>

" method="post" name="fm">







">





















<%= LanguageUtil.get(pageContext, "you-have-successfully-updated-your-preferences") %>

<%= LanguageUtil.get(pageContext,"") %>






" onClick="submitForm(document.fm);">


三、             相关的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;
}
private String _hello;
}
4.         EditPreferencesAction.java 对portlet的preferences进行处理的类
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"));
}
}
以上四个类经过编译后,放到%TOMCAT_HOME%/liferay/WEB-INF/classes/,如果没有classes文件夹,可以自己新建一个。
四.相关部署描述文件的修改
1、修改portlet.xml
添加如下片断

70
Hello To
com.liferay.portlet.StrutsPortlet

edit-action
/hello/edit


view-action
/hello/view

300

text/html
edit

com.liferay.portlet.StrutsResourceBundle


names
Kevin
John



Power User


User


2、修改struts-config.xml
标签下添加





3、修改liferay-portal.xml
标签下添加

4、修改liferay-display.xml
标签下添加



5、修改tiles-defs.xml
标签下添加







6、修改language.properties文件
由于liferay-tomcat-3.1.0里该文件是封装在portal-ejb.jar文件里,所以需要在%TOMCAT_HOME%/liferay/WEB-INF/下创建content文件夹,并在里面创建properties.languange文件
javax.portlet.title.70=HelloTo
category.myportlet=MyPortlet
要实现portlet的国际化,还需要创建一系列的properties文件,比如说简体中文的porperties_zh_CN.properties不过里面的中文用unicode编写,就不在这里说明了。
以上是我的实践所得,所有代码经过实验证实可行,这个例子简单而又有代表性,自己动手写写,受益匪浅。