spring - 使用 ExtJS 和 Spring 构建应用程序

标签 spring tomcat sencha-cmd extjs6-classic

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

How I can say to ExtJS.Store that requests need to send to localhost:8080/** instead of localhost:1841/**?



对不起我的英语!

最佳答案

在 ExtJS 中有 singleton 你可以使用这个类。

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

For this "How I can say to ExtJS.Store that requests need to send to localhost:8080/** instead of localhost:1841/**?"

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

Firstly create singletone class

/*
 * 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);
    },
});

Create/Define your store

//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 文档

关于spring - 使用 ExtJS 和 Spring 构建应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48020553/

相关文章:

java - 我可以在 Spring 中设置默认的 AnnotationBeanNameGenerator 吗?

spring - 用 gradle 构建 spring

java - 基于云的应用程序作为 Java 后端和 Android 前端的 Restful 服务

spring-mvc - Tomcat内存泄漏

Spring Batch - 如何使用分区并行读写数据?

java - 方法结束后丢失已确定的实例/null (@Autowire)

java - 如何确定 JNI 全局引用内存泄漏的原因?

extjs - 为什么我在构建 sencha 应用程序时会出现 YUI 解析错误?

javascript - d3.js 作为带有 cordova 的应用程序运行

extjs - Sencha CMD,从 6.0 升级到 6.1 IndexOutOfBoundsException