Class ConventionUnknownHandler

java.lang.Object
org.apache.struts2.convention.ConventionUnknownHandler
All Implemented Interfaces:
UnknownHandler

public class ConventionUnknownHandler extends Object implements UnknownHandler

This class is the default unknown handler for all of the Convention plugin integration with XWork. This handles instances when a URL doesn't have an action associated with it but does a viable result and also the instance where an action returns a result code that isn't already configured but there is a viable result for the code.

This class also handles all of the index actions using redirects and actions in nested packages. For example, if there is an action /foo/index and the URL /foo is used, this will render the index action in the /foo namespace.

  • Field Details

    • configuration

      protected Configuration configuration
    • objectFactory

      protected ObjectFactory objectFactory
    • servletContext

      protected jakarta.servlet.ServletContext servletContext
    • resultMapBuilder

      protected ResultMapBuilder resultMapBuilder
    • defaultParentPackageName

      protected String defaultParentPackageName
    • parentPackage

      protected PackageConfig parentPackage
    • allowedMethods

      protected Set<String> allowedMethods
  • Constructor Details

    • ConventionUnknownHandler

      public ConventionUnknownHandler(Configuration configuration, ObjectFactory objectFactory, jakarta.servlet.ServletContext servletContext, Container container, String defaultParentPackageName, String redirectToSlash, String nameSeparator)
      Constructs the unknown handler.
      Parameters:
      configuration - The XWork configuration.
      objectFactory - The XWork object factory used to create result instances.
      servletContext - The servlet context used to help build the action configurations.
      container - The Xwork container
      defaultParentPackageName - The default XWork package that the unknown handler will use as the parent package for new actions and results.
      redirectToSlash - A boolean parameter that controls whether or not this will handle unknown actions in the same manner as Apache, Tomcat and other web servers. This handling will send back a redirect for URLs such as /foo to /foo/ if there doesn't exist an action that responds to /foo.
      nameSeparator - The character used as word separator in the action names. "-" by default
  • Method Details

    • handleUnknownAction

      public ActionConfig handleUnknownAction(String namespace, String actionName) throws StrutsException
      Description copied from interface: UnknownHandler
      Handles the case when an action configuration is unknown. Implementations can return a new ActionConfig to be used to process the request.
      Specified by:
      handleUnknownAction in interface UnknownHandler
      Parameters:
      namespace - The namespace
      actionName - The action name
      Returns:
      An generated ActionConfig, can return null
      Throws:
      StrutsException - in case of errors
    • findResource

      protected ConventionUnknownHandler.Resource findResource(Map<String,ResultTypeConfig> resultsByExtension, String... parts)
      Finds a resource using the given path parts and all of the extensions in the map.
      Parameters:
      resultsByExtension - Map of extension to result type config objects.
      parts - The parts of the resource.
      Returns:
      The resource path or null.
    • canonicalize

      protected String canonicalize(String path)
    • buildActionConfig

      protected ActionConfig buildActionConfig(String path, ResultTypeConfig resultTypeConfig)
    • handleUnknownResult

      public Result handleUnknownResult(ActionContext actionContext, String actionName, ActionConfig actionConfig, String resultCode) throws StrutsException
      Description copied from interface: UnknownHandler
      Handles the case when a result cannot be found for an action and result code.
      Specified by:
      handleUnknownResult in interface UnknownHandler
      Parameters:
      actionContext - The action context
      actionName - The action name
      actionConfig - The action config
      resultCode - The returned result code
      Returns:
      A result to be executed, can return null
      Throws:
      StrutsException - in case of errors
    • findResult

      protected Result findResult(String path, String resultCode, String ext, ActionContext actionContext, Map<String,ResultTypeConfig> resultsByExtension)
    • buildResult

      protected Result buildResult(String path, String resultCode, ResultTypeConfig config, ActionContext invocationContext)
    • string

      protected String string(String... parts)
    • determinePath

      protected String determinePath(ActionConfig actionConfig, String namespace)
      Determines the result path prefix that the request URL is for, minus the action name. This includes the base result location and the namespace, with all the slashes handled.
      Parameters:
      actionConfig - (Optional) The might be a ConventionActionConfig, from which we can get the default base result location of that specific action.
      namespace - The current URL namespace.
      Returns:
      The path prefix and never null.
    • handleUnknownActionMethod

      public Object handleUnknownActionMethod(Object action, String methodName)
      Not used
      Specified by:
      handleUnknownActionMethod in interface UnknownHandler
      Parameters:
      action - The action object
      methodName - The method name to call
      Returns:
      The result returned from invoking the action method, can return null if this instance cannot handle such methodName to allow other handlers give a chance