RequestParameterMapper はサーブレット環境用の Command の実装です。
機能はサーブレットの呼び出しに使われたリクエストパラメータの内容と対応する Command を呼び出して実行すること。
具体的に。
ChainProcessor が URL パターン
/execute
にマッピングされているとします。
このとき、
http://server:port/webapp_name/execute?command=sample1
というリクエストが送られてくると RequestParameterMapper は対応する Command "sample1" を Catalog から取り出して実行します。
RequestParameterMapper を使うときに特別にしなければならないことは他の Mapper と同じで次の 2 つ。
RequestParameterMapper は Context からプロパティ catalogKey で指定されたキーで Catalog を取り出します。
そして、リクエストパラメータの内容に応じた Command を Catalog から取り出して実行します。
ということで RequestParameterMapper が実行される前に Context に Catalog を詰めておいてやる必要があります。
今回のサンプルでは Context に Catalog を詰める Command を作ってみました。
コードの方はサンプルのところで。
実際のところは xml ファイルで記述せずともコードで書いてやれば Chain の設定は可能です。
ただ、xml で書いておいたほうが便利なので説明は xml です。
Command タグで指定する属性は次の通り。
xml を書くときのポイントはこれだけ。
<command catalogKey="キーを指定" parameter="パラメータ名" className="org.apache.commons.chain.web.servlet.RequestParameterMapper"/>こんな感じです。
今回作成したサンプルの全ソースコードです。
サンプルの内容はというと単に画面を表示するだけの Web アプリケーションです。
まずは web.xml。
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
    <context-param>
        <param-name>org.apache.commons.chain.CONFIG_WEB_RESOURCE</param-name>
        <param-value>/WEB-INF/chain-config.xml</param-value>
    </context-param>
    <servlet>
        <servlet-name>ChainProcessor</servlet-name>
        <servlet-class>org.apache.commons.chain.web.servlet.ChainProcessor</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ChainProcessor</servlet-name>
        <url-pattern>/execute</url-pattern>
    </servlet-mapping>
</web-app>
次に Chain の設定ファイル (chain-config.xml)。
<catalog>
    <chain name="command">
        <command toKey="catalogKey" className="javademo.commands.PrepareMapper"/>
        <command catalogKey="catalogKey" parameter="command" className="org.apache.commons.chain.web.servlet.RequestParameterMapper"/>
    </chain>
    <command name="sample1" className="javademo.commands.Sample1"/>
    <command name="sample2" className="javademo.commands.Sample2"/>
</catalog>
作った Command の実装たちです。
PrepareMapper は Context に Catalog を詰めるために作った Command。
package javademo.commands;
import org.apache.commons.chain.Context;
import org.apache.commons.chain.Command;
import org.apache.commons.chain.Catalog;
import org.apache.commons.chain.CatalogFactory;
import org.apache.commons.chain.web.servlet.ServletWebContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
public class PrepareMapper implements Command{
    private String fromKey;
    private String toKey;
    
    public String getFromKey(){
        return this.fromKey;
    }
    
    public void setFromKey(String key){
        this.fromKey = key;
    }
    
    public String getToKey(){
        return this.toKey;
    }
    
    public void setToKey(String key){
        if(key == null) throw new IllegalArgumentException("ToKey is must not null.");
        this.toKey = key;
    }
    
    public boolean execute(Context context) throws Exception{
        CatalogFactory factory = CatalogFactory.getInstance();
        
        Catalog catalog;
        if(getFromKey() == null){
            catalog = factory.getCatalog();
        }
        else{
            catalog = factory.getCatalog(getFromKey());
        }
        context.put(getToKey(), catalog);
        return false;
    }
}
Sample1 と Sample2 はそれぞれ画面遷移の処理を行う Command です。
package javademo.commands;
import org.apache.commons.chain.Context;
import org.apache.commons.chain.Command;
import org.apache.commons.chain.web.servlet.ServletWebContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
public class Sample1 implements Command{
    public boolean execute(Context context) throws Exception{
        ServletWebContext webContext = (ServletWebContext) context;
        HttpServletRequest request = webContext.getRequest();
        RequestDispatcher dispatcher = request.getRequestDispatcher("/sample.jsp");
        dispatcher.forward(request, webContext.getResponse());
        
        return false;
    }
}
package javademo.commands;
import org.apache.commons.chain.Context;
import org.apache.commons.chain.Command;
import org.apache.commons.chain.web.servlet.ServletWebContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
public class Sample2 implements Command{
    public boolean execute(Context context) throws Exception{
        ServletWebContext webContext = (ServletWebContext) context;
        HttpServletRequest request = webContext.getRequest();
        RequestDispatcher dispatcher = request.getRequestDispatcher("/sample2.jsp");
        dispatcher.forward(request, webContext.getResponse());
        
        return false;
    }
}
そして最後が表示内容の JSP、sample.jsp と sample2.jsp です。
中身は何もしてません。
sample.jsp ファイル。
<html>
    <body>This page is sample.</body>
</html>
sample2.jsp ファイル。
<html>
    <body>This page is sample2.</body>
</html>
web アプリケーション名は sample で、ファイルの配置は
で、残るは実行です。
http://localhost:8080/sample/execute?command=sample1
にアクセスすると sample.jsp の内容が、
http://localhost:8080/sample/execute?command=sample2
にアクセスすると sample2.jsp の内容が表示されるハズです。(Tomcat デフォルト設定の場合)
RequestParameterMapper はサーブレット呼び出し時のリクエストパラメータを元に Catalog から Command を取り出して実行する。