ExtDirectSpring 使用教程

ExtDirectSpring 使用教程

extdirectspringImplementation of the Ext Direct protocol with Java and Spring项目地址:https://gitcode.com/gh_mirrors/ex/extdirectspring

1、项目介绍

ExtDirectSpring 是一个用于 Ext JS 3.x, 4.x, 5.x, 6.x, 7.x 以及 Sencha Touch 2.x 应用程序与 Java/Spring 后端连接的库,实现了 Ext Direct 规范。该库支持所有 Ext Direct 的功能,包括配置注解、简单远程调用、命名参数、方法批处理、表单提交、带文件上传的表单提交、轮询等。

2、项目快速启动

环境准备

  • Java 17
  • Spring 6.0.5
  • Servlet 6.0.0
  • Jackson 2.14.2

添加依赖

pom.xml 中添加以下依赖:

<dependency>
    <groupId>ch.ralscha</groupId>
    <artifactId>extdirectspring</artifactId>
    <version>1.2.0</version>
</dependency>

配置 Spring MVC

web.xml 中配置 Spring 的 DispatcherServlet:

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/dispatcher/*</url-pattern>
</servlet-mapping>

创建 Spring 控制器

创建一个简单的 Spring 控制器,使用 ExtDirectSpring 注解:

import ch.ralscha.extdirectspring.annotation.ExtDirectMethod;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/api")
public class MyController {

    @ExtDirectMethod
    public String sayHello(String name) {
        return "Hello, " + name + "!";
    }
}

配置 ExtDirectSpring

在 Spring 配置文件中启用 ExtDirectSpring:

<bean class="ch.ralscha.extdirectspring.controller.ApiController" />

前端调用

在 Ext JS 中调用远程方法:

Ext.onReady(function () {
    Ext.Ajax.request({
        url: 'api/myController/sayHello',
        params: {
            name: 'World'
        },
        success: function (response) {
            console.log(response.responseText);
        }
    });
});

3、应用案例和最佳实践

案例一:表单提交

使用 ExtDirectSpring 处理表单提交,包括文件上传:

@ExtDirectMethod(ExtDirectMethodType.FORM_POST)
public void submitForm(MultipartHttpServletRequest request) {
    // 处理表单数据
}

案例二:数据加载

使用 ExtDirectSpring 处理数据加载,支持过滤、排序和分页:

@ExtDirectMethod(ExtDirectMethodType.STORE_READ)
public List<Person> loadAllPersons() {
    // 返回数据列表
}

4、典型生态项目

Spring Boot 集成

ExtDirectSpring 可以与 Spring Boot 集成,简化配置过程:

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

安全性

结合 Spring Security 实现安全控制:

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers("/api/**").authenticated()
            .and().formLogin();
    }
}

通过以上步骤,您可以快速启动并使用 ExtDirectSpring 实现 Ext JS 与 Spring 后端的集成。

extdirectspringImplementation of the Ext Direct protocol with Java and Spring项目地址:https://gitcode.com/gh_mirrors/ex/extdirectspring

  • 8
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

仰北帅Bobbie

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值