如何在Spring MVC应用程序中使用Jquery发送多个参数Ajax请求?

时间:2022-12-08 20:20:46

Here is my relevant codes.my out put is like this. 如何在Spring MVC应用程序中使用Jquery发送多个参数Ajax请求?

这是我的相关代码。我的输出是这样的。

I need to send region and tsrId as parameters to query. here is my code

我需要将区域和tsrId作为参数进行查询。这是我的代码

jsp

jsp

Here is my ajax request with jquery

这是我的jquery ajax请求

<script type="text/javascript">
      $(document).ready(function() {
            var region = document.getElementById('region').value;
            var tsrId = document.getElementById('tsrId').value;
            $('#tsrId').autocomplete({
                serviceUrl: 'getTsrId.html',
                data: ({queryData : {region:region,tsrId:tsrId}}),
                //delimiter: ",",
                transformResult: function(response) {
                return {suggestions: $.map($.parseJSON(response), function(item) {return { value: item.name, data: item.id };
                   })};}});});
</script>    

here is the HTML form

这是HTML表单。

  <td>Region</td>
  <td><input type="text" name="region" id="region"><div class="autocomplete-suggestions"></div></td>
  <td>TSR ID</td>
  <td><input type="text" name="tsrId" id="tsrId" maxlength="8"><div class="autocomplete-suggestions2"></div></td>

here is my controller

这是我的控制器

@RequestMapping(value = "/getTsrId", method = RequestMethod.GET)
public @ResponseBody List<TSRMaster> getTsrId(@RequestParam String tagName,@RequestBody QueryData queryData) {
    List<TSRMaster> tsrMasterList=new ArrayList<TSRMaster>();
    tsrMasterList=gpsdao.getTsrIdList(queryData.getRegion(),queryData.getTsrId());
    return tsrMasterList;
}       

here is my bean class for requestMapping

这是请求映射的bean类

public class QueryData {

    private String region;
    private String  tsrId;

    public String getRegion() {
        return region;
    }
    public void setRegion(String region) {
        this.region = region;
    }
    public String getTsrId() {
        return tsrId;
    }
    public void setTsrId(String tsrId) {
        this.tsrId = tsrId;
    }

}

Please help me to sort out this issue..is there any other alternative solution, please mention that path below thanks.

请帮助我解决这个问题。有没有其他的解决办法,请在下面提到,谢谢。

2 个解决方案

#1


3  

The only way I have been able to make this work so far is to call JSON.stringify() on the client, which turns a JavaScript Object into a JSON String. (To be cross browser compatible you would need json2.js)

到目前为止,我能够完成这项工作的唯一方法是在客户机上调用JSON.stringify(),它将JavaScript对象转换为JSON字符串。(要跨浏览器兼容,需要json2.js)

Then you send this as a String parameter to Spring and parse it there using the Jackson library.

然后将其作为字符串参数发送到Spring,并使用Jackson库对其进行解析。

Sample Code:

示例代码:

Java Script

Java脚本

data: ({queryData : JSON.stringify({region:region,tsrId:tsrId}})),

Java

Java

RequestMapping(value = "/getTsrId", method = RequestMethod.GET)
public @ResponseBody List<TSRMaster> getTsrId(@RequestParam String tagName,@RequestParam String queryData) {

    ObjectMapper myMapper = new ObjectMapper();
    QueryData myQueryData = myMapper.readValue(queryData, QueryData.class);

    List<TSRMaster> tsrMasterList=new ArrayList<TSRMaster>();
    tsrMasterList=gpsdao.getTsrIdList(myQueryData.getRegion(),queryData.getTsrId());
    return tsrMasterList;
}  

#2


0  

You can use Jackson framework for JSON java transformation. Then you can use following method to send data to the controller from the view.

您可以使用Jackson框架进行JSON java转换。然后,您可以使用以下方法从视图向控制器发送数据。

Add jackson jars to the project.

将jackson jar添加到项目中。

jackson-core-2.0.5 jackson-databind-2.0.5 jackson-annotation-2.0.5

jackson-core-2.0.5 jackson-databind-2.0.5 jackson-annotation-2.0.5

Add following code to WebApplicationContext.xml

将以下代码添加到WebApplicationContext.xml中。

<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jacksonMessageConverter"/>
        </list>
    </property>
</bean>

Ajax call

Ajax调用

 $.ajax({
        url: getTsrId.html,
        type: 'GET',
        data: "region=" + region + "&tsrId=" + tsrId,           
        dataType: "json", 
        success: function(response){

//response here
}
});

Controller

控制器

 @RequestMapping(value = "/getTsrId", method = RequestMethod.GET,produces="application/json")
    public @ResponseBody List<TSRMaster> getTsrId(
                @ModelAttribute(value = "queryData") QueryData queryData) {
        List<TSRMaster> tsrMasterList = new ArrayList<TSRMaster>();
        tsrMasterList = gpsdao.getTsrIdList(queryData.getRegion(),queryData.getTsrId());
        return tsrMasterList;
    } 

#1


3  

The only way I have been able to make this work so far is to call JSON.stringify() on the client, which turns a JavaScript Object into a JSON String. (To be cross browser compatible you would need json2.js)

到目前为止,我能够完成这项工作的唯一方法是在客户机上调用JSON.stringify(),它将JavaScript对象转换为JSON字符串。(要跨浏览器兼容,需要json2.js)

Then you send this as a String parameter to Spring and parse it there using the Jackson library.

然后将其作为字符串参数发送到Spring,并使用Jackson库对其进行解析。

Sample Code:

示例代码:

Java Script

Java脚本

data: ({queryData : JSON.stringify({region:region,tsrId:tsrId}})),

Java

Java

RequestMapping(value = "/getTsrId", method = RequestMethod.GET)
public @ResponseBody List<TSRMaster> getTsrId(@RequestParam String tagName,@RequestParam String queryData) {

    ObjectMapper myMapper = new ObjectMapper();
    QueryData myQueryData = myMapper.readValue(queryData, QueryData.class);

    List<TSRMaster> tsrMasterList=new ArrayList<TSRMaster>();
    tsrMasterList=gpsdao.getTsrIdList(myQueryData.getRegion(),queryData.getTsrId());
    return tsrMasterList;
}  

#2


0  

You can use Jackson framework for JSON java transformation. Then you can use following method to send data to the controller from the view.

您可以使用Jackson框架进行JSON java转换。然后,您可以使用以下方法从视图向控制器发送数据。

Add jackson jars to the project.

将jackson jar添加到项目中。

jackson-core-2.0.5 jackson-databind-2.0.5 jackson-annotation-2.0.5

jackson-core-2.0.5 jackson-databind-2.0.5 jackson-annotation-2.0.5

Add following code to WebApplicationContext.xml

将以下代码添加到WebApplicationContext.xml中。

<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jacksonMessageConverter"/>
        </list>
    </property>
</bean>

Ajax call

Ajax调用

 $.ajax({
        url: getTsrId.html,
        type: 'GET',
        data: "region=" + region + "&tsrId=" + tsrId,           
        dataType: "json", 
        success: function(response){

//response here
}
});

Controller

控制器

 @RequestMapping(value = "/getTsrId", method = RequestMethod.GET,produces="application/json")
    public @ResponseBody List<TSRMaster> getTsrId(
                @ModelAttribute(value = "queryData") QueryData queryData) {
        List<TSRMaster> tsrMasterList = new ArrayList<TSRMaster>();
        tsrMasterList = gpsdao.getTsrIdList(queryData.getRegion(),queryData.getTsrId());
        return tsrMasterList;
    }