문제

Here is my relevant codes.my out put is like this. enter image description here

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').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

  <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

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.

도움이 되었습니까?

해결책

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)

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;
    } 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top