Save This Page
Home » struts-2.1.8.1-src » org.apache » struts2 » showcase » token » [javadoc | source]
    1   /*
    2    * $Id: TokenAction.java 642323 2008-03-28 18:07:22Z hermanns $
    3    *
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
   10    * with the License.  You may obtain a copy of the License at
   11    *
   12    *  http://www.apache.org/licenses/LICENSE-2.0
   13    *
   14    * Unless required by applicable law or agreed to in writing,
   15    * software distributed under the License is distributed on an
   16    * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   17    * KIND, either express or implied.  See the License for the
   18    * specific language governing permissions and limitations
   19    * under the License.
   20    */
   21   package org.apache.struts2.showcase.token;
   22   
   23   import java.util.Date;
   24   
   25   import com.opensymphony.xwork2.ActionContext;
   26   import com.opensymphony.xwork2.ActionSupport;
   27   
   28   /**
   29    * Example to illustrate the <code>token</code> and <code>tokenSession</code> interceptor.
   30    *
   31    */
   32   public class TokenAction extends ActionSupport {
   33   
   34       private static final long serialVersionUID = 616150375751184884L;
   35   
   36       private int amount;
   37   
   38       public String execute() throws Exception {
   39           // transfer from source to destination
   40   
   41           Integer balSource = (Integer) ActionContext.getContext().getSession().get("balanceSource");
   42           Integer balDest = (Integer) ActionContext.getContext().getSession().get("balanceDestination");
   43   
   44           Integer newSource = new Integer(balSource.intValue() - amount);
   45           Integer newDest = new Integer(balDest.intValue() + amount);
   46   
   47           ActionContext.getContext().getSession().put("balanceSource", newSource);
   48           ActionContext.getContext().getSession().put("balanceDestination", newDest);
   49           ActionContext.getContext().getSession().put("time", new Date());
   50   
   51           Thread.sleep(2000); // to simulate processing time
   52   
   53           return SUCCESS;
   54       }
   55   
   56       public String input() throws Exception {
   57           // prepare input form
   58           Integer balSource = (Integer) ActionContext.getContext().getSession().get("balanceSource");
   59           Integer balDest = (Integer) ActionContext.getContext().getSession().get("balanceDestination");
   60   
   61           if (balSource == null) {
   62               // first time set up an initial account balance
   63               balSource = new Integer(1200);
   64               ActionContext.getContext().getSession().put("balanceSource", balSource);
   65           }
   66   
   67           if (balDest == null) {
   68               // first time set up an initial account balance
   69               balDest = new Integer(2500);
   70               ActionContext.getContext().getSession().put("balanceDestination", balDest);
   71           }
   72   
   73           return INPUT;
   74       }
   75   
   76       public int getAmount() {
   77           return amount;
   78       }
   79   
   80       public void setAmount(int amount) {
   81           this.amount = amount;
   82       }
   83   
   84   }

Save This Page
Home » struts-2.1.8.1-src » org.apache » struts2 » showcase » token » [javadoc | source]