By default, the portletaction Extension Based on Struts action provided by liferay does not support multiple distribution commands, which are commonly used dispatchaction. However, in our daily Struts-based operations, a large number of dispatchaction processing methods have been used, such as "cmd = queryall.
This article explains how to extend liferay to support multi-distribution command actions. First, let's take a look at how liferay is handled: In the Portlet. XML, we usually configure the following:
<Portlet-class> com. liferay. Portlet. strutsportlet </Portlet-class>
<Init-param>
<Name> View-Action </Name>
<Value>/EXT/reports/view_reports </value>
</Init-param> in this way, when liferay is facing a Portlet request, the doview or doedit method of the Portlet is executed based on the request model. When doview is executed, the action represented by the Action URL configured by view-action is requested for processing. The process is roughly as follows:
Portlet class --> requestprocessor --> strutsaction processing class.
We can use two extension schemes to support multi-distribution:
Solution (1 ):Extend the strutsportlet class of liferay and write a dispatchportletaction class, so you do not need to extend the requestprocessor implementation.
Solution 2 ):Extend requestprocessor and write a dispatchportletaction class, so that you can directly use the strutsportlet class provided by liferay. For the extension of requestprocessor, configure the "struts. Portlet. Request. processor" attribute in the portal. properties file. Next, we will explain the two solutions in detail (This article first describes solution 1 ):
Solution (1)First, let's write a dispatchportletaction class. This class can be implemented by extending the portletaction of liferay, or by extending the dispatchaction of struts. I chose the latter method. In this way, the number of code extensions is small. Do not write the execute Method on your own. simply use the base class. For dispatchportletaction, The dispatchmethod and getmethodname methods are mainly extended. Note that in the getmechodname method, the request is also appended. getattribute (parameter) gets the method name and pays attention to the unspecified method. This is executed by default when no access method is specified. Therefore, developers must implement this when writing their own actions.
Public class dispatchportletaction extends dispatchaction ...{
Protected actionforward dispatchmethod (actionmapping mapping,
Actionform form, httpservletrequest request,
Httpservletresponse response, string name) throws exception ...{
Portletconfig = (portletconfig) Request
. Getattribute (webkeys. javax_portlet_config );
Renderrequest = (renderrequest) Request
. Getattribute (webkeys. javax_portlet_request );
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 methodname;
}
Public actionforward unspecified (actionmapping mapping, actionform form,
Portletconfig config, renderrequest req, renderresponse res)
Throws exception ...{