title: Struts 2: Action classes
body: <p>So, I have next situation:</p>
  
  <p>I have a page with form -> this form submitted to the server -> On the server, present special struts 2 Action for processing submitted form -> and on the last step for user show again this page with form.</p>
  
  <p>when, I try testing this workflow I see one strange for me moment:</p>
  
  <p>When I first time open page - form fields - empty, but after submitted form on the server and reload page - fields - not empty! Fields contains data, that I fill on previous steps.</p>
  
  <p>Here my test Action:</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.List;
  
  import com.opensymphony.xwork2.Action;
  import com.opensymphony.xwork2.ActionSupport;
  
  public class TestAction extends ActionSupport {
  
      private List&lt;Integer&gt; books;
      private String title;
      private String description;
  
      public List&lt;Integer&gt; getBooks() {
          return this.books;
      }
  
      public void setBooks(List&lt;Integer&gt; books) {
          this.books = books;
      }
  
      public String getTitle() {
          return this.title;
      }
  
      public void setTitle(String title) {
          this.title = title;
      }
  
      public String getDescription() {
          return this.description;
      }
  
      public void setDescription(String description) {
          this.description = description;
      }
  
      private static final long serialVersionUID = 1L;
  
      public String show() {
  
          // filling books variable
  
          List&lt;Integer&gt; l = new ArrayList&lt;Integer&gt;();
  
          l.add(1);
          l.add(2);
          l.add(3);
  
          this.books = l;
          return Action.SUCCESS;
      }
  
      public String save() {
  
          // processing title and description variables here!
  
          return show();
  
      }
  
  }
  </code></pre>
  
  <p>My test jsp  file:</p>
  
  <pre><code>&lt;%@ taglib prefix="s" uri="/struts-tags"%&gt;
  
  &lt;html&gt;
  &lt;body&gt;
  
      &lt;s:property value="books" /&gt;
  
  
      &lt;s:form action="saveAction"&gt;
          &lt;s:textfield id="title" label="Title" name="title" /&gt;
          &lt;s:textfield id="description" label="Description"
              name="description" /&gt;
          &lt;s:submit /&gt;
      &lt;/s:form&gt;
  
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And this small part my struts.xml with action tags:</p>
  
  <pre><code>    &lt;action name="showAction" class="action.TestAction" method="show"&gt;
          &lt;result&gt;pages/test.jsp&lt;/result&gt;
      &lt;/action&gt;
  
          &lt;action name="saveAction" class="action.TestAction" method ="save"&gt;
          &lt;result&gt;pages/test.jsp&lt;/result&gt;
      &lt;/action&gt;
  </code></pre>
  
  <p>so, I start investigate this problem, and what I have in result:</p>
  
  <p>Action class instantiate only one time, and after all user request work with this instance of Action class.</p>
  
  <p>so, if in my Action class I have few action methods - all this methods have access to all fields.</p>
  
  <p>It is a good idea?</p>
  
  <p>for example: user send request to the my <code>Action.remove</code> method with some <code>id</code>.</p>
  
  <p>Next, user send request to the my <code>Action.add</code> method but without <code>id</code> parameter. - this situation must be failed! (for example - I forget add Validation).</p>
  
  <p>but - nothing wrong - action instance contains value for field <code>id</code>.  </p>
  
  <p>So, my general question next:</p>
  
  <p><strong>1. One Action Class - One Method - it is the best practice?</strong></p>
  
  <p><strong>2. If used few methods in one Action - I need add some service methods for clear modified fields? Or what I must do in this case?</strong></p>
  
  <p>Thanks!</p>