Here is my relevant codes.my out put is like this.
I need to send region and tsrId as parameters to query. here is my code
jsp
Here is my ajax request with jquery
<script type="text/javascript">
$(document).ready(function() {
var region = document.getElementById('region').value;
var tsrId = document.getElementById('tsrId').value;
$('#tsrId').autoplete({
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
<td>Region</td>
<td><input type="text" name="region" id="region"><div class="autoplete-suggestions"></div></td>
<td>TSR ID</td>
<td><input type="text" name="tsrId" id="tsrId" maxlength="8"><div class="autoplete-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
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.
Here is my relevant codes.my out put is like this.
I need to send region and tsrId as parameters to query. here is my code
jsp
Here is my ajax request with jquery
<script type="text/javascript">
$(document).ready(function() {
var region = document.getElementById('region').value;
var tsrId = document.getElementById('tsrId').value;
$('#tsrId').autoplete({
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
<td>Region</td>
<td><input type="text" name="region" id="region"><div class="autoplete-suggestions"></div></td>
<td>TSR ID</td>
<td><input type="text" name="tsrId" id="tsrId" maxlength="8"><div class="autoplete-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
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.
Share Improve this question edited Feb 24, 2014 at 4:24 Priyan RockZ asked Feb 24, 2014 at 4:05 Priyan RockZPriyan RockZ 1,6158 gold badges41 silver badges68 bronze badges 3- I don't see this as spring / spring-mvc related. You seem to be having problem with JQuery autoplete plugin – gerrytan Commented Feb 24, 2014 at 4:15
- yes dear gerrytan.but its not get by mvc method.what is the error in my app.jackson mapper beans also added in servlet xml file – Priyan RockZ Commented Feb 24, 2014 at 4:22
-
data: ({queryData : {region:region,tsrId:tsrId}})
what is intent of using()
? – Vikram Commented Feb 24, 2014 at 4:37
2 Answers
Reset to default 3The 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 patible you would need json2.js)
Then you send this as a String parameter to Spring and parse it there using the Jackson library.
Sample Code:
Java Script
data: ({queryData : JSON.stringify({region:region,tsrId:tsrId}})),
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;
}
You can use Jackson framework for JSON java transformation. Then you can use following method to send data to the controller from the view.
Add jackson jars to the project.
jackson-core-2.0.5 jackson-databind-2.0.5 jackson-annotation-2.0.5
Add following code to 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({
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;
}