Liferay MVC resourceコマンドに機能を追加する必要がある場合は、追加できます。Liferay MVCコマンドフレームワークは、MVC resourceコマンドのカスタマイズをサポートしています。MVCコマンドにロジックを追加するプロセスに従い、MVCRenderCommand
およびMVCActionCommand
で説明したものと同様です。留意すべきことがいくつかあります。
-
コンポーネントで指定するサービスは
MVCResourceCommand.class
です -
MVCRenderCommand
のオーバーライドと同様に、拡張する基本の実装クラスはありません。MVCResourceCommand
インターフェイスを自分で実装します。 -
オリジナルへの参照を取得し、呼び出しを
serveResource
メソッドに返すことで、元のMVCResourceCommand
のロジックにロジックを追加することにより、コードを元のコードから切り離したままにします。return mvcResourceCommand.serveResource(resourceRequest, resourceResponse);
次の例では、Liferayのログインポートレットのlogin-web
モジュールから、com.liferay.login.web.portlet.action.CaptchaMVCResourceCommand
の動作をオーバーライドします。コンソールに1行出力するだけで、元のロジック (アカウント作成画面用のCaptchaイメージを返す) を実行します。
@Component(
property = {
"javax.portlet.name=" + LoginPortletKeys.LOGIN,
"mvc.command.name=/login/captcha"
},
service = MVCResourceCommand.class
)
public class CustomCaptchaMVCResourceCommand implements MVCResourceCommand {
@Override
public boolean serveResource
(ResourceRequest resourceRequest, ResourceResponse resourceResponse) {
System.out.println("Serving login captcha image");
return mvcResourceCommand.serveResource(resourceRequest, resourceResponse);
}
@Reference(target =
"(component.name=com.liferay.login.web.internal.portlet.action.CaptchaMVCResourceCommand)")
protected MVCResourceCommand mvcResourceCommand;
}
説明は以上です。アプリケーションのソースコードを所有していない場合でも、コンポーネントのクラス名がわかればMVCコマンドをオーバーライドできます。