Liferay默认提供的基于
Struts Action扩展的PortletAction是不支持多分发命令的,也就是我们一般常用的DispatchAction。
但在我们日常基于
Struts处理的操作中,已经大量的沿用了DispatchAction处理方式,采用“cmd=queryall”诸如此类的方式。
本文就来给大家讲解如何通过扩展,让
Liferay实现对多分发命令Action的支持。
首先让我们来看看
Liferay是如何处理的:
在
portlet.xml中,我们一般会配置如下:
这样
Liferay面对一个Portlet请求的时候,会根据请求model来执行Portlet的doView或doEdit方式。当执行doView的时候就会请求其view-action所配置的Action URL所代表的Action来处理。
其处理流程大致是:
Portlet类——〉RequestProcessor——〉StrutsAction处理类
。
我们可以通过两种扩展方案来实现对多分发的支持:
方案(一):
扩展
Liferay的StrutsPortlet类,并写一个DispatchPortletAction类,这样不用扩展RequestProcessor实现。
方案(二):
扩展
RequestProcessor 与,并写一个DispatchPortletAction类,这样可以直接使用Liferay所提供的StrutsPortlet类。对于 RequestProcessor的扩展,在通过portal.properties文件中通过配置 “struts.portlet.request.processor”属性来设置。
接下来就两种方案做分别的详细讲解(本篇先讲方案一):
方案(一)
首先让我们写一个
DispatchPortletAction 类,此类可以通过扩展Liferay本身的PortletAction实现,也可以通过扩展Struts本身的DispatchAction实现。本人是选择后一种方式,这样扩展的代码量较少,都不要自己写execute方式,直接使用基类的即可。
对于
DispatchPortletAction 主要扩展dispatchMethod和getMethodName方法。注意在getMechodName方法中,还追加了从 request.getAttribute(parameter)获取方法名称,并注意unspecified方法,这个是在没有指明访问方法的时候默认执行的,所以开发人员在后续写自己的Action一定要实现这个。
public
class
DispatchPortletAction
extends
DispatchAction
{
protected ActionForward dispatchMethod(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response, String name) throws Exception {
PortletConfig portletConfig = (PortletConfig) request
.getAttribute(WebKeys.JAVAX_PORTLET_CONFIG);
RenderRequest renderRequest = (RenderRequest) request
.getAttribute(WebKeys.JAVAX_PORTLET_REQUEST);
RenderResponse renderResponse = (RenderResponse) request
.getAttribute(WebKeys.JAVAX_PORTLET_RESPONSE);
if (name == null ) {
return this .unspecified(mapping, form, portletConfig,
renderRequest, renderResponse);
}
Method method = null ;
try {
method = getMethod(name);
} catch (NoSuchMethodException e) {
String message = messages.getMessage( " dispatch.method " ,
mapping.getPath(), name);
log.error(message, e);
String userMsg = messages.getMessage( " dispatch.method.user " ,
mapping.getPath());
throw new NoSuchMethodException(userMsg);
}
ActionForward forward = null ;
try {
Object args[] = { mapping, form, portletConfig, renderRequest,
renderResponse } ;
forward = (ActionForward) method.invoke( this , args);
} catch (ClassCastException e) {
String message = messages.getMessage( " dispatch.return " ,
mapping.getPath(), name);
log.error(message, e);
throw e;
} catch (IllegalAccessException e) {
String message = messages.getMessage( " dispatch.error " ,
mapping.getPath(), name);
log.error(message, e);
throw e;
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
if (t instanceof Exception) {
throw ((Exception) t);
} else {
String message = messages.getMessage( " dispatch.error " ,
mapping.getPath(), name);
log.error(message, e);
throw new ServletException(t);
}
}
return (forward);
}
protected String getMethodName(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response,
String parameter) throws Exception {
String methodName = request.getParameter(parameter);
if (methodName == null || methodName.length() == 0 ) {
methodName = (String) request.getAttribute(parameter);
}
return
protected ActionForward dispatchMethod(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response, String name) throws Exception {
PortletConfig portletConfig = (PortletConfig) request
.getAttribute(WebKeys.JAVAX_PORTLET_CONFIG);
RenderRequest renderRequest = (RenderRequest) request
.getAttribute(WebKeys.JAVAX_PORTLET_REQUEST);
RenderResponse renderResponse = (RenderResponse) request
.getAttribute(WebKeys.JAVAX_PORTLET_RESPONSE);
if (name == null ) {
return this .unspecified(mapping, form, portletConfig,
renderRequest, renderResponse);
}
Method method = null ;
try {
method = getMethod(name);
} catch (NoSuchMethodException e) {
String message = messages.getMessage( " dispatch.method " ,
mapping.getPath(), name);
log.error(message, e);
String userMsg = messages.getMessage( " dispatch.method.user " ,
mapping.getPath());
throw new NoSuchMethodException(userMsg);
}
ActionForward forward = null ;
try {
Object args[] = { mapping, form, portletConfig, renderRequest,
renderResponse } ;
forward = (ActionForward) method.invoke( this , args);
} catch (ClassCastException e) {
String message = messages.getMessage( " dispatch.return " ,
mapping.getPath(), name);
log.error(message, e);
throw e;
} catch (IllegalAccessException e) {
String message = messages.getMessage( " dispatch.error " ,
mapping.getPath(), name);
log.error(message, e);
throw e;
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
if (t instanceof Exception) {
throw ((Exception) t);
} else {
String message = messages.getMessage( " dispatch.error " ,
mapping.getPath(), name);
log.error(message, e);
throw new ServletException(t);
}
}
return (forward);
}
protected String getMethodName(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response,
String parameter) throws Exception {
String methodName = request.getParameter(parameter);
if (methodName == null || methodName.length() == 0 ) {
methodName = (String) request.getAttribute(parameter);
}
return