struts.xml interceptor-stack 要素とは何か
interceptor-stack 要素とは何か
interceptors.interceptor
をひとまとまり に定義したもの
利用により、汎用的に interceptor
を interceptor-ref
で参照できる
具体的に
- interceptor が
- ロギング
- DB コネクション処理
の 2 つで定義されているとする
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <constant name="struts.devMode" value="true" /> <constant name="struts.ui.theme" value="simple" /> <package name="top" extends="struts-default"> <interceptors> <interceptor name="myLogging" class="jp.ymatsukawa.interceptors.Logger" /> <interceptor name="myDBConnection" class="jp.ymatsukawa.interceptors.DBConnection" /> </interceptors> <action name="index" class="jp.ymatsukawa.top.Index"> <interceptor-ref name="myLogging" /> <interceptor-ref name="myDBConnection" /> <result name="success">/index.jsp</result> </action> </package> </struts>
action
が新規追加されるごとに action#name="index"
の
interceptor-ref
がコピペされそうである
汎用的に これらまとまり(interceptors.interceptor
) を参照できる形式にしよう
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <constant name="struts.devMode" value="true" /> <constant name="struts.ui.theme" value="simple" /> <package name="top" extends="struts-default"> <interceptors> <interceptor name="myLogging" class="jp.ymatsukawa.interceptors.Logger" /> <interceptor name="myDBConnection" class="jp.ymatsukawa.interceptors.DBConnection" /> <interceptor-stack name="myStackDBCon"> <interceptor-ref name="myLogging" /> <interceptor-ref name="myDBConnection" /> </interceptor-stack> </interceptors> <action name="index" class="jp.ymatsukawa.top.Index"> <interceptor-ref name="myStackDBCon" /> <result name="success">/index.jsp</result> </action> </package> </struts>
struts.package.interceptors.interceptor
の下に
interceptor-stack
を追加interceptor-stack#name
に 各action
のinterceptor-ref
が参照する名前を命名
[参考元]
https://struts.apache.org/docs/interceptors.html#Interceptors-StackingInterceptors