【问题标题】:Does smartgwt supports mulitple record updates on one HTTP callsmartgwt 是否支持一个 HTTP 调用上的多个记录更新
【发布时间】:2018-08-17 18:22:06
【问题描述】:

在 smartgwt 中,如果我们想更新或添加新记录到 listgrid,它允许在一次 HTTP 调用中添加一条记录。我想在一次 HTTP 调用中添加或更新多条记录。我正在使用 Spring 框架。我找到了一些参考资料来解决这个问题,方法是排队请求并在一次 HTTP 调用中将排队的请求发送到后端。但是,我对那里的解释不清楚。我想知道是否有人可以为客户端和服务器端逻辑提供示例代码。

这是参考链接: https://www.smartclient.com/smartgwt/javadoc/com/smartgwt/client/data/RestDataSource.html

【问题讨论】:

    标签: java spring smartgwt


    【解决方案1】:

    使用 SmartGWT 可以通过多种方式完成此操作。我认为最简单的方法之一是使用内置的 sql 数据源。

    使用 SQLDataSource,您可以像这样执行添加操作。

    Record record = new Record();
    record.setAttribute("name", "Tom")
    record.setAttribute("age", "28");
    
    Record record2 = new Record();
    record2.setAttribute("name", "John")
    record2.setAttribute("age", "50");
    
    DataSource myDataSource = DataSource.get("myDataSource")
    myDataSource.addData(record);
    myDataSource.addData(record2);
    

    但这会发送两个单独的 HTTP 请求,每个添加操作一个。但是,您可以使用 RPCManager 将它们放入单个 HTTP 调用中。可以这样完成:

    RPCManager.startQueue();
    myDataSource.addData(record);
    myDataSource.addData(record2);
    RPCManager.sendQueue(new RPCQueueCallback() {
            @Override
            public void execute(RPCResponse... rpcResponses) {
    
            }
    });
    

    如果您选择像这样使用“开箱即用”的 sql 数据源,那么您可以使用非常少的服务器端代码进行保存/更新。你只需要有一个像这样定义的数据源:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xml>
    <DataSource
            ID="Person"
            serverType="sql"
            tableName="Person"
            >
        <fields>
            <field primaryKey="true" name="personId" autoGenerated="true" hidden="true"/>
            <field name="name" length="100" type="text" required="true"  />
            <field name="age" type="integer" required="true"  />
    
        </fields>
    </DataSource>
    

    但是,如果您想要或需要除了基本的添加/更新操作之外的其他逻辑(如果您希望能够将代码放在服务器端 - 那么您将需要定义一个服务器端 bean)。您可以通过多种方式在数据源中定义服务器端 bean。我这样做的方式是这样的:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xml>
    <DataSource
            ID="Person"
            serverType="sql"
            tableName="Person"
    
            >
         <serverObject lookupStyle="spring" bean="personDataSource" />
        <fields>
            <field primaryKey="true" name="personId" autoGenerated="true" hidden="true"/>
            <field name="name" length="100" type="text" required="true"  />
            <field name="age" type="integer" required="true"  />
    
        </fields>
    </DataSource>
    

    然后你需要像这样定义一个服务器端 bean:

    @Component("personDataSource")
    public class PersonDataSource {
    
        @Autowired
        PersonService service;
    
        private final Logger logger = LogManager.getLogger(PersonDataSource.class);
    
        public DSResponse add(DSRequest dsRequest) {
            try {
                String primaryKey = (String) dsRequest.getFieldValue("personId");
    
    
                service.create(person);
    
                return new DSResponse().setSuccess();
            } catch (Exception e) {
                logger.error("Failed to add " + dsRequest.getValues(), e);
                e.printStackTrace();
                return new DSResponse().setFailure();
            }
        }
    

    【讨论】:

    • 谢谢艾伦。我用过这个方法。我的服务器端是模型驱动的。服务器端代码将如何。??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-22
    • 1970-01-01
    • 2015-10-27
    • 2018-06-06
    • 2021-09-28
    相关资源
    最近更新 更多