使用Jquery Ajax将数据从jsp发送到struts2操作类

时间:2013-10-16 16:02:49

标签: java jquery ajax jsp struts2

我需要使用jquery Ajax将表单数据从jsp传递到struts2,并从Struts2动作类接收JSON数据。 我已经给出了下面的代码。当我传递AJAX数据时,

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

“undefined”被传递而不是从jsp到动作类的orijinal值。

我的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功能是:

$("#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>

行动类:

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逻辑。与“未定义”部分相撞。

2 个答案:

答案 0 :(得分:4)

您需要使用.val()功能,如下所示

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

另一种方式是

 $.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));
          }});

希望这有帮助!

答案 1 :(得分:1)

非常重要(如果要将数据从Action Class传输到JQuery),如果没有这3个jar文件,则无法将变量从Action Class传递给JQuery。

  • JS​​ON-简单-1.1.jar
  • struts2的-JSON-插件-2.3.1.2.jar
  • 公地琅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>