title: Sending data from jsp to struts2 action class using Jquery Ajax
body: <p>I need to pass the form data from jsp to struts2 using jquery Ajax and receive back a JSON data from Struts2 action class. 
  I have given the code below. When i am passing the AJAX data ,</p>
  
  <pre><code>url:'search.action?searchText='+ $('searchValue').value+'&amp;environment='+$('environmentSelect').value
  </code></pre>
  
  <p>"undefined" is getting passed instead of the orijinal value from jsp to the action class.</p>
  
  <p>My JSP</p>
  
  <pre><code> &lt;div id="tab0"&gt;
                  &lt;s:form action="search" method="post"&gt;
  
                      &lt;table style="margin-left: auto; margin-right: auto"&gt;
                          &lt;tr&gt;
                              &lt;td&gt;Environment:&lt;/td&gt;
                              &lt;td&gt;&lt;select id="environmentSelect" name="environment"&gt;
                                      &lt;option value="1"&gt;1&lt;/option&gt;
                                      &lt;option value="2"&gt;2&lt;/option&gt;
                                      &lt;option value="3"&gt;3&lt;/option&gt;
                              &lt;/select&gt;&lt;/td&gt;
                          &lt;/tr&gt;
                          &lt;tr&gt;
                              &lt;td&gt;Search Value:&lt;/td&gt;
                              &lt;td&gt;&lt;input id="searchValue" name="searchText" type="text" /&gt;&lt;/td&gt;
                          &lt;/tr&gt;
                          &lt;tr&gt;
                              &lt;td&gt;&lt;/td&gt;
                              &lt;td&gt;&lt;button id="searchButton"&gt;Search&lt;/button&gt;&lt;/td&gt;
                          &lt;/tr&gt;
                      &lt;/table&gt;
                  &lt;/s:form&gt;
              &lt;/div&gt;
  </code></pre>
  
  <p>My Ajax function is:</p>
  
  <pre><code>$("#searchButton").on("click",function(){
  
              console.log("Inside Ajax call = "+  $('#tab0'));
  
              $.ajax({
                  type: 'POST',
                  url:'search.action?searchText='+ $('searchValue').value+'&amp;environment='+$('environmentSelect').value,
                  dataType: 'json',
                  success: function(data){
                      console.log(stringify(data));
                          s=data....
                          }
  
                      document.getElementById('displayLog').innerHTML=s;
              });
  
              return false;
          }); 
  </code></pre>
  
  <p>Struts.xml:</p>
  
  <pre><code> &lt;struts&gt;
          &lt;constant name="struts.enable.DynamicMethodInvocation"
              value="false" /&gt;
          &lt;constant name="struts.devMode" value="true" /&gt;
          &lt;constant name="struts.custom.i18n.resources"
              value="ApplicationResources" /&gt;
          &lt;constant name="struts.convention.default.parent.package" value="default"/&gt; 
          &lt;constant name="struts.ui.theme" value="simple" /&gt;
  
          &lt;package name="default" extends="struts-default" namespace="/"&gt;
              &lt;action name="search" class="com.SearchAction" method="execute"&gt;
                  &lt;result name="success"&gt;/jsp/dummy.jsp&lt;/result&gt;
                  &lt;result name="error"&gt;/jsp/search.jsp&lt;/result&gt;
              &lt;/action&gt;
          &lt;/package&gt;  
  
      &lt;/struts&gt;
  </code></pre>
  
  <p>Action Class:</p>
  
  <pre><code>public class SearchAction extends ActionSupport {
  
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
  
  
    private String environment;
    private String searchText;
  
  
      public String getEnvironment() {
          return environment;
      }
  
      public void setEnvironment(String environment) {
          System.out.println("environment in setter new = "+ environment);
          this.environment = environment;
      }
  
      public String getSearchText() {
          return searchText;
      }
  
      public void setSearchText(String searchText) {
          System.out.println("searchText in setter = "+ searchText);
          this.searchText = searchText;
      }
  
  
  
    public String execute() {
  
      Map map1 = new Map();
      if(environment !=null &amp;&amp; searchText != null){
          map1= getMap(environment,searchText);
          return success;
      }
      else{
          return "error";
      }
  }
  }
  </code></pre>
  
  <p>I did not incorporate the JSON logic yet. Struck with the "undefined" part.</p>