3

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 ,

url:'search.action?searchText='+ $('searchValue').value+'&environment='+$('environmentSelect').value

"undefined" is getting passed instead of the orijinal value from jsp to the action class.

My JSP

 <div id="tab0">
                <s:form action="search" method="post">

                    <table style="margin-left: auto; margin-right: auto">
                        <tr>
                            <td>Environment:</td>
                            <td><select id="environmentSelect" name="environment">
                                    <option value="1">1</option>
                                    <option value="2">2</option>
                                    <option value="3">3</option>
                            </select></td>
                        </tr>
                        <tr>
                            <td>Search Value:</td>
                            <td><input id="searchValue" name="searchText" type="text" /></td>
                        </tr>
                        <tr>
                            <td></td>
                            <td><button id="searchButton">Search</button></td>
                        </tr>
                    </table>
                </s:form>
            </div>

My Ajax function is:

$("#searchButton").on("click",function(){

            console.log("Inside Ajax call = "+  $('#tab0'));

            $.ajax({
                type: 'POST',
                url:'search.action?searchText='+ $('searchValue').value+'&environment='+$('environmentSelect').value,
                dataType: 'json',
                success: function(data){
                    console.log(stringify(data));
                        s=data....
                        }

                    document.getElementById('displayLog').innerHTML=s;
            });

            return false;
        }); 

Struts.xml:

 <struts>
        <constant name="struts.enable.DynamicMethodInvocation"
            value="false" />
        <constant name="struts.devMode" value="true" />
        <constant name="struts.custom.i18n.resources"
            value="ApplicationResources" />
        <constant name="struts.convention.default.parent.package" value="default"/> 
        <constant name="struts.ui.theme" value="simple" />

        <package name="default" extends="struts-default" namespace="/">
            <action name="search" class="com.SearchAction" method="execute">
                <result name="success">/jsp/dummy.jsp</result>
                <result name="error">/jsp/search.jsp</result>
            </action>
        </package>  

    </struts>

Action Class:

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 && searchText != null){
        map1= getMap(environment,searchText);
        return success;
    }
    else{
        return "error";
    }
}
}

I did not incorporate the JSON logic yet. Struck with the "undefined" part.

2 Answers 2

4

You need to use .val() function as below

 $.ajax({
       type: 'POST',
       url:'search.action?searchText='+ $("#searchValue").val()+'&environment='+$("#environmentSelect").val(),
       dataType: 'json',
       success: function(data){
             console.log(stringify(data));
        }});

The other way is to

 $.ajax({
          type: 'POST',
          url:'search.action?searchText='+ document.getElementById('searchValue').value  +'&environment='+document.getElementById('environmentSelect').value,
          dataType: 'json',
          success: function(data){
          console.log(stringify(data));
          }});

Hope this helps!

Sign up to request clarification or add additional context in comments.

5 Comments

Hey thank you for answer. Getting the form value by using document.getElementId works fine but getting the data using $("#searchValue").val() still gives undefined. I will look in to it. Anyway Thank you !
it shouldn't give you undefined, if you have jquery.min.js in your script
I got that there was a typo. Thank you ! Do you have any idea how to receive the JSON data in to a jsp page? If you see in my struts.xml if it is success i am directing it to dummy.jsp. That page contains only the <s:property value="map1"/> which i am setting it in my action class. Now i need to display that in the dummy.jsp. How to do that?
You are expecting a json value from server. so, you need to create the json string and return to the client. Check this link mkyong.com/struts2/struts-2-and-json-example
@Keerthivasan: What if you have many elements in the form and we want to pass all those parameter as a bean instead of passing each and every parameter separately, like we use model in struts2.Could you please advise if that can be done
1

Very Important (If you want to transfer data from Action Class to JQuery) without these 3 jar files you can not pass variable from Action Class to JQuery.

  • json-simple-1.1.jar
  • struts2-json-plugin-2.3.1.2.jar
  • commons-lang-2.6.jar

Struts.xml

<package name="json" namespace="/" extends="json-default">
   <action name="action_name" class="com.ActionClass" method="execute">
       <result type="json">
           <param name="root">jsonData</param>
       </result>
   </action>
</package>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.