portlet使用指南

摘要
  jsr-168是适合于portlet开发人员的java api集合。设计符合规范的jsr-168 portlet的原因有很多。可移植性就一个显而易见的好处。
根据规范编写的代码更容易在门户服务器之间移动。多数基于java的门户服务器都支持jsr-168 portlet。
  另一个好处是更易于联合。当portlet符合jsr-168规范时,通过web services for remote portlets (wsrp)生产者公开jsr-168 portlet会更容易一些。
wsrp提供了一个通过web service联合portlet内容的标准。jsr-168和wsrp 1.0 portlet功能是紧密耦合的。
jsr-168 to wsrp portlet桥利用jsr-168的url重写api。本文将阐述开发jsr-168 portlet以便获得可移植性的最佳实践。
[b]1. 总是利用url重写api,以获得portlet中的内容[/b]
  java开发人员经常在如下所示jsp中编写图像的url:
<img src="/<%= request.getcontextpath()%>/images/logo.gif"/>
  这在jsr-168 portlet中是不正确的。正确的方法是:
<img src="<%= renderresponse.encodeurl(renderrequest.getcontextpath()+"/images/logo.gif") %>"/>

  encodeurl()方法可以采用完全路径uri或者完全限定url。完全路径uri是最常用的。在使用jsr-168 portlet将资源嵌入web application archive (war)中时,
可以使用此技术。在将图像放置到单独服务器上时,可以使用完全限定url。专门为静态内容提供服务的缓存服务器就是一个示例,它卸掉来自门户服务器的通信量。
尽管可以通过对完全限定url使用encodeurl()来引用portlet以外的内容,但应该只在无法通过客户机访问资源时这样做。
如果客户机可以直接浏览资源,则无需对url使用encodeurl()。例如,如果有一台web服务器,
可用该服务器获得门户用户无法直接浏览的防火墙内的静态内容,则需要调用encodeurl()。
如果这些内容在防火墙之外,并且门户用户可以直接浏览到web服务器,则无需调用encodeurl()。
[b]2. 不要将路径附加到重写url中[/b]
  传入renderrequest的encodeurl()方法中的url在调用该方法之前必须是完整的。在调用该方法之后,无法添加url的某些部分。
例如,如果想从xslt转换中生成一个url转换,则不能将已编码的基本url(http://foo.com/)作为参数传递,
并将路径(pages/bar.jsp)附加到该转换中的已编码基本url中。
  以下调用演示了将url编码到图像中的正确方式:
<@= renderresponse.encodeurl(renderrequest.getcontextpath()+"/images/logo.gif")@>
  它使用一个.portal文件在bea weblogic portal 9.2中生成以下html片段:
<img src="http://localhost:7001/portalwebapp/images/logo.gif;
portal_tau=w3f6fbmllcgzq9fpv1jhls5rrjg8lgj2nndvjqdfshhrgfnsqckz!-545815275"/>

  以下调用是不正确的。url并不指向想要的资源。
<@= renderresponse.encodeurl(renderrequest.getcontextpath()+"/images/")+"logo.gif"@>
  它使用.portal文件在weblogic portal 9.2中生成以下html文件:
<img src="http://localhost:7001/portalwebapp/images/;portal_tau=w3f6fbmllcgzq9fpv1jhls5rrjg8lgj2nndvjqdfshhrgfnsqckz!-545815275logo.gif"/>

[b]3. 使用名称空间限定客户端脚本变量和方法[/b]
  假设您想使用portlet中的javas cript验证用户输入。以下javas cript功能可能很有用:
<s cript>
function validate(foo) {
if (foo.bar.value=="") {
return false;
}
return true;
}
</s cript>
  同一页面中的其他portlet可能也有一个命名为validate()的具有不同逻辑的javas cript方法。门户框架本身可能使用javas cript方法。
这个问题的解决方法是使用客户端脚本中的名称空间方法和顶层变量。<portlet:namespace/>标记将为每个portlet生成一个惟一标识符。
第一步是通过taglib directive将标记库包含在jsp中。
<%@taglib uri="http://java.sun.com/portlet" prefix="portlet"%>
  脚本中的validate()方法可以对标记加以区分。
<s cript>
function validate<portlet:namespace/>(foo) {
if (foo.bar.value=="") {
return false;
}
return true;
}
</s cript>
  以下是调用带名称空间的javas cript方法的方式:
<form action="http://www.somesite.org/servlet"
method="get" οnsubmit="return validate<portlet:namespace/>(this);">
<label for="bar">text(required): </label>
<input type="text" name="bar" id="bar">
</form>
[b]4. 确保引用portlet资源的内联客户端脚本符合规范 [/b] 
客户端脚本常常引用外部资源(如图像、电影和外部页面)来增强用户界面。常见的示例是预先加载图像以使交换图像更有效的javas cript。以下是一个示例:
<s cript>
function preloadimages(){
var menuimage = new image();
menuimage.src = "images/icon.gif";
var menuimagedark=new image();
menuimagedark.src = "images/icon.gif";
}
</s cript>
  客户端脚本中的url必须根据jsr-168规范进行重写。这些脚本必须在jsp或jsp-168 portlet类中,以便调用重写api的url。它们不能在单独的javas cript (.js)文件中。以下是一个包含url重写的适当名称空间脚本在jsr-168 portlet中看起来的样子:
<s cript>
function <portlet:namespace/>preloadimages(){
var menuimage = new image();
menuimage.src = "<%=renderresponse.encodeurl(renderrequest.getcontextpath()+ "images/icon.gif")%>";
var menuimagedark= new image();
menuimagedark.src = "<%=renderresponse.encodeurl(renderrequest.getcontextpath()+ "images/icon_dark.gif") %>";
}
</s cript>
[b]5. 总是为portlet响应声明一个内容类型[/b]  
根据jsr-168规范,"portlet必须使用renderresponse接口的setcontenttype方法设置响应的内容类型"。没有显式设置其内容类型的portlet仍然会成功获得编译。但weblogic portal不会执行没有设置其内容类型的portlet。确保您的portlet设置了其内容类型。  
以下示例演示了一个正确设置其内容类型的portlet:
public class myportlet extends genericportlet {
public void doview(renderrequest request, renderresponse response)
throws portletexception, ioexception {
response.setcontenttype("text/html");
printwriter writer = response.getwriter();
writer.println("i set my content type!");
}
}
  下面的示例是错误的,但仍将获得编译:
public class myportlet extends genericportlet {
public void doview(renderrequest request, renderresponse response)
throws portletexception, ioexception {
// no content type set!
printwriter writer = response.getwriter();
writer.println("i did not set my content type!");
}
}
[b]6. 不要从portlet发送cookie[/b]
根据jsr-168 portlet规范,在httpservletresponse上调用addcookie()实际上不会设置一个cookie。
允许设置cookie的portlet容器被打破。不要调用此方法。  
如果您喜欢在用户使用门户的时候基于每位用户持久存储信息,那么可以将信息存储为portlet会话中的一个属性。
如果您喜欢在用户退出后持久存储信息,那么可以将信息存储到数据存储库(文件系统、数据库、ldap等)中。
[b]7. 将业务逻辑从表示中分离出来[/b]
有经验的开发人员都知道模型查看器控制器框架类似于struts或beehive,可以使开发富web应用程序变得更容易。这同样也适用于portlet。
jsr-168并不是适用于平台独立portlet的惟一理想规范。wsrp portlet在实现标准的门户(包括非java门户)之间移动很方便。
weblogic portal 可以通过wsrp公开beehive和struts portlet。  
如果需要将portlet部署为jsr-168 war,您仍然有一些选择。将业务逻辑从jsr-168 portlet的表示逻辑中分离出来的最简单方法是指派一个javaserver page (jsp)。
portlet处理呈现方法(比如render()和doview())中的业务逻辑。
portlet使用应用程序级作用域或portlet作用域将信息传递给jsp。下面的示例将一个portlet请求指派给jsp,并传递portlet作用域中的一个字符串:
public void doview( renderrequest request,
renderresponse response)
throws portletexception, ioexception {
response.setcontenttype("text/html");
request.setattribute("foo","bar");
string jsp = "/pages/portal.jsp";
portletcontext ctx = getportletcontext();
portletrequestdispatcher dispatcher = ctx.getrequestdispatcher(jsp);
dispatcher.include(request, response);
}
  到达jsp(上述示例中的jsp)的路径值并不包括portlet的web归档文件(war)的上下文路径。  
jsr-168的指派方法允许将业务逻辑与表示分离。不过,它们缺乏mvc框架的成熟度。  
适用于jsr-168开发的框架包括: spring portlet mvc webwork struts action 2 struts action 2是struts和webwork的组合,
因此portlet代码库对现在而言几乎是一样的。这些框架简化了复杂portlet的开发和维护。
结束语:
遵守这些指导原则会使您的portlet符合jsr-168规范。遵守规范会使您的portlet在java门户服务器之间移动变得更容易。还会使利用wsrp联合门户内容变得更容易。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值