Interceptors

Site Admin · 11 Sep 2026 · 2 views

What Are Interceptors?

Interceptors are classes that run before and after an action, letting you add cross-cutting behaviour without touching action code. They form a chain: the parameters interceptor populates action fields from the request, the validation interceptor checks them, and workflow decides success or error - all before your action method even runs.

Struts 2 interceptor stack

Built-In Interceptors

  • params - copies request parameters into action properties.
  • validation - runs configured validators.
  • workflow - returns input result on validation errors.
  • fileUpload - handles multipart uploads.
  • session - makes the HTTP session available in the ActionContext.

You use them by declaring the stack in struts.xml:

<package name="default" extends="struts-default">
  <action name="login" class="com.example.LoginAction">
    <result name="success">/welcome.jsp</result>
    <result name="input">/login.jsp</result>
  </action>
</package>

Custom Interceptors

Write one by extending AbstractInterceptor and register it in the stack.

Key Points

  • Interceptors add services horizontally across all actions.
  • The default stack already handles params, validation and workflow.
  • Order in the stack matters: invocations nest in and out.
Share this post:

Comments (0)

Please login or register to comment.