【问题标题】:Smartgwt with RestDataSource and SpringControllerSmartgwt 与 RestDataSource 和 SpringController
【发布时间】:2011-05-17 04:52:42
【问题描述】:

我已经被困了一段时间了。我有一个绑定到 restdatasource 的 smartgwt 小部件 listgrid。我已将其 URL 映射到我的 spring 服务。但是我无法弄清楚如何在 spring 服务器端检索 JSON dsrequest。甚至我的调度程序 servlet 也不包含这些参数。

我的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)。开发人员控制台显示正确发出的请求,但我在服务器端没有收到任何内容。 我的 spring 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 String 我得到一个这样的字符串。 0=%7B&amp;1=%0D&amp;2=%20&amp;3=%20&amp;4=%20&amp;5=%20&amp;6=%22&amp;7=f&amp;8=i&amp;9=l&amp;10=e&amp;11=I&amp;12=d&amp;13=%22&amp;14=%3A&amp;15=%22&amp;16=0&amp;17=%22&amp;18=%2C 。我期望一个 json 字符串。
  • 我刚刚把transformRequest的@Override改回了super.transformRequest(dsRequest)。我现在有一个字符串,我可以使用它并且可以继续前进。

标签: spring rest smartgwt


【解决方案1】:

找到我自己的答案。希望这对某人有所帮助。

update.setDataProtocol(DSProtocol.POSTPARAMS);

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) { }

【讨论】:

  • 谢谢,这对我将 spring3 mvc 与 SmartGwt Rest Datasource 集成有很大帮助。
猜你喜欢
  • 2013-08-19
  • 2012-11-07
  • 1970-01-01
  • 2013-03-19
  • 2013-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多