0 Replies Latest reply on Mar 11, 2014 9:16 AM by kamal.java

    how to make a prepare method in struts 2


      Hi i am having the following action in my struts application, but m stucked up wid the problem where i want to implement preparable interface and use of customize methods like prepareCreate() etc see the action below :    

       package manning.chapterThree;
      import static com.opensymphony.xwork2.Action.SUCCESS;
      import manning.chapterThree.utils.PortfolioService;
      import manning.chapterThree.utils.User;
      import com.opensymphony.xwork2.ActionSupport;
      import com.opensymphony.xwork2.Preparable;
       * This is our first version of the Register action.  This version uses
       * the basic validation and message localization services provided by the 
       * ActionSupport help class.  By extending this class, we automatically
       * receive implementations of several interfaces that allow us to do
       * validation and localize our message texts with out polluting the
       * execute() method of our action.   
      public class Register extends ActionSupport implements Preparable{
          public String execute(){
               * Create and move the data onto our application domain object, user.
      //        User user = new User();
      //        user.setPassword( getPassword() );
      //        user.setPortfolioName( getPortfolioName() );
      //        user.setUsername( getUsername() );
      //        getPortfolioService().createAccount( user ); 
              return SUCCESS;
          /* JavaBeans Properties to Receive Request Parameters */
          private String username;
          private String password;
          private String portfolioName;
          public String getPortfolioName() {
              return portfolioName;
          public void setPortfolioName(String portfolioName) {
              this.portfolioName = portfolioName;
          public String getPassword() {
              return password;
          public void setPassword(String password) {
              this.password = password;
          public String getUsername() {
              return username;
          public void setUsername(String username) {
              this.username = username;
          /* Validateable Implementation 
           * The validate method validates, invoked by the validation interceptor in
           * the default stack, will validate the data already set on the action
           * by the params interceptor, also in the default stack.  
           * If this method finds problems in validation it stores error messages via
           * the methods exposed by the ValidationAware interface -- already implemented
           * by the ActionSupport class that this action extends.  To complete the 
           * the validation process, the workflow interceptor fires next in the default
           * stack.  It checks for error messages on the action, and if it finds them 
           * it diverts workflow back to the input page where the error messages are 
           * displayed to the user.  In this case, the execute method of the action
           * will not be called because the workflow was diverted, due to validation
           * problems, before execution reached the action itself.
           * */
          public void validate(){
              /* Retrieve our simple portfolio service object. */
              PortfolioService ps = getPortfolioService();
              /* Check that fields are not empty */
              if ( getPassword().length() == 0 ){            
                  addFieldError( "password", getText("password.required") );
              if ( getUsername().length() == 0 ){            
                  addFieldError( "username", getText("username.required") );
              if ( getPortfolioName().length() == 0  ){            
                  addFieldError( "portfolioName", getText( "portfolioname.required" ));
              /* Make sure user doesn't already have an account */
              if ( ps.userExists(getUsername() ) ){        
                  addFieldError("username", getText( "user.exists"));
           * Simple way to retrieve our business logic and data persistence
           * object.  Late versions of the portfolio app will integrate with
           * more sophisticated technologies for these services.
          public PortfolioService getPortfolioService( )     {
                //   update();
              return new PortfolioService();
          public void prepare() throws Exception {
              System.out.println("Calling prepare itself");
            //  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
          void create() throws Exception
            System.out.println("Calling Update Updating ...");
          void prepareCreate () throws Exception
            System.out.println("Prepare Updating ...");            

      i called create() on line 26, the framework should call prepareCreate() prior to create , but it is not executing , only prepare method get called , any help will be appreciated , THANKS IN ADVANCE