使用的主要工具有:liferay-tomcat- 下载得到这两个工具的zip文件夹,分别解压到两个目录里, 在环境变量里把TOMCAT_HOME, AXIS_HOME分别指向这两个文件夹根目录。 具体配置方法分别参考 实现功能:在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- javax.portlet.title.70=HelloTo category.myportlet=MyPortlet 要实现portlet的国际化,还需要创建一系列的properties文件,比如说简体中文的porperties_zh_CN.properties不过里面的中文用unicode编写,就不在这里说明了。 以上是我的实践所得,所有代码经过实验证实可行,这个例子简单而又有代表性,自己动手写写,受益匪浅。 |
|
来自: smoking_boy > 《我的图书馆》