Remplacement de l'affichage appelé par une commande de contrôleur

Pour remplacer l'affichage appelé par une commande de contrôleur, créez une nouvelle classe d'implémentation pour la commande contrôleur. Par exemple, créez un nouveau ModifiedControllerCmdImpl qui étend ExistingControllerCmdImpl et implémente l'interface ExistingControllerCmd. Notez que cela ne s'applique qu'à la couche d'application Web.

Pourquoi et quand exécuter cette tâche

Dans la classe ModifiedControllerCmdImpl, remplacez la méthode performExecute. Dans la nouvelle méthode performExecute, appelez super.performExecute pour vous assurer que tout le traitement des commandes se produit. Une fois la logique de commande exécutée, vous pouvez utiliser les propriétés de réponse pour remplacer l'affichage appelé. L'extrait de code suivant montre comment remplacer une vue redirect :


// Import the packages containing TypedProperty and ECConstants.
import com.ibm.commerce.datatype.*;
import com.ibm.commerce.server.*;

public class ModifiedControllerCmdImplImpl extends ExistingControllerCmdImpl 
   implements ExistingControllerCmd 
   {
      public void performExecute () 
         throws com.ibm.commerce.exception.ECException 
         {
            // Execute the original command logic.
            super.performExecute();

            // Use the response properties returned from the superclass (preceding call).
            // If the superclass has not returned any, then create a new response 
            // properties object.
            TypedProperty responseProperties = getResponseProperties();
            if (responseProperties == null) 
            {
                    responseProperties = new TypedProperty();
                    setResponseProperties(responseProperties);
            }

            // Replace the view name in the response properties.
            responseProperties.put(ECConstants.EC_VIEWTASKNAME, "MyView");

            ///////////////////////////////////////////////////// 
            //  The following line is optional. The Struts     //
            //  configuration files can specify the redirect   //
            //  URL.                                           //
            /////////////////////////////////////////////////////
            responseProperties.put(ECConstants.EC_REDIRECTURL, MyURL);
         }
   }    

L'extrait de code suivant montre comment remplacer une vue forward :


// Import the packages containing TypedProperty and ECConstants.
import com.ibm.commerce.datatype.*;
import com.ibm.commerce.server.*;

public class ModifiedControllerCmdImplImpl extends ExistingControllerCmdImpl 
   implements ExistingControllerCmd 
   {
      public void performExecute () 
         throws com.ibm.commerce.exception.ECException 
         {
            // Execute the original command logic.
            super.performExecute();

            // Use the response properties returned from the superclass (preceding call).
            // If the superclass has not returned any, then create a new response 
            // properties object.
            TypedProperty responseProperties = getResponseProperties();
            if (responseProperties == null) 
            {
                    responseProperties = new TypedProperty();
                    setResponseProperties(responseProperties);
            }

            // Replace the view name in the response properties.
            responseProperties.put(ECConstants.EC_VIEWTASKNAME, "MyView");

            ///////////////////////////////////////////////////// 
            //  The following line is optional. The Struts     //
            //  configuration files can specify the JSP page.  //
            /////////////////////////////////////////////////////
            responseProperties.put(ECConstants.EC_DOCPATHNAME, "MyJSP.jsp");  
         }
    }