Smartgwt with RestDataSource and SpringController

帅比萌擦擦* 提交于 2019-12-10 11:40:38

问题


I have been stuck at this for a while. I have a smartgwt widget listgrid tied to a restdatasource. I have mapped its URLs to my spring services. However I cannot figure out how to retrieve the JSON dsrequest on spring server side. Even my dispatcher servlet does not contain the params.

My restdatasource is as follows:

RestDataSource myDS = new RestDataSource() {  
                @Override  
                protected Object transformRequest(DSRequest dsRequest) {
                    dsRequest.setContentType("application/json");
                    JavaScriptObject jso = dsRequest.getData();
            String s1 = JSON.encode(jso);
            return s1;
//                  return super.transformRequest(dsRequest);  
                }  
                @Override  
                protected void transformResponse(DSResponse response, DSRequest request, Object data) {  
                    super.transformResponse(response, request, data);  
                }  
            };

Then on this datasource set the operations as follows:

// set the operation on the datasource  
            OperationBinding fetch = new OperationBinding();  
            fetch.setOperationType(DSOperationType.FETCH);  
            fetch.setDataProtocol(DSProtocol.POSTMESSAGE);  
            OperationBinding add = new OperationBinding();  
            add.setOperationType(DSOperationType.ADD);  
            add.setDataProtocol(DSProtocol.POSTMESSAGE);  
            OperationBinding update = new OperationBinding();  
            update.setOperationType(DSOperationType.UPDATE);  
            update.setDataProtocol(DSProtocol.POSTPARAMS);  
            OperationBinding remove = new OperationBinding();  
            remove.setOperationType(DSOperationType.REMOVE);  
            remove.setDataProtocol(DSProtocol.POSTMESSAGE);  
            myDS.setOperationBindings(fetch, add, update, remove);
            myDS.setDataFormat(DSDataFormat.JSON);
//          myDS.setDataProtocol(DSProtocol.POSTMESSAGE);

Set some fields in the datasource:

// set the values for the datasource
            DataSourceTextField Id = new DataSourceTextField("Id", "Id");
            Id.setPrimaryKey(true);  
            Id.setCanEdit(false);  
            DataSourceTextField name= new DataSourceTextField("name", "Name");
            name.setCanEdit(false);
            DataSourceTextField employeeType= new DataSourceTextField("employeeType", "employeeType");
            employeeType.setCanEdit(true);
            employeeType.setValueMap("Manager", "Associate", "Contractor");

Set these fields to the datasource:

myDS.setFields(Id, name,employeeType);  
myDS.setFetchDataURL("/rest/myservice/fetch");  
myDS.setAddDataURL("/rest/myservice/add");  
myDS.setUpdateDataURL("/rest/myservice/update");  
myDS.setRemoveDataURL("/rest/myservice/remove");

So in the case that the user changes the employeeType (because it's a dropdown), an update request is sent. I send the JSON string to the server configured as below:

@Controller
@RequestMapping("/myservice")
public class MyService {
...fetch
...update
    @RequestMapping(value="/update", method=RequestMethod.POST)
    @ResponseBody
    public String update()
    {
         }

I am failing to understand how to retrieve the (JSON) dsrequest because even my DispatcherServlet does not have the parameters (even if I use POSTPARAMS). The developer console shows the request made correctly but I don't receive anything on the server side. My spring servlet is configured as below in web.xml:

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>    </servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>

I think I am missing something obvious but I can't locate it. Do I use @PathVariable or RequestParams?


回答1:


Found my own answer. Hope this helps someone.

update.setDataProtocol(DSProtocol.POSTPARAMS);

to

update.setDataProtocol(DSProtocol.POSTMESSAGE);
@Override
protected Object transformRequest(DSRequest dsRequest) {
    JavaScriptObject jso = dsRequest.getData(); 
    String s1 = JSON.encode(jso); 
return s1; 
}
@RequestMapping(value="/update", method=RequestMethod.POST)
@ResponseBody public String update(@RequestBody String json) { }


来源:https://stackoverflow.com/questions/6026386/smartgwt-with-restdatasource-and-springcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!