title: How to use multiple forms / actions in a single view in Struts 2
body: <p>I have a search box which is displayed on every page. The JSP code for the search box is inserted into every page via tiles.</p>
  <p>The search box has a form and an action class <code>SearchAction</code> which needs to preload some properties for drop down boxes. The <code>SearchAction</code> class has an <code>input()</code> method, which does this initialization.</p>
  <p>Some pages also have their own form in the main area. Also with their own action class. They also have an <code>input()</code> method which does some preloading.</p>
  <ol>
  <li>Is it possible to use two actions in the same view?</li>
  <li>How can each form have access to it's own actions's properties.</li>
  <li>And how could I manage to have the input method of both action classes be called before the JSP is rendered?</li>
  </ol>
  <p><strong>Update:</strong></p>
  <p>I am adding a trimmed down example, since it's probably not clear what I am trying to do. It's a register page <code>register.jsp</code> with an <code>RegisterAction</code>. And the page also contains the search form. <em>(BTW: I left out the getter/setter and other stuff in the action classes to keep it short)</em>:</p>
  <p><strong><code>register.jsp</code>:</strong></p>
  <pre><code>&lt;s:form action=&quot;executeSearch&quot;&gt;
      &lt;s:textfield key=&quot;name&quot; label=&quot;Name&quot;/&gt;
      &lt;s:textfield key=&quot;age&quot;  label=&quot;Age&quot;/&gt;
      &lt;s:submit/&gt;
  &lt;/s:form&gt;
  
  &lt;s:form action=&quot;executeRegister&quot;&gt;
      &lt;s:textfield key=&quot;firstName&quot; label=&quot;First Name&quot;/&gt;
      &lt;s:textfield key=&quot;lastName&quot; label=&quot;Last Name&quot;/&gt;
      &lt;s:textfield key=&quot;age&quot; label=&quot;Age&quot;/&gt;
      &lt;s:submit/&gt;
  &lt;/s:form&gt;
  </code></pre>
  <p><strong><code>struts.xml</code>:</strong></p>
  <pre><code>&lt;action name=&quot;*Search&quot; class=&quot;action.SearchAction&quot; method=&quot;{1}&quot;&gt;
      &lt;result name=&quot;success&quot;&gt;/searchresult.jsp&lt;/result&gt;
  &lt;/action&gt;
  
  &lt;action name=&quot;*Register&quot; class=&quot;action.RegisterAction&quot; method=&quot;{1}&quot;&gt;
      &lt;result name=&quot;input&quot;&gt;/register.jsp&lt;/result&gt;
      &lt;result name=&quot;success&quot;&gt;/registerOk.jsp&lt;/result&gt;
  &lt;/action&gt;
  </code></pre>
  <p><strong><code>SearchAction.java</code>:</strong></p>
  <pre><code>public class SearchAction extends ActionSupport {
  
      private String name;
      private int age;
  
      @Override
      public String input() throws Exception {
          // preload the search form with some demo data
          name = &quot;test&quot;;
          age = 20;
          return INPUT;
      }
  
      @Override
      public String execute() throws Exception {
          return SUCCESS;
      }
      
      ...
  }
  </code></pre>
  <p><strong><code>RegisterAction.java</code>:</strong></p>
  <pre><code>public class RegisterAction extends ActionSupport {
  
      private String firstName;
      private String lastName;
      private int age;
  
      @Override
      public String input() throws Exception {
          // preload the register form with some demo data
          firstName = &quot;John&quot;;
          lastName = &quot;Rambo&quot;;
          age = 10;
          return INPUT;
      }
  
      @Override
      public String execute() throws Exception {
          return SUCCESS;
      }
      
      ...
  }
  </code></pre>
  <p>Let's say I call the action <code>inputRegister.action</code>. Then <code>RegisterAction.input()</code> is called. The properties are set. And result <code>SUCCESS</code> causes <code>register.jsp</code> to be rendered.</p>
  <p>But what about my search form. How do I get access to the search action and it's model. Neither of those are on the <code>ValueStack</code> of course. And neither do I see a way to call any methods of <code>SearchAction</code> in order to initialize it's model. I deliberately choose <code>age</code> to be in both action classes. In the rendered page you can see that the search form also accesses the <code>RegisterAction-*.properties</code> (because that's on top of the <code>ValueStack</code>). But it needs to access <code>SearchAction</code>. It must display 20 and not 10.</p>
  <p><img src="https://i.stack.imgur.com/TY1xs.png" alt="Rendered Jsp Page" /></p>
  <p>I am clearly doing it wrong. But even after a lot of googling, I still did't find out the right way to do it.</p>