Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Create an Interceptor class by implementing the com.opensymphony.xwork.interceptor.Interceptor interface (bundled in the xwork-1.1.jar provided with the framework distribution).
  2. Declare the class in your XML configuration file (actionstruts.xml) using the element <interceptor /> nested within <interceptors />.
  3. Create stacks of Interceptors, using the <interceptor-stack /> element (optional).
  4. Determine which Interceptors are used by which mapping, using <interceptor-ref /> or <default-interceptor-ref />. The <interceptor-ref> defines the interceptors to be used in a specific action. The <default-interceptor-ref /> determines the default interceptor stack to be used by all actions that do not specify their own <interceptor-ref />.

...

Code Block
java
java
titleGreetingInterceptor.java
package tutorial;

import java.util.Calendar;
import com.opensymphony.xwork.interceptor.Interceptor;
import com.opensymphony.xwork.ActionInvocation;

public class GreetingInterceptor implements Interceptor {
  public void init() { }
  public void destroy() { }
  public String intercept(ActionInvocation invocation) throws Exception {
    Calendar calendar = Calendar.getInstance();
    int hour = calendar.get(Calendar.HOUR_OF_DAY);
    String greeting = (hour < 6) ? "Good evening" :
        ((hour < 12) ? "Good morning":
  	  ((hour < 18) ? "Good afternoon": "Good evening"));

     invocation.getInvocationContext().getSession().put("greeting", greeting);

     String result = invocation.invoke();

     return result;
  }
}

...

Code Block
xml
xml
titlestruts.xml
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
  <include file="struts-default.xml" />

  <package name="default" extends="struts-default">
     <interceptors>
       <interceptor name="greeting" class="tutorial.GreetingInterceptor" />
     </interceptors>

     <action name="greeting" class="tutorial.GreetingAction">
    	<interceptor-ref name="greeting" />
 	   <result name="success" type="velocity">greeting.vm</result>
      </action>

      <!-- other action mappings -->

   </package>
 </struts>

...