?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










































































更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
