使用ExtJS和Spring构建应用程序

时间:2017-12-29 09:49:07

标签: spring tomcat sencha-cmd extjs6-classic

我正在学习ExtJS框架,用于前端端ExtJS和后端端JavaEE Spring框架(它配置为REST服务)的实验。
所以,我在localhost:1841开始我的前端部分,在localhost:8080开始后端部分。问题是:

  

我如何向ExtJS.Store说请求需要发送给   localhost:8080 / **而不是localhost:1841 / **?



抱歉,我的英文!

1 个答案:

答案 0 :(得分:1)

在ExtJS中有 singleton ,您可以使用此类。

Singleton 模式是一种设计模式,它将类的实例化限制为仅一个对象。当整个系统只需要一个对象时,这很有用。 Singleton模式提供对特定实例的单点访问。单例模式的实现必须满足单实例和全局访问原则。

  

对于这个“我怎么能对ExtJS.Store说请求需要发送到localhost:8080 / **而不是localhost:1841 / **?”

您可以在您的应用中使用如下代码:

  

首先创建singletone

/*
 * Create singletone class in your application
 * This class you can access in your application anywhere you want within the app.
 * Usage: 
 * commonUtility.getServerUrl();// whatever property you have defined inside of config you can access like this
 */
Ext.define('APPNAME.utils.SingleToneClassName', {
    alternateClassName: 'commonUtility',
    singleton: true,
    config: {
        /*
         * you can put local or live also or whatever you want.
         * for local it will be ip address like this {'http://192.168.30.83:8080/'}
         * for live is will be live tomcate  host  url {http://example.com/}
         */
        serverURL: 'http://192.168.30.83:8080/'
    },

    constructor: function(config) {
        var me = this;

        me.initConfig(config);
    },
});
  

创建/定义商店

//Your store
Ext.define('APPNAME.store.StoreName', {
    extend: 'Ext.data.Store',
    fields: ['your fields here'],
    storeId: 'storeIdHere',
    alias: "store.storeAliasHere",
    proxy: {
        type: 'ajax',
        url: commonUtility.getServerUrl() + 'your Server Method name here', //Based on your server URL acceptance
        withCredentials: true,
        reader: {
            type: 'json',
            rootProperty: 'data',
            keepRawData: true
        }
    },
    autoLoad: true, //If you need auto load then put true otherwise false
    listeners: {
        beforeload: function(store, operation, options) {
            //If you have token based authenthication then you need to put like below
            store.getProxy().setHeaders({
                "x-auth-token": 'your token here'
            });
            //If you have need to pass some  parameter in API method then you can pass like below
            store.getProxy().extraParams.your_parameter_name = 'value';
        }
    },
});

//If you want to load your store on some event or any other functions
//then

Ext.getStore('your_storeId_herer').load({
    url: commonUtility.getServerUrl() + 'your Server Method name here', //Based on your server URL acceptance
    params: {
        //If you have need to pass some params in server side then 
        //you put here like
        name: 'value'
    }
});

我希望这会对你有所帮助。有关详细信息,请参阅 ExtJS6.x 文档