「Struts2入門」インターセプタ(intercept)を実装するサンプル

1.インターセプタの実装

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class TestTnterceptor extends AbstractInterceptor {
   @Override
public String intercept(ActionInvocation invocation) throws Exception {
String result=null;
final ActionMapping atMapping=ServletActionContext.getActionMapping():
//処理コードsome code
result = invocation.invoke();
return result;
}
}
public class TestTnterceptor extends AbstractInterceptor {    @Override public String intercept(ActionInvocation invocation) throws Exception { String result=null; final ActionMapping atMapping=ServletActionContext.getActionMapping(): //処理コードsome code result = invocation.invoke(); return result; } }
public class TestTnterceptor extends AbstractInterceptor  {

   @Override
    public String intercept(ActionInvocation invocation) throws Exception {
    
        String result=null;
        final ActionMapping atMapping=ServletActionContext.getActionMapping():
        
        //処理コードsome code		
        
         result = invocation.invoke();

        return result;
    }
}

2.インターセプタの登録

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<package name="cft" namespace="/" extends="struts-default">
  <interceptors>
<interceptor name="TestTnterceptor" class="com.arkgame.study.TestTnterceptor" />
<interceptor-stack name="TestTnterceptorStack">
<interceptor-ref name="servletConfig" />
<interceptor-ref name="i18n" />
<interceptor-ref name="prepare" />
<interceptor-ref name="TestTnterceptor" />
</interceptor-stack>
  </interceptors>
</package>
<package name="cft" namespace="/" extends="struts-default">   <interceptors> <interceptor name="TestTnterceptor" class="com.arkgame.study.TestTnterceptor" /> <interceptor-stack name="TestTnterceptorStack"> <interceptor-ref name="servletConfig" /> <interceptor-ref name="i18n" /> <interceptor-ref name="prepare" /> <interceptor-ref name="TestTnterceptor" /> </interceptor-stack>   </interceptors> </package>
<package name="cft" namespace="/" extends="struts-default">
  <interceptors>
    <interceptor name="TestTnterceptor" class="com.arkgame.study.TestTnterceptor" />
            <interceptor-stack name="TestTnterceptorStack">
                <interceptor-ref name="servletConfig" />
                <interceptor-ref name="i18n" />
                <interceptor-ref name="prepare" />
                <interceptor-ref name="TestTnterceptor" />
            </interceptor-stack>
  </interceptors>
</package>

3.インターセプタを呼出す

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@ParentPackage("cft")
public class TestAction extends BaseAction{
//some code
}
@ParentPackage("cft") public class TestAction extends BaseAction{ //some code }
@ParentPackage("cft")
public class TestAction extends BaseAction{

   //some code
 }

 

Struts2

Posted by arkgame