我一直堅持這一段時間。我有一個smartgwt小部件listgrid綁定到restdatasource。我已將其URL映射到我的彈簧服務。但是我無法弄清楚如何在spring服務器端檢索JSON dsrequest。即使我的調度程序servlet也不包含參數。帶RestDataSource和SpringController的Smartgwt
我restdatasource如下:
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);
}
};
然後在此數據源設置的操作如下:
// 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 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");
設置這些字段到數據源:
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");
因此,在用戶更改employeeType(因爲它是一個下拉列表)的情況下,會發送更新請求。我JSON字符串發送到如下配置的服務器:
@Controller
@RequestMapping("/myservice")
public class MyService {
...fetch
...update
@RequestMapping(value="/update", method=RequestMethod.POST)
@ResponseBody
public String update()
{
}
我不理解如何檢索(JSON)dsrequest,因爲連我的DispatcherServlet沒有參數(即使我用POSTPARAMS)。開發者控制檯顯示正確的請求,但是我在服務器端沒有收到任何東西。 我春天的servlet的配置如下在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>
我想我失去了一些東西很明顯,但我不能找到它。我是否使用@PathVariable或RequestParams?
如果我使用@RequestBody字符串我得到像這樣的字符串。 '0 =%7B&1 =%0D&2 =%20 3 =%20 4 =%20 5 =%20 6 =%22&7 = F&8 = I&9 = 1&10 = E&11 = I&12 = d&13 =%22 14 =%3A和15 =%22 16 = 0&17 =%22 18 = %2C'。我期待一個JSON字符串。 – sunny 2011-05-17 05:13:16
我剛剛將transformRequest的@Override更改回super.transformRequest(dsRequest)。我現在有一個字符串,我可以繼續工作,並可以繼續前進。 – sunny 2011-05-17 05:20:51