分享
 
 
 

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

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

使用的主要工具有: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片断

<service name="HelloService" provider="java:RPC">

<parameter name="allowedMethods" value="sayHello"/>

<parameter name="className" value="com.kevinGQ.service.HelloService"/>

</service>

二、 写portlet的三个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>

三、 相关的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

添加如下片断

<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-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编写,就不在这里说明了。

以上是我的实践所得,所有代码经过实验证实可行,这个例子简单而又有代表性,自己动手写写,受益匪浅。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有