从JSP发送数据使用jQuery的Ajax Struts2的action类数据、jQuery、JSP、action

2023-09-10 16:34:47 作者:稀有man※

我需要从JSP传递表单数据Struts2的使用jQuery的Ajax和接收回程从Struts2的动作类JSON数据。 我给下面的code。当我路过AJAX数据,

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

未定义获得通过,而不是orijinal值从JSP到action类。

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

我的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>

我的Ajax的功能是:

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.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类:

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";
    }
}
}

我没有纳入JSON逻辑呢。施特鲁克与不确定的部分。

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

推荐答案

您需要使用 .VAL()函数如下

 $.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!